Skip to content

Instantly share code, notes, and snippets.

@johnloy
Forked from gre/scrollparent.js
Created June 10, 2024 17:57
Show Gist options
  • Save johnloy/e0de7d6a33608bb4d92bb17839fdcc5d to your computer and use it in GitHub Desktop.
Save johnloy/e0de7d6a33608bb4d92bb17839fdcc5d 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment