Get mouse wheel events in jQuery?

前端 未结 14 1227
旧巷少年郎
旧巷少年郎 2020-11-22 13:26

Is there a way to get the mouse wheel events (not talking about scroll events) in jQuery?

相关标签:
14条回答
  • 2020-11-22 13:38

    I got same problem recently where $(window).mousewheel was returning undefined

    What I did was $(window).on('mousewheel', function() {});

    Further to process it I am using:

    function (event) {
        var direction = null,
            key;
    
        if (event.type === 'mousewheel') {
            if (yourFunctionForGetMouseWheelDirection(event) > 0) {
                direction = 'up';
            } else {
                direction = 'down';
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:39

    This worked for me:)

     //Firefox
     $('#elem').bind('DOMMouseScroll', function(e){
         if(e.originalEvent.detail > 0) {
             //scroll down
             console.log('Down');
         }else {
             //scroll up
             console.log('Up');
         }
    
         //prevent page fom scrolling
         return false;
     });
    
     //IE, Opera, Safari
     $('#elem').bind('mousewheel', function(e){
         if(e.originalEvent.wheelDelta < 0) {
             //scroll down
             console.log('Down');
         }else {
             //scroll up
             console.log('Up');
         }
    
         //prevent page fom scrolling
         return false;
     });
    

    from stackoverflow

    0 讨论(0)
  • 2020-11-22 13:43
    ​$(document).ready(function(){
        $('#foo').bind('mousewheel', function(e){
            if(e.originalEvent.wheelDelta /120 > 0) {
                console.log('scrolling up !');
            }
            else{
                console.log('scrolling down !');
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-22 13:45

    This is working in each IE, Firefox and Chrome's latest versions.

    $(document).ready(function(){
            $('#whole').bind('DOMMouseScroll mousewheel', function(e){
                if(e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
                    alert("up");
                }
                else{
                    alert("down");
                }
            });
        });
    
    0 讨论(0)
  • 2020-11-22 13:45

    I was stuck in this issue today and found this code is working fine for me

    $('#content').on('mousewheel', function(event) {
        //console.log(event.deltaX, event.deltaY, event.deltaFactor);
        if(event.deltaY > 0) {
          console.log('scroll up');
        } else {
          console.log('scroll down');
        }
    });
    
    0 讨论(0)
  • 2020-11-22 13:47

    Here is a vanilla solution. Can be used in jQuery if the event passed to the function is event.originalEvent which jQuery makes available as property of the jQuery event. Or if inside the callback function under we add before first line: event = event.originalEvent;.

    This code normalizes the wheel speed/amount and is positive for what would be a forward scroll in a typical mouse, and negative in a backward mouse wheel movement.

    Demo: http://jsfiddle.net/BXhzD/

    var wheel = document.getElementById('wheel');
    
    function report(ammout) {
        wheel.innerHTML = 'wheel ammout: ' + ammout;
    }
    
    function callback(event) {
        var normalized;
        if (event.wheelDelta) {
            normalized = (event.wheelDelta % 120 - 0) == -0 ? event.wheelDelta / 120 : event.wheelDelta / 12;
        } else {
            var rawAmmount = event.deltaY ? event.deltaY : event.detail;
            normalized = -(rawAmmount % 3 ? rawAmmount * 10 : rawAmmount / 3);
        }
        report(normalized);
    }
    
    var event = 'onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll';
    window.addEventListener(event, callback);
    

    There is also a plugin for jQuery, which is more verbose in the code and some extra sugar: https://github.com/brandonaaron/jquery-mousewheel

    0 讨论(0)
提交回复
热议问题