Checking element visibility in jQuery

What is the best way to determine if an item is actually displayed on the page? How do they change in pixels, because the element is not hidden through CSS, but is in the visible part of the scrollable area (window or some kind of overflowing block element)?

I assume that I need to check .is (': hidden') first against the element and its parents. Then I would need to iterate over all the parents and the window, checking the scroll overflow / auto / hidden, then compare the position and scroll size of the parent element with the original position and size of the element. And I will also need to check the absolute positioning and look at the z-indices.

Is there an easier way?

+6
jquery visibility
source share
2 answers

There is a plugin. jQuery inview event plugin

+2
source share

I have implemented $. inside a plugin in which you can specify ancestor , in your case:

 $('html').css('height', '100%'); // make `html` the height of the viewport $('#your-element').inside('html'); // compare `#your-element` to `html` 

will return for example:

 { left: 0.2, // your element is "x-inside" (because >0 and <1) top: -2.3 // but is not "y-inside" (because <0) } 

Read more in README.

In your case, you can add an extra check for is(':hidden') and z-index

Hope this helps.

0
source share

All Articles