JavaScript 鼠标滚轮事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 鼠标滚轮事件相关的知识,希望对你有一定的参考价值。

/*
 * Author URL: http://adomas.org/javascript-mouse-wheel/
 */

/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {
        if (delta < 0)
		…;
        else
		…;
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

以上是关于JavaScript 鼠标滚轮事件的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript鼠标滚轮事件

恢复鼠标滚轮事件javascript库

如何使用 JavaScript 在 Firefox 中触发鼠标滚轮事件?

javascript 鼠标滚轮事件

javascript 鼠标滚轮事件

JavaScript 鼠标滚轮事件