Skip to content

Instantly share code, notes, and snippets.

@apostololeg
Last active January 8, 2020 15:02
Show Gist options
  • Save apostololeg/03f8b4021a92cb3df98a94b6bf2bdc28 to your computer and use it in GitHub Desktop.
Save apostololeg/03f8b4021a92cb3df98a94b6bf2bdc28 to your computer and use it in GitHub Desktop.
const METRICS_BY_AXIS = {
horizontal: {
size: 'clientWidth',
scroll: 'scrollWidth'
},
vertical: {
size: 'clientHeigh',
scroll: 'scrollHeigh'
}
};
function isScrollableWithTresh(node, axis, threshold = 5) {
const m = METRICS_BY_AXIS[axis];
return node[m.scroll] - node[m.width] > threshold;
}
function isScrollable(node, axis) {
if (!axis)
return (
isScrollableWithTresh(node, 'vertical') ||
isScrollableWithTresh(node, 'horizontal')
);
return isScrollableWithTresh(node, axis);
}
export function getScrollParent(node, axis) {
if (node == null) return null;
if (node.tagName === 'BODY' || isScrollable(node, axis)) return node;
return getScrollParent(node.parentNode, axis);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment