function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
从这里引用简单的油门功能
element.on('mousewheel DOMMouseScroll',throttle(function(){
...
}))
或者您可以在滑块移动时使用“锁”来锁定您的事件处理程序:
element.on('mousewheel DOMMouseScroll',function(){
if(!element.hasClass('locked')){
element.addClass('locked');
...//process, move next, move previous
element.removeClass('locked');
}
})
这个很容易理解
一个完整的:
(function() {
var slider = revapi1;
slider.parent().on('mousewheel DOMMouseScroll',throttle(function(event) {
if(event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
slider.revprev();
}else {
slider.revnext();
}
},250));
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}
})()