gpt4 book ai didi

javascript - 基于 jQuery 滚动的淡入不等待滚动

转载 作者:行者123 更新时间:2023-11-28 05:57:48 26 4
gpt4 key购买 nike

我之前已经成功地将此代码用于淡入、动画、CSS 过渡和转换。

$(window).scroll( function(){

$('.item1').each(function (i) {

var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* For testing only */
console.log(bottom_of_object);
console.log(bottom_of_window);

/* If the object is visible in the window */
if (bottom_of_window > bottom_of_object) {

$(this).fadeIn(1000, 'swing', function () {
$('.item2').fadeIn(1000, 'swing', function () {
$('.item3').fadeIn(1000, 'swing', function () {
$('.item4').fadeIn(1000, 'swing');
});
});
});
}
})
});

但我无法让它与这个 HTML 一起正常工作。

<div class="container">
<div class="row">
<div class="col-md-3 col-xs-12 img-responsive item item1">
<img src="images/gallery/Brows/brows13.png" />
</div>
<div class="col-md-3 col-xs-12 img-responsive item item2">
<img src="images/gallery/Liner/liner2.png" />
</div>
<div class="col-md-3 col-xs-12 img-responsive item item3">
<img src="images/gallery/Body Ink/BI004.png" />
</div>
<div class="col-md-3 col-xs-12 img-responsive item item4">
<img src="images/gallery/Nails/nails1.png" />
</div>
</div>
</div>

我想不通这次我做错了什么。

应该说得更清楚抱歉,我急于在商店关门前出门。图像淡入,但它们是在页面加载时执行的,而不是在滚动时执行的。

最佳答案

最后您缺少两个 DIVS。

当您陷入困境时,总是验证您的 HTML,因为某些东西在它应该工作的时候不工作。您可以通过这种方式避免大量痛苦。

我有一种感觉,您只是忘记在此处包含最后两个 div,而它们出现在您自己的代码中。但是,我仍然会说验证页面只是为了检查。

当我在这里时,在循环外声明:


var bottom_of_window = $(window).scrollTop() + $(window).height();

您想尽可能减少 DOM 调用,并且这在循环期间不会改变。

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

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