无论如何,当你再次滚动到顶部时,这个div会回来

时间:2014-06-09 09:58:46

标签: javascript jquery scroll jquery-animate

所以我将这个div动画完美地移出了页面,但每当我向后滚动它仍然不在页面中我尝试了if / else语句但是它没有回来,任何人都可以帮助我这个?

提前致谢!

$(document).ready(function(){
    $(window).scroll(function(){
        if ($(window).scrollTop() > 300){
            $('.offer').stop().animate({ top: '+= 10' }, 10, "linear");
        }
        else if ($(window).scrollTop() < 300){
            $('.offer').stop().animate({ top: '-=10' }, 10, "linear");
        }
    });
});

2 个答案:

答案 0 :(得分:0)

问题是您每次都会在位置添加或删除10 触发滚动事件。

你应该做的是有一个标志,例如isFurtherThan300,它会跟踪你的位置是在你的截止日期之前还是之后。

然后,如果isFurtherThan300发生变化,请仅执行动画

答案 1 :(得分:0)

尝试以下代码

$(document).ready(function(){
 //Keep track of last scroll
  var lastScroll = 0;
  $(window).scroll(function(){
    var st = $(window).scrollTop();
    if (st > lastScroll){
        $('.offer').stop(true).animate({ top: '+= 100px' }, 10, "linear");
    }
    else{
        $('.offer').stop().animate({ top: '-=100px' }, 10, "linear");
    }
    //Updates scroll position
    lastScroll = st;
  });
});

希望它有所帮助!