Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent parent scroll when in child div

Tags:

jquery

css

scroll

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 scroll when the cursor is over it.

Example: JsFiddle

like image 908
user3675747 Avatar asked Aug 04 '14 18:08

user3675747


People also ask

How do I stop parent element from scrolling?

To do this (in Chrome, Firefox, and Edge), we can add the CSS property overscroll-behavior: contain to the overflow: auto element. This will prevent the "scroll chaining" behavior, which will, in turn, keep the mouse-wheel active within the target element.

How do I disable parent scroll in CSS?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do I stop scrolling when scrolling a div element CSS?

bind('mousewheel DOMMouseScroll', function() { return false; }); $(this).


1 Answers

Accepted answer seems outdated. The "mousewheel" event is a non-standard feature. The "wheel" event seems to be the standard now. Also wheelDelta isn't defined in most browsers. Change to -event.deltaY seems to do the trick. Works in IE 10, Chrome and Firefox

$(".scroll-div").on("wheel", function ( e ) {
            var event = e.originalEvent,
                d = -event.deltaY || -event.detail ;

            this.scrollTop += ( d < 0 ? 1 : -1 ) * 30;
            e.preventDefault();
    });
like image 190
2748 Avatar answered Oct 02 '22 19:10

2748