Wednesday 4 May 2011

How to check whether an element is visible or not using jQuery

jQuery(':visible') - Selects all elements that are visible.

Elements can be considered hidden for several reasons:

* They have a CSS display value of none.
* They are form elements with type="hidden".
* Their width and height are explicitly set to 0.
* An ancestor element is hidden, so the element is not shown on the page.

Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation. During animations to show an element, the element is considered to be visible at the start at the animation.

Below JavaScript is useful if you need to know if a specific object is visible:

if ( $('#elementid').is(':visible')){
// element is visible, do something
}

OR

if ($('#elementid:visible').length > 0){
// element is visible, do something
}

No comments:

Post a Comment