Skip to content

Instantly share code, notes, and snippets.

@gavacho
Forked from DKvistgaard/jquery.isInView.js
Created February 9, 2013 05:55
Show Gist options
  • Save gavacho/4744104 to your computer and use it in GitHub Desktop.
Save gavacho/4744104 to your computer and use it in GitHub Desktop.
jQuery.fn.isInView = function(){
// setup different vars for easier access to these values
var middle = Math.round(this.height() / 2),
winHeight = $(window).height(),
posTop = this.position().top,
scrollTop = $(window).scrollTop(),
middlePos = middle + posTop;
if ( middlePos < scrollTop ) {
// middle of element is above the viewport
return false;
} else if ( middlePos > scrollTop && middlePos < scrollTop + winHeight ) {
// middle of element is within the viewport
return true;
} else if ( middlePos > scrollTop && middlePos > scrollTop + winHeight ) {
// middle of element is below the viewport
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment