Skip to content

Instantly share code, notes, and snippets.

@cdzombak
Created May 9, 2011
Embed
What would you like to do?
check whether an element is currently visible (JS)

This is a small Javascript function which tells you whether an HTML element is currently visible - that is, whether or not it's been scrolled out of view on the page.

This function requires jQuery.

function isScrolledIntoView(elem) {
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ( (elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) && (elemTop >= docViewTop) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment