-
-
Save DKvistgaard/4434160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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