I'm working on a popover with scrollable content (similar to Facebook's notification popover).
Currently when the popover has focus and the user scrolls its content scrolls and all is well until the bottom is reached and the parent begins scrolling (see example of problem on CodePen).
I am trying to prevent the parent from scrolling when the bottom of the child is reached and would like to do so with just CSS if possible!
How to prevent child elements from scrolling parent elements. Today we’re going to talk about a nifty CSS property called overscroll-behavior. The overscroll-behavior property gives you control over the scroll behavior between the child and parent elements.
ScrollStop is a jQuery plugin that has the ability to prevent Body from scrolling when you reach the bottom of a scrollable child element by mouse wheel. How to use it: 1. Place both jQuery library and the jQuery ScrollStop plugin' script at the bottom of the html page.
Thanks to the CSS property: overscroll-behavior. To stop the scroll at the end of an element, set on the element’s CSS: overscroll-behavior: contain; This way, if the user reaches the end of the scroll of an element, it will stop there and not “scroll-chain” to the body or parent element.
This means that if you have an element with a vertical or horizontal overflow and the user has reached the end of the scroll, the browser will start scrolling the parent element (which is mostly the body) - this is called ‘scroll chaining.’ But sometimes, preventing that behavior from happening is useful, e.g.: modals, chat popups, dropdowns.
If you set overflow: hidden
in the body
, that will prevent it from scrolling. Of course when you close the popup, you will want to remove this property.
To add to Sam's answer. Tell it what you want on the fly -- example;
<div class="hover-content"
onmouseover="document.body.style.overflow='hidden';"
onmouseout="document.body.style.overflow='auto';">
blah yay I'm fixed
</div>
Hope this helps, cheers
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