js滑鼠滾輪上下滾動監聽事件應用執行個體(跨瀏覽器,親測)

來源:互聯網
上載者:User

標籤:js   js滑鼠滾輪事件   滑鼠滾輪上下滾動事件   

最近做項目涉及到多個DIV轉場效果,包括普通按鈕點擊切換和滑鼠滾輪上下滾動切換等;其主要涉及到的內容就是滑鼠滾輪上下切換的事件監聽,此處是第一次接觸到,故在此記錄一下;也希望能幫到各位有需要的朋友。


以下為項目執行個體:

$(function() {

    var count = $("#sysCount").val();
    var isUp = false;

    // 初始化子系統模組
    if (count > 6) {
        $("#panel").css("width", "1200px");
        $(".line2").css("width", "550px");

        $(".hoverBtn").css("display", "block");
        for ( var i = 6; i < count; i++) {
            $("#sys" + i).css("display", "none");
        }

        $("#lastBtn").hover(function() {
            if (start != 0) {
                $(this).toggleClass("hoverBtnTog");
            }
        });
        $("#nextBtn").hover(function() {
            if (count - start != 6) {
                $(this).toggleClass("hoverBtnTog");
            }
        });
    
        //滑鼠滾輪事件
        var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;
        //Firefox事件:DOMMouseScroll、IE/Opera/Chrome事件:mousewheel
        var mousewheel = isFirefox ? "DOMMouseScroll" : "mousewheel";
        
        //滑鼠滾動事件
        var scrollFunc = function(e) {
            e = e || window.event;
            
            //當滑鼠在子系統地區內時:屏蔽視窗滾輪-滾動捲軸事件;
            //此時才調用子系統滾動方法
            if(isUp){
                e = e || window.event;
                if (e.stopPropagation) e.stopPropagation();
                else e.cancelBubble = true;
                if (e.preventDefault) e.preventDefault();
                else e.returnValue = false;
                
                if(isFirefox){
                    if (e.detail == -3) {
                        // 向上滾動
                        getLast();
                    } else {
                        // 向下滾動
                        getNext();
                    }
                }else{
                    if (e.wheelDelta == 120) {
                        // 向上滾動
                        getLast();
                    } else {
                        // 向上滾動
                        getNext();
                    }
                }
            }
        };
        
        document.addEventListener(mousewheel, scrollFunc, false);
        
        $("#hor").mouseover(function(){
            isUp = true;
        });
        $("#hor").mouseleave(function(){
            isUp = false;
        });
    }
        
});

js滑鼠滾輪上下滾動監聽事件應用執行個體(跨瀏覽器,親測)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.