滑鼠滾動事件

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   java   ar   strong   

主要討論相容性的問題。

 經測試,IE/Opera屬於同一類型,使用attachEvent即可添加滾輪事件。

/*IE註冊事件*/if(document.attachEvent){     document.attachEvent(‘onmousewheel‘,scrollFunc);}

Firefox使用addEventListener添加滾輪事件

/*Firefox註冊事件*/if(document.addEventListener){    document.addEventListener(‘DOMMouseScroll‘,scrollFunc,false);}

Safari與Chrome屬於同一類型,可使用HTML DOM方式添加事件

window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome

其中除Firefox外其餘均可使用HTML DOM方式添加事件,因此添加事件使用以下方式

/*註冊事件*/if(document.addEventListener){    document.addEventListener(‘DOMMouseScroll‘,scrollFunc,false);}//W3Cwindow.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome

detail與wheelDelta

判斷滾輪向上或向下在瀏覽器中也要考慮相容性,現在五大瀏覽器(IE、Opera、Safari、Firefox、Chrome)中Firefox 使用detail,其餘四類使用wheelDelta;兩者只在取值上不一致,代表含義一致,detail與wheelDelta只各取兩個 值,detail只取±3,wheelDelta只取±120,其中正數表示為向上,負數表示向下。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <p><label for="wheelDelta">  滾動值:</label>(IE/Opera)<input type="text" id="wheelDelta" /></p> <p><label for="detail"> 滾動值:(Firefox)</label><input type="text" id="detail" /></p> <script type="text/javascript"> var oTxt=document.getElementById("txt");/************************ 函數:判斷滾輪滾動方向* 參數:event* 返回:滾輪方向 1:向上 -1:向下*************************/var scrollFunc=function(e){    var direct=0;    e=e || window.event;       var t1=document.getElementById("wheelDelta");    var t2=document.getElementById("detail");    if(e.wheelDelta){//IE/Opera/Chrome        t1.value=e.wheelDelta;    }else if(e.detail){//Firefox        t2.value=e.detail;    }    ScrollText(direct);}/*註冊事件*/if(document.addEventListener){    document.addEventListener(‘DOMMouseScroll‘,scrollFunc,false);}//W3Cwindow.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome/Safari</script>

 

滑鼠滾動事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.