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

后端 未结 14 751
生来不讨喜
生来不讨喜 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 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;
            }
        }
    }
    

提交回复
热议问题