【问题标题】:Prevent page scrolling while scrolling a div element滚动 div 元素时防止页面滚动
【发布时间】:2015-11-12 13:25:30
【问题描述】:

我已经在此处接受的答案中找到了解决方案:

How to prevent page scrolling when scrolling a DIV element?

但还想禁用按键上的主页滚动(当 div 内容不能再滚动时)。

我正在尝试制作这样的东西,但它不起作用:

$( '.div-scroll' ).bind( 'keydown mousewheel DOMMouseScroll', function ( e ) {
                var e0 = e.originalEvent,
                delta = e0.wheelDelta || -e0.detail;

                this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;

                if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
                    e.preventDefault();
                }
                e.preventDefault();
            });

任何想法为什么?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    您可以通过以下方式停止整个页面的滚动:

    方法一

    <div  onmouseover="document.body.style.overflow='hidden';"  onmouseout="document.body.style.overflow='auto';"></div>
    

    但当您将鼠标悬停在 div 上时,它会使浏览器的滚动条消失。

    方法二

    否则你可以看看 jquery-mousewheel。

    var toolbox = $('#toolbox'),
        height = toolbox.height(),
        scrollHeight = toolbox.get(0).scrollHeight;
    
    toolbox.off("mousewheel").on("mousewheel", function (event) {
      var blockScrolling = this.scrollTop === scrollHeight - height && event.deltaY < 0 || this.scrollTop === 0 && event.deltaY > 0;
      return !blockScrolling;
    });
    

    DEMO

    方法3

    在没有插件的情况下停止传播。

    HTML

    <div class="Scrollable">
      <!-- A bunch of HTML here which will create scrolling -->
    </div>
    

    JS

    $('.Scrollable').on('DOMMouseScroll mousewheel', function(ev) {
        var $this = $(this),
            scrollTop = this.scrollTop,
            scrollHeight = this.scrollHeight,
            height = $this.height(),
            delta = (ev.type == 'DOMMouseScroll' ?
                ev.originalEvent.detail * -40 :
                ev.originalEvent.wheelDelta),
            up = delta > 0;
    
        var prevent = function() {
            ev.stopPropagation();
            ev.preventDefault();
            ev.returnValue = false;
            return false;
        }
    
        if (!up && -delta > scrollHeight - height - scrollTop) {
            // Scrolling down, but this will take us past the bottom.
            $this.scrollTop(scrollHeight);
            return prevent();
        } else if (up && delta > scrollTop) {
            // Scrolling up, but this will take us past the top.
            $this.scrollTop(0);
            return prevent();
        }
    });
    

    方法四

    您可以通过取消这些交互事件来做到这一点:

    鼠标和触摸滚动以及与滚动相关的按钮。

    // left: 37, up: 38, right: 39, down: 40,
    // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
    var keys = {37: 1, 38: 1, 39: 1, 40: 1};
    
    function preventDefault(e) {
      e = e || window.event;
      if (e.preventDefault)
          e.preventDefault();
      e.returnValue = false;  
    }
    
    function preventDefaultForScrollKeys(e) {
        if (keys[e.keyCode]) {
            preventDefault(e);
            return false;
        }
    }
    
    function disableScroll() {
      if (window.addEventListener) // older FF
          window.addEventListener('DOMMouseScroll', preventDefault, false);
      window.onwheel = preventDefault; // modern standard
      window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
      window.ontouchmove  = preventDefault; // mobile
      document.onkeydown  = preventDefaultForScrollKeys;
    }
    
    function enableScroll() {
        if (window.removeEventListener)
            window.removeEventListener('DOMMouseScroll', preventDefault, false);
        window.onmousewheel = document.onmousewheel = null; 
        window.onwheel = null; 
        window.ontouchmove = null;  
        document.onkeydown = null;  
    }
    

    【讨论】:

    • 是的,我知道如何防止键滚动,但不知道如何将其合并到我提供的代码中,即只有 .div-scroll 元素可以使用箭头滚动而不是整个页面我在 .div-scroll 元素的底部/顶部并点击向上/向下箭头。
    • 当使用上述方法4时,当可滚动列表到达底部时,主/容器div再次开始滚动,这看起来很奇怪,考虑到应该还没有触发mouseout .
    【解决方案2】:

    您需要像这样将文档绑定到“keydown”事件:

    $( document ).bind( 'keydown', function (e) { ... e.preventDefault(); }
    

    【讨论】:

    • 是的,我知道如何防止键滚动,但不知道如何将其合并到我提供的代码中,即只有 .div-scroll 元素可以使用箭头滚动而不是整个页面我在 .div-scroll 元素的底部/顶部并点击向上/向下箭头。
    【解决方案3】:

    此代码使用键阻止滚动:

    $(document).keydown(function(e) {
            if (e.keyCode === 32 || e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40) {
                return false;
            }
    });
    

    【讨论】:

    • 是的,我知道如何防止键滚动,但不知道如何将其合并到我提供的代码中,即只有 .div-scroll 元素可以使用箭头滚动而不是整个页面我在 .div-scroll 元素的底部/顶部并点击向上/向下箭头。
    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多