Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent scrolling in a child from scrolling its parent [duplicate]

Tags:

html

css

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!

like image 814
Warpling Avatar asked Apr 15 '14 18:04

Warpling


People also ask

How to prevent child elements from scrolling parent elements in CSS?

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.

How to prevent body from scrolling when reaching bottom of page?

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.

How do I stop scroll at the end of an element?

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.

What is scroll chaining and how do I prevent it?

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.


2 Answers

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.

like image 156
Sam G-H Avatar answered Oct 13 '22 19:10

Sam G-H


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

like image 25
Chris W. Avatar answered Oct 13 '22 19:10

Chris W.