Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if an element is not visible in a fast way in JavaScript?

In the past we used the CSS attribute "display" to show and hide DOM elements. To check if an element is visible, we could just use:

element.offsetWidth > 0

Since we had some problems with Flash and Java Applets (they stop when they get display:none) we switched to the CSS attribute "visibility".

I am now looking for a fast and easy way to check if an element is not visible.

I have tried the following:

  • Checking the attribute itself on the element and and all parents => too slow
  • Checking the calculated style directly from the browser (element.currentStyle or window.getComputedStyle() plus getPropertyValue(style property)) => also too slow

Do you know any other way or shortcut to see if an element is visible?

like image 969
Jonathan Weiss Avatar asked Aug 12 '09 09:08

Jonathan Weiss


1 Answers

use JQuery and the you can do this

var isVisible = $('#foo').is(':visible'); 
like image 112
RoutineOp Avatar answered Oct 13 '22 19:10

RoutineOp