如何检测滚动结束

我的脚本有些问题。 所以,我想检测滚动操作的结束。 当我滚动时我有警报但是如果我结束它则没有。 你能帮助我吗? 这是我的代码:

var animatable = $('body, html'); var animating = false; animatable.animate( {scrollTop: $('#foo').offset()}) $(window).scroll(function(e) { if(!animating){ animatable.stop(false, true); alert('stop scrolling'); } animating = false; });​ 

和一些小提琴: http : //jsfiddle.net/yhnKR/

这是你想要实现的目标:

 $('body').animate( {scrollTop: $('#foo').offset().top},1000,function(){ alert('stop scrolling'); }); 

http://jsfiddle.net/yhnKR/2/

如果使用jquery为滚动设置动画,则不必观看滚动事件。


好的,如果要检测用户何时停止滚动,则必须使用超时来检查用户是否已停止。 否则,您将获得每个滚动步骤的事件。 像这样:

 var delay = 1000; var timeout = null; $(window).bind('scroll',function(){ clearTimeout(timeout); timeout = setTimeout(function(){ alert('scrolling stopped'); },delay); });​​​​​​​​​​ 

http://jsfiddle.net/yhnKR/4/