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
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.
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.
bind('mousewheel DOMMouseScroll', function() { return false; }); $(this).
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With