with jquery, what's the best way to determine if an element is below the fold meaning it's also not in the window and not visible to the user?
Thanks
Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.
To know whether the element is fully visible in viewport, you will need to check whether top >= 0, and bottom is less than the screen height. In a similar way you can also check for partial visibility, top is less than screen height and bottom >= 0.
You can use . offset() to get the offset compared to the document element and then use the scrollTop property of the window element to find how far down the page the user has scrolled: var scrollTop = $(window). scrollTop(), elementOffset = $('#my-element').
Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.
I assume you're referring to whether or not it has been scrolled out of view.
If so, try this:
if( $(window).scrollTop() + $(window).height() > $('#my_el').offset().top ) {
// element is in view
}
Example: http://jsfiddle.net/8x4Zd/
(Click the result panel to test if the targeted element is in view. Then scroll down and repeat.)
If you want to make sure the entire element is in view, then add its .height()
into the equation.
Example: http://jsfiddle.net/8x4Zd/1/
var win = $(window);
var el = $('#my_el');
var winPos = win.scrollTop() + win.height();
var elPos = el.offset().top + el.height();
if( winPos > elPos ) {
// element is in view
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With