js鼠标滚轮事件如何做兼容

兼容谷歌,火狐,IE



呼如林
浏览 752回答 1
1回答

达令说

window.onload=function()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var oBox=document.getElementById('box');&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; wheel(oBox,function(down){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(down);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function wheel(obj,wheelFn)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; window.navigator.userAgent.indexOf('Firefox')!=-1 ? obj.addEventListener('DOMMouseScroll',fn,false) : addEvent(obj,'mousewheel',fn);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; function fn(ev)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var oEvent=ev||event;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var down=oEvent.wheelDelta ? down=oEvent.wheelDelta<0 : down=oEvent.detail>0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ev.preventDefault&&ev.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wheelFn&&wheelFn(down);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function addEvent(obj,sEv,fn)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;obj.addEventListener ? obj.addEventListener(sEv,fn,false) : obj.attachEvent('on'+sEv,fn);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript