Skip to content

Instantly share code, notes, and snippets.

@seektan
Last active September 6, 2015 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seektan/2da97cfd37f3e7873925 to your computer and use it in GitHub Desktop.
Save seektan/2da97cfd37f3e7873925 to your computer and use it in GitHub Desktop.
handle wheel event
(function (w, modName, time) {
var st
,modName = modName || 'handleWheel'
,_run = 1
,time = time || 100
;
function getWheelDir(e) {
var delta = 0;
e =window.event || e;
if (e.wheelDelta) {
delta = e.wheelDelta/120;
} else if (e.detail) {
delta = -e.detail/3;
}
return delta ;
}
w[modName] = function (doUp, doDown) {
function _handleWheel(e) {
if (_run) {
_run = 0;
setTimeout(function () {
_run = 1;
}, time);
var dir = getWheelDir(e);
if (dir >0) {
(typeof doUp == 'function') && doUp()
}else {
(typeof doDown == 'function') && doDown()
}
}
}
window.onmousewheel = document.onmousewheel = _handleWheel;
if (window.addEventListener) {
window.addEventListener('DOMMouseScroll', _handleWheel, false);
}
}
})(window, 'handleWheel', 100);
//此处将组件注册入指定对象中,下面是参数说明
/**
* @param {Object} arg1 指定注入的对象,可以填window或者其他
* @param {String} arg2 组件名称,你写成啥,后面就调用啥,默认是handleWheel
* @param {Number} arg3 时间系统,组件中模拟了underscore的throttle特性,以避免事件的频繁触发,据统计,100ms是很合适的
**/
//invoke
//这里的handleWheel就是上面传入的第二个参数
handleWheel(function () {
console.log('up') ;
}, function () {
console.log('down') ;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment