Prevent parent scroll when in child div

后端 未结 5 1560
野的像风
野的像风 2021-02-08 12:08

When I scroll to the bottom of the child div, the body element starts scrolling.

How can I prevent this? I only want the body to s

5条回答
  •  日久生厌
    2021-02-08 12:53

    If you are not nesting your elements inside other scrolling elements (most cases) you can take this simple high performance approach:

    $(document).ready(function () {
      $('.self-scroll').on('mouseover', function () {
        document.body.style.overflow='hidden';
      });
      $('.self-scroll').on('mouseout', function () {
        document.body.style.overflow='auto'; // or = 'visible'
      });
    });
    

    Now if you apply self-scroll class to any element, it will not scroll body.

提交回复
热议问题