鼠标滚轮兼容:
/* 事件:DOMMouseScroll(W3C)、onmousewheel(IE/Opera/ChromeS) 滚轮方向判断:wheelDelta(IE/Opera/Chrome)、detail(Firefox) 滚轮方向值:wheelDelta(上:120,下:-120)、detail(上:-3,下:3) ps:注意某些时候需要阻止滚动默认行为 */ /** * 鼠标滚轮 * @param o{object} 绑定滚轮事件的对象 * @param callback{function} 滚轮上下滚动时执行的函数,接收一个参数(上:120,下:-120) * * wheel(o, function(detail){ * alert(detail); * }); */ function wheel(o, callback){ function fn(e){ var e = e || window.event, detail = 0; if(e.wheelDelta){ // IE/Opera/Chrome detail = e.wheelDelta; }else if(e.detail){ // Firefox detail = -e.detail * 40; } callback(detail); } if(o.addEventListener){ // W3C o.addEventListener(‘DOMMouseScroll‘,fn,false); } o.onmousewheel = fn; // IE/Opera/Chrome }
鼠标左中右按钮兼容:
/* W3C:左(0),中(1),右(2) IE:左(0, 1, 3, 5, 7),中(4),右(2, 6) */ /* * return {number} 0, 1, 2 * * addEvent(document, ‘mousedown‘, function(ev){ * alert(getEventButton(ev)); * }); */ function getEventButton(event){ var ev = event || window.event; if(document.implementation.hasFeature(‘MouseEvents‘, ‘2.0‘)){ return ev.button; }else{ return [0, 0, 2, 0, 1, 0, 2, 0][ev.button]; } }
时间: 2024-10-13 16:30:48