Skip to content

Instantly share code, notes, and snippets.

@cdzombak
Created May 9, 2011 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdzombak/962021 to your computer and use it in GitHub Desktop.
Save cdzombak/962021 to your computer and use it in GitHub Desktop.
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