滚动备份时Jquery无法正常工作

时间:2018-01-17 14:01:00

标签: javascript jquery scroll

所以我有我的代码设置,当向下滚动(有效)时让一个框淡出,但是当我告诉它在向上滚动时淡入时它不起作用。

$(window).scroll(function() {
    if ($(this).scrollTop() > 200) { //use `this`, not `document`
        $(".box").animate({'opacity':'1'}, 1000);
    }
});
$(window).scroll(function() {
    if ($(this).scrollTop() < 200) { //use `this`, not `document`
        $(".box").animate({'opacity':'0'}, 1000);
    }
});

提前感谢您的回复

1 个答案:

答案 0 :(得分:2)

在开始使用新的annimate函数.stop()

之前,您必须停止设置

见下面的工作小提琴:(向下滚动以查看效果)

&#13;
&#13;
.box {
  width:100px;
  height:100px;
  background-color:red;
  position:fixed;
  right:10px;
  top:10px;
  opacity:0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box"></div>

<p>some paragraph</p>
<p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p><p>some paragraph</p>
&#13;
{{1}}
&#13;
&#13;
&#13;