Last active
March 18, 2024 14:40
-
-
Save wojtekmaj/fe811af47fad12a7265b6f7df1017c83 to your computer and use it in GitHub Desktop.
Find the nearest scrollable container.
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
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