Skip to content

Instantly share code, notes, and snippets.

@DKvistgaard
Forked from davidpdrsn/jquery.isInView.js
Created January 2, 2013 12:15
Show Gist options
  • Save DKvistgaard/4434160 to your computer and use it in GitHub Desktop.
Save DKvistgaard/4434160 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