Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent scrolling of parent container when scrolling in overflow:scroll container has reached its limits

Simply put, i guess you all know the issue: you use your mousewheel (or trackpad gesture) to scroll inside a div that is set to overflow:scroll (or a setting to that effect). The moment you reach the end of the scrollable area, the scroll 'commands' are immediately sent to the parent container - the main window for instance.

This can be quite annoying and i wonder if there is a way to prevent it.

I created this jsFiddle to demonstrate the issue and provide a ground for experimentation.

The only idea that came to my mind was using preventDefault but since i am not a JS wizard, i don't see where or how i could apply that correctly.

like image 611
SquareCat Avatar asked Mar 09 '14 20:03

SquareCat


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 stop overflow scrolling?

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 you stop scroll behavior?

In this method, a new CSS class is created where the height is set to 100% and the scroll bar is disabled by setting the overflow property to hidden. Whenever scrolling has to be disabled, this class is added to the body using the document. body. classList.


1 Answers

This is not optimal but I bootstrap uses this for their modal windows.

FIDDLE

.overflowHidden {
    overflow:hidden !important;
}

$('#scrollable').hover(function () {
        console.log("hello");
        $('body').addClass('overflowHidden');
    }, function () {
        $('body').removeClass('overflowHidden');
    });
like image 173
Lokesh Suthar Avatar answered Oct 17 '22 11:10

Lokesh Suthar