30

Please refer the below code.

$(this.element).on("mousewheel", this.chartMouseWheel);

chartMouseWheel:function(e) {
        if(e.originalEvent.wheelDelta /120 > 0) {
            alert('scrolling up !');
        }
        else{
          alert('scrolling down !');
        }

        if (e.preventDefault)
        e.preventDefault();
        e.returnValue = false;
    },

this event triggering properly in IE,chrome and not triggering in Firefox ?

4

6 回答 6

46

这是 2017 年,现在正确答案是:

$(window).on('wheel', function(event){

    // deltaY obviously records vertical scroll, deltaX and deltaZ exist too
    if(event.originalEvent.deltaY < 0){
        // wheeled up
    }
    else {
        // wheeled down
    }
});

适用于当前的 Firefox 51、Chrome 56、IE9+

注意:增量值取决于浏览器和用户设置。

于 2017-02-05T23:19:25.310 回答
26

Firefox doesn't recognize "mousewheel" as of version 3. You should use "DOMMouseScroll" instead for firefox.

check this: http://www.javascriptkit.com/javatutors/onmousewheel.shtml

于 2013-05-28T10:20:58.203 回答
18

使用wheel事件。此页面还为旧浏览器提供了 polyfills https://developer.mozilla.org/en-US/docs/Web/Events/wheel

于 2015-03-04T07:42:47.257 回答
14

Badri 是对的,您应该使用“DOMMouseScroll”代替 firefox。除此之外,对于 delta,您需要使用 event.originalEvent.detail 而不是 event.originalEvent.wheelDelta。对于 down,event.originalEvent.detail 给出正值,而 event.originalEvent.wheelDelta 给出负值,反之亦然。

 $(stage.content).on('mousewheel DOMMouseScroll', zoomHelper.zoom);   

 if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
            if (event.originalEvent.detail > 0) {
                //scroll down
                delta = 0.2;
            } else {
                //scroll up
                delta = 0;
            }
        } else {
            if (event.originalEvent.wheelDelta < 0) {
                //scroll down
                delta = 0.2;
            } else {
                //scroll up
                delta = 0;
            }
        }

JSFiddle(适用于 IE 11、Firefox 33 和 Chrome 38,我没有测试其他浏览器):http: //jsfiddle.net/rpaul/ckwu7u86/3/

于 2014-11-20T01:27:19.387 回答
3

这似乎适用于 Safari、Chrome 和 Firefox(我没有在 IE 中测试过):

// For Chrome
window.addEventListener('mousewheel', mouseWheelEvent);

// For Firefox
window.addEventListener('DOMMouseScroll', mouseWheelEvent);

function mouseWheelEvent(e) {
  var delta = e.wheelDelta ? e.wheelDelta : -e.detail;
  console.log(delta);
}

来自: http: //www.h3xed.com/programming/javascript-mouse-scroll-wheel-events-in-firefox-and-chrome

于 2016-02-02T23:59:41.457 回答
2

或者只是使用jquery-mousewheel jQuery 插件。

于 2013-05-28T12:11:11.170 回答