Skip to content

Instantly share code, notes, and snippets.

@linuslundahl
Created December 10, 2011 11:20
Show Gist options
  • Save linuslundahl/1454947 to your computer and use it in GitHub Desktop.
Save linuslundahl/1454947 to your computer and use it in GitHub Desktop.
jQuery Function that checks if an element is currently in the browser viewport.
function isScrolledIntoView(elem) {
var $win = $(window),
$elem = $(elem),
docViewTop = $win.scrollTop(),
docViewBottom = docViewTop + $win.height(),
elemTop = $elem.offset().top,
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