如何停止jquery .scroll触发事件太多次了

时间:2013-10-16 06:59:32

标签: javascript jquery animation

我使用spritely.net制作动画它为您提供了启动动画的功能和一个停止动画的功能。

我希望动画在有人开始向下滚动页面时启动,并在停止滚动时停止。

我能够做到这一点,但问题是.scroll会每秒多次触发动画,这会让动画变得疯狂。您可以在此处查看是否向下滚动页面:

http://jsfiddle.net/atyM6/

允许正常运行的动画看起来应该更像:

http://jsfiddle.net/wx7qb/1/

我试过这个插件

http://benalman.com/projects/jquery-throttle-debounce-plugin/

还有这个 http://documentcloud.github.io/underscore/#throttle

但是我无法将我的jquery准备好转换成一个可以传递给它的好函数,并使动画工作。另外我已经看到其他人在没有使用这些插件的情况下管理缩减.scroll(),但我无法调整他们的代码以适应我的目的。

这是我的jquery,用于启动和停止动画 - 用户滚动时的感应。

 $(window).scroll( function() {
 $('#bird')
.sprite({fps: 1, no_of_frames: 16, rewind: true })

});

$(window).scroll(function() {   
clearTimeout($.data(this, 'scrollTimer'));
 $.data(this, 'scrollTimer', setTimeout(function() {
 $('#bird').spStop()

}, 250));

});
$(window).scroll(function() {


$('#bird').spStart()

});

0 个答案:

没有答案
相关问题