这段代码运行良好,但当我在4-5次之后上下滚动时,它会崩溃,所有的元素都会消失。为什么会发生这种事,我该如何解决呢?
$(window).on("load",function() {
$(window).scroll(function() {
var winheight = $(window).innerHeight();
$(".fade").each(function() {
/* Check the location of each desired element */
var objectBottom = $(this).offset().top + $(this).outerHeight();
var windowBottom = $(window).scrollTop() + $(window).innerHeight();
/* If the element is completely within bounds of the window, fade it in */
if ( windowBottom > (objectBottom - (winheight*0.65))) { //object comes into view (scrolling down)
if ($(this).css("opacity")==0) {$(this).fadeTo(500,1);}
} else { //object goes out of view (scrolling up)
if ($(this).css("opacity")==1) {$(this).fadeTo(500,0);}
}
});
}); $(window).scroll(); //invoke scroll-handler on page-load
});发布于 2017-01-24 10:36:00
好的,我假设您的html是这样的:https://jsfiddle.net/szdwwdac/
有时,如果您正在快速上下滚动,当元素正在消失时,您的if就不能正常工作。
if ( windowBottom >= (objectBottom - (winheight*0.65))) {
if ($(this).css("opacity")==0) {$(this).fadeTo(300,1);}
} else { //object goes out of view (scrolling up)
if ($(this).css("opacity")==1) {$(this).fadeTo(300,0);}
}这是因为500毫秒的动画。其中一个解决方案可以是500 of的滚动页的可访问/禁用。您可以检查此解决方案:How to disable scrolling temporarily?
编辑
另一个解决方案可以是:当您在if中时添加一个类“衰落”。然后,在if中,fading元素hasClass“衰落”。如果没有,你可以进去做动画。
https://stackoverflow.com/questions/41810787
复制相似问题