Skip to content

Instantly share code, notes, and snippets.

@antoni
Forked from twxia/getScrollableParent.js
Created June 8, 2022 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoni/477a3db292ee55c310e71b60486e78b2 to your computer and use it in GitHub Desktop.
Save antoni/477a3db292ee55c310e71b60486e78b2 to your computer and use it in GitHub Desktop.
Get Scrollable Parent
function getScrollParent(node) {
const isElement = node instanceof HTMLElement;
const overflowY = isElement && window.getComputedStyle(node).overflowY;
const isScrollable = overflowY !== 'visible' && overflowY !== 'hidden';
if (!node) {
return null;
} else if (isScrollable && node.scrollHeight >= node.clientHeight) {
return node;
}
return getScrollParent(node.parentNode) || document.body;
}
export default getScrollParent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment