【发布时间】:2018-01-03 06:40:02
【问题描述】:
我找到了一个脚本来更改滚动时的不透明度并对其进行了一些修改。 div 淡出 IN 很好,但是当我回到顶部时,我似乎无法逆转淡入淡出。
当浏览器到达顶部时,我可以让浏览器“提醒”我,但不透明部分没有触发,似乎无法弄清楚原因。
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll(function() {
/* Check the location of each desired element */
$('.hideme').each(function(i) {
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if (bottom_of_window > bottom_of_object) {
$(this).animate({
'opacity': '1'
}, 500);
}
/*attempt to fade it out*/
var scrollPosition = $("body, html").scrollTop();
if (scrollPosition == 0) {
$(this).animate({
'opacity': '0'
}, 500);
}
});
});
});
#container {
height: 2000px;
}
#container DIV {
margin: 50px;
padding: 50px;
background-color: lightgreen;
}
.hideme {
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div>Hello</div>
<div class="hideme">Fade In</div>
</div>
【问题讨论】:
标签: jquery jquery-animate scrolltop