【问题标题】:Is it possible to disable only mouse wheel scrolling to the right using jQuery是否可以使用 jQuery 仅禁用鼠标滚轮向右滚动
【发布时间】:2015-03-01 03:42:43
【问题描述】:

大家好,我想知道是否可以使用 jQuery 禁用垂直鼠标滚轮。我不能使用 CSS,因为我有需要可见的溢出

$("#ab").bind("mousewheel", function() { //only effect scrolling to the right??
    return false;
});

谢谢

【问题讨论】:

    标签: jquery html scroll


    【解决方案1】:

    可能重复?直接从这里复制:Disabling vertical scroll by mouse

    $('#ab')
      .bind('mousewheel DOMMouseScroll', function(e) {
        e.preventDefault();
    });
    

    或者这里的另一个:How to do a horizontal scroll on mouse wheel scroll?

    var mouseWheelEvt = function (event) {
      if (document.body.doScroll)
        document.body.doScroll(event.wheelDelta>0?"left":"right");
      else if ((event.wheelDelta || event.detail) > 0)
        document.body.scrollLeft -= 10;
      else
        document.body.scrollLeft += 10;
    
      return false;
    }
    document.body.addEventListener("mousewheel", mouseWheelEvt);
    

    更新:我认为这正是您正在寻找的代码:

    $('#ab').bind('mousewheel DOMMouseScroll', function(e) {
      // Replace with != 0 if you want to prevent left/right instead.
      if (e.originalEvent.wheelDeltaX == 0) {
        e.preventDefault();
      }
    });
    

    例如:http://jsfiddle.net/32w276xL/

    【讨论】:

    • 不,我试图仅在一个方向(向右)禁用鼠标滚轮,而我在问题中包含的脚本会在所有方向上禁用鼠标滚轮
    • 试试更新的代码,我想这就是你要找的?
    【解决方案2】:

    你可以这样试试....

    HTML--

    <div id='no_scroll'></div>
    

    CSS-

    body {
        height: 2000px;
    }
    #no_scroll{
        width: 50px;
        height: 1500px;
        background: blue;
    }
    

    jQuery--

    $(document).ready(function(){
        $('body').on({
            'mousewheel': function(e) {
                if (e.target.id == 'no_scroll') return;
                e.preventDefault();
                e.stopPropagation();
            }
        }) 
    });
    

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多