Created
November 18, 2016 07:41
-
-
Save berkin/8b89a6227250eb171dec39f31e307b63 to your computer and use it in GitHub Desktop.
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 regex = /(auto|scroll)/; | |
const parents = function (node, ps) { | |
if (node.parentNode === null) { return ps; } | |
return parents(node.parentNode, ps.concat([node])); | |
}; | |
const style = function (node, prop) { | |
return getComputedStyle(node, null).getPropertyValue(prop); | |
}; | |
const overflow = function (node) { | |
return style(node, "overflow") + style(node, "overflow-y") + style(node, "overflow-x"); | |
}; | |
const scroll = function (node) { | |
return regex.test(overflow(node)); | |
}; | |
const scrollParent = function (node) { | |
if (!(node instanceof HTMLElement || node instanceof SVGElement)) { | |
return; | |
} | |
const ps = parents(node.parentNode, []); | |
for (let i = 0; i < ps.length; i += 1) { | |
if (scroll(ps[i])) { | |
return ps[i]; | |
} | |
} | |
return document.body; | |
}; | |
export default scrollParent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment