Skip to content

Instantly share code, notes, and snippets.

@gre
Created August 3, 2016 11:27
Show Gist options
  • Save gre/296291b8ce0d8fe6e1c3ea4f1d1c5c3b to your computer and use it in GitHub Desktop.
Save gre/296291b8ce0d8fe6e1c3ea4f1d1c5c3b to your computer and use it in GitHub Desktop.
get first parent scrollable container of a dom element
// more minimal version of https://github.com/olahol/scrollparent.js/blob/master/scrollparent.js
const regex = /(auto|scroll)/;
const style = (node, prop) =>
getComputedStyle(node, null).getPropertyValue(prop);
const scroll = (node) =>
regex.test(
style(node, "overflow") +
style(node, "overflow-y") +
style(node, "overflow-x"));
const scrollparent = (node) =>
!node || node===document.body
? document.body
: scroll(node)
? node
: scrollparent(node.parentNode);
export default scrollparent;
@basvanmeurs
Copy link

I think that node.parentNode on line 18 should be node.parentElement, as getComputedStyle expects an Element rather than a Node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment