js/jq判斷鼠標滾輪方向


js判斷鼠標滾輪方向:

var scrollFunc = function (e) {  
        e = e || window.event;  
        if (e.wheelDelta) {  //判斷瀏覽器IE,谷歌滑輪事件               
            if (e.wheelDelta > 0) { //當滑輪向上滾動時  
               //事件
            }  
            if (e.wheelDelta < 0) { //當滑輪向下滾動時  
                //事件 
            }  
        } else if (e.detail) {  //Firefox滑輪事件  
            if (e.detail> 0) { //當滑輪向上滾動時  
               //事件 
            }  
            if (e.detail< 0) { //當滑輪向下滾動時  
                //事件  
            }  
        }  
    }  
    //給頁面綁定滑輪滾動事件  
    if (document.addEventListener) {//firefox  
        document.addEventListener('DOMMouseScroll', scrollFunc, false);  
    }  
    //滾動滑輪觸發scrollFunc方法  //ie 谷歌  
    window.onmousewheel = document.onmousewheel = scrollFunc; 

jq看起來就很簡單:

$(document).ready(function(){
            var p=0;t=0;
            $(window).scroll(function(e){
                p=$(this).scrollTop();
                if(t<=p){
                    console.log('下滾')
                }
                else{
                    console.log('上滾')
                }
                t = p;
            })
        })

但以上方法會在監聽時調用多次,可以采用節流函數解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM