【发布时间】:2013-10-09 18:20:20
【问题描述】:
我有一些 div,它们本质上只是彩色矩形,以帮助可视化。当我向下滚动页面时,每个矩形应该 fadeIn 或 fadeOut 取决于滚动条的位置。不幸的是,它吓坏了,褪色更像是一个痉挛的闪光灯。我认为最好通过滚动浏览每个元素的距离来确定不透明度级别,但我什至不确定从哪里开始。
似乎this guy 有类似的问题,但答案无效。
jQuery
$(document).ready(function(){
var $element_array = $("#content").find("div");
$(window).scroll(function(){
$element_array.each (function(){
if (($(this).position().top + $(this).height()) < $(window).scrollTop())
$(this).fadeIn();
if (($(this).position().top + $(this).height()) > $(window).scrollTop())
$(this).fadeOut();
});
});
});
HTML
<div id="content">
<div id="bg1"></div>
<div id="bg2"></div>
<div id="bg3"></div>
</div>
CSS
html,body{height:100%;margin:0;}
#content{
background:#333333;
height:2000px;
z-index:1;
}
#bg1{
background:blue;
height:400px;
width:100%;
z-index:2;
position:fixed;
top:100px;
display: none;
}
#bg2{
background:green;
height:400px;
width:100%;
z-index:3;
position:fixed;
top:200px;
display: none;
}
#bg3{
background:red;
height:400px;
width:100%;
z-index:4;
position:fixed;
top:300px;
display: none;
}
【问题讨论】:
标签: javascript jquery html fadein