Skip to content

Instantly share code, notes, and snippets.

@wojtekmaj
Last active March 18, 2024 14:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wojtekmaj/fe811af47fad12a7265b6f7df1017c83 to your computer and use it in GitHub Desktop.
Save wojtekmaj/fe811af47fad12a7265b6f7df1017c83 to your computer and use it in GitHub Desktop.
Find the nearest scrollable container.
const findScrollContainer = (element) => {
if (!element) {
return undefined;
}
let parent = element.parentElement;
while (parent) {
const { overflow } = window.getComputedStyle(parent);
if (overflow.split(' ').every(o => o === 'auto' || o === 'scroll')) {
return parent;
}
parent = parent.parentElement;
}
return document.documentElement;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment