【发布时间】:2017-11-24 19:53:23
【问题描述】:
当不同的元素进入视口视图时,当向下滚动页面时,我想将它们淡入淡出。
然而,我的代码会同时淡入所有 DIV(具有“.fadethis”类),而不是仅在特定元素出现时才出现:
$(window).scroll(function() {
$(".fadethis").each(function() {
var top_of_element = $(this).offset().top;
var bottom_of_element = $(this).offset().top + $(this).outerHeight();
var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
var top_of_screen = $(window).scrollTop();
if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
$(this).fadeIn(10000);
}
});
});
CSS
.fadethis{
display:none;
}
【问题讨论】:
-
你能试试这个吗? $(".fadethis:visible").each(function() {...});
-
你也可以添加你的html代码吗?
-
您的问题可能是这些 div 的样式为“display: none”。在不显示它们的时间内,它们的顶部为 0,底部为 0 + div 的高度。对所有人来说都是一样的。我创建了一支笔,其中 div 可以根据需要淡入:codepen.io/Kathara/pen/RjJjQM
标签: javascript jquery viewport fadein