Skip to content

Instantly share code, notes, and snippets.

@AVVS
Created October 25, 2014 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AVVS/eb7fb6bd518aac2cac4c to your computer and use it in GitHub Desktop.
Save AVVS/eb7fb6bd518aac2cac4c to your computer and use it in GitHub Desktop.
Determine if DOM node is hidden
/**
* Determines if domElement is visible
* @param {Node} domElement - DOM element to assess
* @return {Boolean}
*/
Observer.prototype.isHidden = function (domElement) {
if (!domElement || domElement.offsetParent === null) {
return true;
}
var style = window.getComputedStyle(domElement);
if (style.visibility === 'hidden' || (style.width === '0px' && style.height === '0px')) {
return true;
}
try {
// for some reason nodes may lie about their state, so we have to check parents as well
while ((domElement = domElement.parentNode) && domElement.tagName !== 'BODY') {
style = window.getComputedStyle(domElement);
if (style.visibility === 'hidden') {
return true;
}
}
} catch (e) {
logger.error('Couldn\'t trace parents');
console.error(e);
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment