gpt4 book ai didi

javascript - jQuery 滚动淡入崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:24 25 4
gpt4 key购买 nike

此代码运行良好,但是当我在 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
});

最佳答案

好的,我猜你的 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);}
}

是因为500ms的动画。解决方案之一是启用/禁用滚动页面的 500 毫秒。您可以查看此解决方案:How to disable scrolling temporarily?

编辑

另一种解决方案是:在 if 中添加一个“fading”类。然后,在 if 中,评估元素是否具有类“衰落”。如果没有,你可以进去制作动画。

关于javascript - jQuery 滚动淡入崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com