简体   繁体   English

jquery 在执行 .hide() 和 .fadeIn() 方法之前闪烁 Div 元素

[英]jquery Flashes Div Element Before Executing .hide() and .fadeIn() Methods

This is my code:这是我的代码:

$('.items').html(response).hide().fadeIn();

The problem is that when this loads, the page "jumps" up due to the element being rendered on page first (having a height) before the .hide().fadeIn() is triggered.. is there some other way to do this?问题是,当加载时,由于元素在.hide().fadeIn()被触发之前首先呈现在页面上(具有高度),页面“跳转”了......还有其他方法可以做到这一点?

如果要保持元素的尺寸不变,则可以改用不透明度:

$('.items').html(response).css({'opacity':0}).animate({'opacity':1});

Hide using CSS and then fade it in when required :使用 CSS 隐藏,然后在需要时将其淡入:

css : css :

.items {
   display:none;
}

JavaScript JavaScript

$('.items').html(response).fadeIn();

This is a cleaner solution since it avoids a flashing effect of the element being added first, then quickly having its opacity set to 0.这是一个更干净的解决方案,因为它避免了先添加元素的闪烁效果,然后快速将其不透明度设置为 0。

This way the elem is added already having an opacity of 0.这样 elem 添加的不透明度为 0。

var elem = $(response).css({'opacity': 0});
$('.items').html(elem);
elem.animate({'opacity': 1});

If you want to show a smooth transtion between existing content and new, try the following:如果要在现有内容和新内容之间显示平滑过渡,请尝试以下操作:

$(function(){
    $('.items').fadeOut(function(){
      $(this).html(response).fadeIn();   
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM