函数节流scroll,兼容火狐滚轮事件

    //函数节流
    var wheelTimeout;
    var wheelFun = function (func) {
        if (wheelTimeout) {
            return;
        }
        func();
        wheelTimeout = setTimeout(function () {
            wheelTimeout = 0;
        }, 1000);
    };

//如果是火狐浏览器就mousewheelevt=DOMMouseScroll;否则mousewheelevt=mousewheel,因为火狐绑定滚轮事件是DOMMouseScroll

var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";

    function mousewheelEvent(e, delta) {
        //console.log("delta==", delta);
        wheelFun(function () {
            mosewheelFun(e, delta);
        });
    }

    //判断是否是IE,IE的监听
    if (document.attachEvent) {
        document.attachEvent("on" + mousewheelevt, function (e) {
            mousewheelEvent(e, e.wheelDelta);
        });
    } else if (document.addEventListener) {
        //FireFox(DOMMouseScroll)、Chrome、Opera、safari
        document.addEventListener(mousewheelevt, function (e) {
            e = e || window.event;
            if (e.detail) {
                //Firefox
                mousewheelEvent(e, e.detail * 40);
            } else if (e.wheelDelta) {
                mousewheelEvent(e, -e.wheelDelta);
            }
        }, false);
    }

 

posted on 2018-01-04 21:27  小碎步  阅读(847)  评论(0编辑  收藏  举报