Last active
January 8, 2020 15:02
-
-
Save apostololeg/03f8b4021a92cb3df98a94b6bf2bdc28 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 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