功能
监听鼠标滚轮运动方向
代码
javascript
var mouseListener = function() {//监听鼠标滚轮向上或者向下运动
var event = event || window.event;
var delta = event.wheelDelta ? event.wheelDelta : (-event.detail) * 40;
if (delta == 150) console.log('up');
if (delta == -150) console.log('down');
}
//注册事件监听
document.addEventListener('mousewheel', mouseListener);
//FireFox支持一个名为DOMMouseScroll的类似事件,类比于mousewheel。
document.addEventListener('DOMMouseScroll', mouseListener);
扩展函数
javascript
var mouseListener = function(fUp,fDown) {//监听鼠标滚轮向上或者向下运动
var event = event || window.event;
var delta = event.wheelDelta ? event.wheelDelta : (-event.detail) * 40;
if (delta == 150){
console.log('up');
if(fUp instanceof Function) fUp();//触发鼠标滚轮向上运动的函数
}
if (delta == -150){
console.log('down');
if(fDown instanceof Function) fDown();//触发鼠标滚轮向下运动的函数
}
}
//注册事件监听
document.addEventListener('mousewheel', mouseListener);
document.addEventListener('DOMMouseScroll', mouseListener);