简体   繁体   English

jQuery:某些东西延迟了动画

[英]jQuery: Something is delaying animations

What makes the animation delay? 是什么导致动画延迟? All jQuery on the site is having some sort of delay.. 该网站上的所有jQuery都有某种延迟。

$(function(){
  $(window).scroll(function() {
       var elementTop = $('body').offset().top; 
       var position = elementTop+ $(window).scrollTop();
       if(position >= 20){
           $('#top').animate({top: '40px'}, 300);
       } else if(position < 20){
           $('#top').animate({top: '80px'}, 300);
       }
console.log(position);
  });   
});

Live: Link here - It's the menu/navigation 实时: 链接到此处 -这是菜单/导航

Your code is being fired each time you scroll. 每次滚动时都会触发代码。 The animate methods are being concatenated, running one after the other. animate方法被连接起来,一个接一个地运行。 To achieve what you want, you need to stop the current animation and start a new one: 要实现所需的效果,您需要停止当前动画并开始一个新动画:

$(function(){
    $(window).scroll(function(){
        var elementTop = $('body').offset().top,
        position = elementTop+ $(window).scrollTop();

        if (position >= 20){
            $('#top').stop().animate({top: '40px'}, 300);
        }
        else if (position < 20) {
           $('#top').stop().animate({top: '80px'}, 300);
        }
    });   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM