Created
April 23, 2014 05:22
-
-
Save beane/11203525 to your computer and use it in GitHub Desktop.
Function to determine if a jQuery or window element is in view vertically
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
// rectangle.bottom shows the absolute position in pixels of the bottom of the rectangle from the top of the entire DOM. | |
// as you scroll down the page, this number gets smaller, and becomes negative when above the visible window rectangle.height | |
// shows the height in pixels of the rectangle. | |
// the difference between them should never exceed the height of the visible window | |
function isInViewVertically(element) { | |
if (element instanceof jQuery) element = element[0] | |
var rectangle = element.getBoundingClientRect(); | |
var windowHeight = window.innerHeight; | |
return Math.abs(rectangle.bottom - rectangle.height) <= windowHeight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment