Is there any way that I can check if an element is visible in pure JS (no jQuery) ?
So, for example, in this page: Performance Bikes, if you hover over Deals (on the
So what I found is the most feasible method:
function visible(elm) {
if(!elm.offsetHeight && !elm.offsetWidth) { return false; }
if(getComputedStyle(elm).visibility === 'hidden') { return false; }
return true;
}
This is build on these facts:
display: none
element (even a nested one) doesn't have a width nor height.visiblity
is hidden
even for nested elements.So no need for testing offsetParent
or looping up in the DOM tree to test which parent has visibility: hidden
. This should work even in IE 9.
You could argue if opacity: 0
and collapsed elements (has a width but no height - or visa versa) is not really visible either. But then again they are not per say hidden.