Skip to content

Instantly share code, notes, and snippets.

@beane
Created April 23, 2014 05:22
Show Gist options
  • Save beane/11203525 to your computer and use it in GitHub Desktop.
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
// 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