Get mouse wheel events in jQuery?

前端 未结 14 1215
旧巷少年郎
旧巷少年郎 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:48

    my combination looks like this. it fades out and fades in on each scroll down/up. otherwise you have to scroll up to the header, for fading the header in.

    var header = $("#header");
    $('#content-container').bind('mousewheel', function(e){
        if(e.originalEvent.wheelDelta > 0) {
            if (header.data('faded')) {
                header.data('faded', 0).stop(true).fadeTo(800, 1);
            }
        }
        else{
            if (!header.data('faded')) header.data('faded', 1).stop(true).fadeTo(800, 0);
        }
    });
    

    the above one is not optimized for touch/mobile, I think this one does it better for all mobile:

    var iScrollPos = 0;
    var header = $("#header");
    $('#content-container').scroll(function () {
    
        var iCurScrollPos = $(this).scrollTop();
        if (iCurScrollPos > iScrollPos) {
            if (!header.data('faded')) header.data('faded', 1).stop(true).fadeTo(800, 0);
    
        } else {
            //Scrolling Up
            if (header.data('faded')) {
                header.data('faded', 0).stop(true).fadeTo(800, 1);
            }
        }
        iScrollPos = iCurScrollPos;
    
    });
    
    0 讨论(0)
  • 2020-11-22 13:50

    As of now in 2017, you can just write

    $(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
      }
    });
    

    Works with current Firefox 51, Chrome 56, IE9+

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