Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first scrollable parent

I have this situation in which I need to scroll an element into the viewport. The problem is that I don't know which element is scrollable. For example, in Portrait the body is scrollable and in Landscape its an other element (and there are more situation which change the scrollable element)

Now the question, given an element which needs to be scrolled into the viewport, what is the best way to find its first scrollable parent ?

I've setup a demo here. With the button you can toggle between two different situations

<div class="outer">     <div class="inner">         <div class="content">              ...             <span>Scroll me into view</span>         </div>     </div> </div> 

The body is scrollable or .outer

Any suggestions ?

like image 928
Jeanluca Scaljeri Avatar asked Mar 11 '16 12:03

Jeanluca Scaljeri


People also ask

How do I find the scrolling element?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

What is scrollTop and scrollHeight?

In summary, scrollTop is how much it's currently scrolled, and scrollHeight is the total height, including content scrolled out of view.

How do I check if a div is on a screen?

Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.


2 Answers

Just check if the scrollbar is visible, if not look to the parent.

function getScrollParent(node) {   if (node == null) {     return null;   }    if (node.scrollHeight > node.clientHeight) {     return node;   } else {     return getScrollParent(node.parentNode);   } } 
like image 99
Stefano Nardo Avatar answered Oct 25 '22 02:10

Stefano Nardo


This is a pure JS port of the jQuery UI scrollParent method that cweston spoke of. I went with this rather than the accepted answer's solution which will not find the scroll parent if there's no content overflow yet.

The one difference with my port is that, if no parent is found with the right value for the CSS overflow property, I return the <body> element. JQuery UI, instead returned the document object. This is odd as values like .scrollTop can be retrieved from the <body> but not the document.

function getScrollParent(element, includeHidden) {     var style = getComputedStyle(element);     var excludeStaticParent = style.position === "absolute";     var overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;      if (style.position === "fixed") return document.body;     for (var parent = element; (parent = parent.parentElement);) {         style = getComputedStyle(parent);         if (excludeStaticParent && style.position === "static") {             continue;         }         if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;     }      return document.body; } 
like image 22
Web_Designer Avatar answered Oct 25 '22 02:10

Web_Designer