作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前已经成功地将此代码用于淡入、动画、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/
我是一名优秀的程序员,十分优秀!