Scrolling child div scrolls the window, how do I stop that?

后端 未结 14 734
生来不讨喜
生来不讨喜 2020-11-30 18:03

I have a div, with a scroll bar, When it reaches the end, my page starts scrolling. Is there anyway I can stop this behavior ?

相关标签:
14条回答
  • 2020-11-30 18:59

    As answered here, most modern browsers now support the overscroll-behavior: none; CSS property, that prevents scroll chaining. And that's it, just one line!

    0 讨论(0)
  • 2020-11-30 19:00

    Here's a cross-browser way to do this on the Y axis, it works on desktop and mobile. Tested on OSX and iOS.

    var scrollArea = this.querySelector(".scroll-area");
    scrollArea.addEventListener("wheel", function() {
        var scrollTop = this.scrollTop;
        var maxScroll = this.scrollHeight - this.offsetHeight;
        var deltaY = event.deltaY;
        if ( (scrollTop >= maxScroll && deltaY > 0) || (scrollTop === 0 && deltaY < 0) ) {
            event.preventDefault();
        }
    }, {passive:false});
    
    scrollArea.addEventListener("touchstart", function(event) {
        this.previousClientY = event.touches[0].clientY;
    }, {passive:false});
    
    scrollArea.addEventListener("touchmove", function(event) {
        var scrollTop = this.scrollTop;
        var maxScroll = this.scrollHeight - this.offsetHeight;
        var currentClientY = event.touches[0].clientY;
        var deltaY = this.previousClientY - currentClientY;
        if ( (scrollTop >= maxScroll && deltaY > 0) || (scrollTop === 0 && deltaY < 0) ) {
            event.preventDefault();
        }
        this.previousClientY = currentClientY;
    }, {passive:false});
    
    0 讨论(0)
  • 2020-11-30 19:02

    You could use a mouseover event on the div to disable the body scrollbar and then a mouseout event to activate it again?

    E.g. The HTML

    <div onmouseover="disableBodyScroll();" onmouseout="enableBodyScroll();">
        content
    </div>
    

    And then the javascript like so:

    var body = document.getElementsByTagName('body')[0];
    function disableBodyScroll() {
        body.style.overflowY = 'hidden';
    }
    function enableBodyScroll() {
        body.style.overflowY = 'auto';
    }
    
    0 讨论(0)
  • 2020-11-30 19:03

    I couldn't get any of the answers to work in Chrome and Firefox, so I came up with this amalgamation:

    $someElement.on('mousewheel DOMMouseScroll', scrollProtection);
    
    function scrollProtection(event) {
        var $this = $(this);
        event = event.originalEvent;
        var direction = (event.wheelDelta * -1) || (event.detail);
        if (direction < 0) {
            if ($this.scrollTop() <= 0) {
                return false;
            }
        } else {
            if ($this.scrollTop() + $this.innerHeight() >= $this[0].scrollHeight) {
                return false;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 19:04

    You can inactivate the scrolling of the whole page by doing something like this:

    <div onmouseover="document.body.style.overflow='hidden';" onmouseout="document.body.style.overflow='auto';"></div>
    
    0 讨论(0)
  • 2020-11-30 19:05

    this disables the scrolling on the window if you enter the selector element. works like charms.

    elements = $(".selector");
    
    elements.on('mouseenter', function() {
        window.currentScrollTop = $(window).scrollTop();
        window.currentScrollLeft = $(window).scrollTop();
        $(window).on("scroll.prevent", function() {
            $(window).scrollTop(window.currentScrollTop);
            $(window).scrollLeft(window.currentScrollLeft);
        });
    });
    
    elements.on('mouseleave', function() {
        $(window).off("scroll.prevent");
    });
    
    0 讨论(0)
提交回复
热议问题