我正在尝试使用wheel
事件和一个油门功能创建我自己的简单全高滚动脚本(因为我只希望每X毫秒注册一次滚动。我想将wheel
事件的事件数据传递给函数受到我的throttle()
功能的限制。
这是我的代码的简化版本:
function throttle(callback, limit) {
var wait = false; // Initially, we're not waiting
return function () { // We return a throttled function
if (!wait) { // If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () { // After a period of time
wait = false; // And allow future invocations
}, limit);
}
}
}
function onScroll(event) {
// do stuff here on scroll
}
window.addEventListener('wheel', throttle(onScroll, 700), false);
如果我不需要来自wheel
事件的数据,这将工作正常,但我需要使用onScroll()
确定我的event.deltaY
函数内的滚动方向。
基本上我想将最后一行更改为以下内容(虽然我知道这不起作用):
window.addEventListener('wheel', throttle(onScroll(event), 700), false);
所以...
如何将wheel
事件数据传递到onScroll()
函数中,同时仍然限制它?
我尝试过的:
我尝试了以下代码,以便我可以将event
数据传递到throttle()
函数,该函数成功运行
window.addEventListener('wheel', function(e) {
throttle(onScroll, 700, e)
}, false);
但我不知道如何处理我的e
函数中的throttle()
能够将它传递给onScroll()
函数。我尝试用以下方式更改线路:
callback.call(); // Execute users function
至:
callback.call(e); // Execute users function
(使用e
进入油门功能)
但这阻止了onScroll
函数甚至被调用...(我假设因为throttle()
正在返回一个函数但是这个返回的函数没有被调用,因为它被包装在eventListener
中的另一个函数中?)