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

后端 未结 14 754
生来不讨喜
生来不讨喜 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:54

    I wrote resolving for this issue

      var div;
      div = document.getElementsByClassName('selector')[0];
    
      div.addEventListener('mousewheel', function(e) {
        if (div.clientHeight + div.scrollTop + e.deltaY >= div.scrollHeight) {
          e.preventDefault();
          div.scrollTop = div.scrollHeight;
        } else if (div.scrollTop + e.deltaY <= 0) {
          e.preventDefault();
          div.scrollTop = 0;
        }
      }, false);
    

提交回复
热议问题