$(window).scroll(function() {
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
// do something
console.log("Haven't scrolled in 250ms!");
}, 250));
});
更新
我写了一个扩展以增强jQuery的默认on
-event处理程序。它将一个或多个事件的事件处理函数附加到所选元素,并在事件未在给定时间间隔内触发时调用处理函数。如果您只想在延迟后触发回调,例如调整大小事件等,这很有用。
重要的是要检查github-repo的更新!
https://github.com/yckart/jquery.unevent.js
;(function ($) {
var on = $.fn.on, timer;
$.fn.on = function() {
var args = Array.apply(null, arguments);
var last = args[args.length - 1];
if (isNaN(last) || (last === 1 && args.pop())) return on.apply(this, args);
var delay = args.pop();
var fn = args.pop();
args.push(function() {
var self = this, params = arguments;
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(self, params);
}, delay);
});
return on.apply(this, args);
};
}(this.jQuery || this.Zepto));
使用它像任何其他on
或bind
-event处理程序,但您可以通过一个额外的参数作为最后:
$(window).on('scroll', function(e) {
console.log(e.type + '-event was 250ms not triggered');
}, 250);
http://yckart.github.com/jquery.unevent.js/
(此演示使用resize
而不是scroll
,但谁在乎?)
可能重复[火灾事件scrollling后滚动条或鼠标滚轮的javascript](http://stackoverflow.com/questions/8931605/fire-event-after-scrollling -scrollbars-or-mousewheel-javascript) – 2012-02-04 21:11:52
太棒了,不完全重复,但确切地说,我正在寻找和帮助我到最后解决我的问题的胡同。谢谢。 – chris 2012-02-04 22:05:02