Created
January 5, 2018 03:41
-
-
Save twxia/bb20843c495a49644be6ea3804c0d775 to your computer and use it in GitHub Desktop.
Get Scrollable Parent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sometimes scrolling element is not body, so document.scrollingElement
works for me. Caniuse
return getScrollParent(node.parentNode) || document.scrollingElement || document.body;
Hi everyone, a version supporting shadow dom
https://gist.github.com/oscarmarina/3a546cff4d106a49a5be417e238d9558
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
謝謝威廉老師~ 我們公司也在用XD 讚讚