Problems with mousewheel in jQuery

后端 未结 4 1565
北海茫月
北海茫月 2021-01-13 18:29

I have the following code in the head-section:



jQuery(function($         


        
相关标签:
4条回答
  • 2021-01-13 18:37

    Works fine with jQuery 1.6.x, but does NOT work with jQuery 1.7.x
    Seems like something broke in jQuery.

    0 讨论(0)
  • 2021-01-13 18:48

    It means that delta is undefined which probably means you didn't include the proper plugin, or included it in the wrong place.

    Don't know what you download, I've tried now with this and it's working fine.

    If you remove the external resource, it won't work as happens in your case.

    0 讨论(0)
  • 2021-01-13 18:52

    Not working either with new jQuery 2.0.1... well, it works but not as desired. This code only detect down movement (that means negative delta). I have tried this code and the result is always negative:

    var num = 0;
    
    $('#container').bind('mousewheel DOMMouseScroll',function(e, delta) {
        if(delta > 0) {
            $("#output").text(++num);
        } else {
            $("#output").text(--num);
        }
    
        e.stopPropagation();
        e.preventDefault();
    });
    

    The DOMMouseScroll is the same as mousewheel but for Firefox.

    CORRECTION

    I have found this new code in this post and now it's working in Chrome and Firefox (I have fixed it) with jQuery 2.0.1. The problem was that the delta value returned was undefined.

    function extractDelta(e) {
        if (e.wheelDelta) {
            return e.wheelDelta;
        }
    
        if (e.originalEvent.detail) {
            return e.originalEvent.detail * -40;
        }
    
        if (e.originalEvent && e.originalEvent.wheelDelta) {
            return e.originalEvent.wheelDelta;
        }
    }
    
    var num = 0;
    
    $('#container').bind('mousewheel DOMMouseScroll',function(e) {
        var d = extractDelta(e);
    
        if(d > 0) {
            $("#output").text(++num);
        } else {
            $("#output").text(--num);
        }
    
        e.stopPropagation();
        e.preventDefault();
    });
    

    Container and output are just two empty divs.

    0 讨论(0)
  • 2021-01-13 18:56

    Here is a cross-browser solution:

    var onMouseWheel = function(e) {
        e = e.originalEvent;
        var delta = e.wheelDelta>0||e.detail<0?1:-1;
        alert(delta);
    }
    $("body").bind("mousewheel DOMMouseScroll", onMouseWheel);
    
    0 讨论(0)
提交回复
热议问题