我使用这个代码时,当我滚动到它时,我发现fadein元素(带有class:hideme),但只有当它完全可见时它会消失,有没有办法可以调整代码,一旦它出现在浏览器中就会淡入?
我试图改变">"到" ="在这一行,它有点工作:
if( bottom_of_window > bottom_of_object ){
但是当我申请第二个元素
时,它无法正常工作谢谢!
原始链接: http://jsfiddle.net/tcloninger/e5qaD/
这里是代码:
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},500);
}
});
});
});
答案 0 :(得分:1)
您只需要检查对象的顶部
var top_of_object = $(this).position().top;
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the top of the object is visible in the window, fade it in */
if( bottom_of_window >= top_of_object ){
$(this).animate({'opacity':'1'},500);
}
在此处更新了JSFiddle:http://jsfiddle.net/e5qaD/2727/