如何使MouseWheel事件在jQuery中仅触发一次?

rae*_*aeq 5 javascript jquery events mouseevent mousewheel

因此,我只希望用户每次通过鼠标滚轮向上或向下滚动时才触发一次功能。请参阅:jsFiddle演示。问题是,即使我拥有e.preventDefault(),该函数仍会多次触发。

目的是使该功能仅在用户向上或向下滚动时才触发一次。类似于这个网站

这是我到目前为止的代码:

var sq = {};
sq = document;
if (sq.addEventListener) {
    sq.addEventListener("mousewheel", MouseWheelHandler(), false);
    sq.addEventListener("DOMMouseScroll", MouseWheelHandler(), false);
} else {
    sq.attachEvent("onmousewheel", MouseWheelHandler());
}

function MouseWheelHandler() {
    return function (e) {
        var e = window.event || e;
        var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
        if (delta < 0) {
            /* Scroll Down */
            e.preventDefault();
            console.log("Down. I want this to happen only once");

        } else {
            /* Scroll Up */
            console.log("up. I want this to happen only once");
            e.preventDefault();
        }
        return false;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

ami*_*min -3

   \xe2\x80\x8b$(document).ready(function(){\nvar up=false;\nvar down=false;\n        $(\'#foo\').bind(\'mousewheel\', function(e){\n            if(e.originalEvent.wheelDelta /120 > 0 && up=false)  {\nup=true;\ndown=false;\n                $(this).text(\'scrolling up !\');\n            }\n            elseif(e.originalEvent.wheelDelta /120 > 0 && down=false){\ndown=true;\nup=false;\n                $(this).text(\'scrolling down !\');\n            }\n        });\n    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

一个插件,您可以使用它

\n