Skip to content

Instantly share code, notes, and snippets.

@berkin
Created November 18, 2016 07:41
Show Gist options
  • Save berkin/8b89a6227250eb171dec39f31e307b63 to your computer and use it in GitHub Desktop.
Save berkin/8b89a6227250eb171dec39f31e307b63 to your computer and use it in GitHub Desktop.
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