如何使用scrolltop和animate添加回调

时间:2013-08-28 09:09:00

标签: jquery jquery-animate

对于IE8,我使用此代码,当用户滚动到250或更多时,使用jQuery为页面底部的横幅设置动画。问题是这是非常缓慢并且有很大的延迟,我相信这是因为animate事件被触发了很多次,我需要一个写回.stop()的回调;但我不确定如何/在哪里放这个。有什么想法吗?

} else {
$(window).scroll(function() {
  if ($(this).scrollTop() < 250) {
     if($("#carriage-promo").not(':animated')){
        $("#carriage-promo").animate({
           height: 0
        },100);

     }
  } else {
     if($("#carriage-promo").not(':animated')){
        $("#carriage-promo").animate({
           height: '40px'
        },100);
     }
  }
});
}

1 个答案:

答案 0 :(得分:0)

试试这个:

$(window).scroll(function() {
  if ($(this).scrollTop() < 250) {
     if($("#carriage-promo").not(':animated')){
        $("#carriage-promo").stop(true,true).animate({
           height: 0
        },100);

     }
  } else {
     if($("#carriage-promo").not(':animated')){
        $("#carriage-promo").stop(true,true).animate({
           height: '40px'
        },100);
     }
  }
});