Skip to content

Instantly share code, notes, and snippets.

@BlaM
Created May 28, 2018 10:23
Show Gist options
  • Save BlaM/1e43ec3f0766d0ef6df24b75b8c7cfe0 to your computer and use it in GitHub Desktop.
Save BlaM/1e43ec3f0766d0ef6df24b75b8c7cfe0 to your computer and use it in GitHub Desktop.
Detect if a DOM node is hidden by "overflow: hidden" on a parent node
function hiddenByOverflow(node) {
var parent = node.parentNode;
var visible = true;
var nodeRect = node.getBoundingClientRect();
var parentRect = null, parentStyle = null, errcount = 0;;
while (parent && visible && errcount < 10) {
try {
parentRect = parent.getBoundingClientRect();
parentStyle = getComputedStyle(parent);
if (parentStyle['overflow-x'] === 'hidden') {
visible = visible && (nodeRect.x + nodeRect.width >= parentRect.x) && (nodeRect.x <= parentRect.x + parentRect.width);
}
if (parentStyle['overflow-y'] === 'hidden') {
visible = visible && (nodeRect.y + nodeRect.height >= parentRect.y) && (nodeRect.y <= parentRect.y + parentRect.height);
}
} catch (x) {
errcount++;
}
parent = parent.parentNode;
}
return !visible;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment