Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Created February 5, 2014 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabssnake/8821321 to your computer and use it in GitHub Desktop.
Save gabssnake/8821321 to your computer and use it in GitHub Desktop.
find out if element is within the viewport, thus visible in that sense
// http://ejohn.org/blog/getboundingclientrect-is-awesome/
// http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433
function visible_viewport(el, offset) {
var r, html;
offset = typeof offset !== 'undefined' ? offset : 200;
if ( !el || 1 !== el.nodeType ) { return false; }
html = document.documentElement;
r = el.getBoundingClientRect();
return ( !!r && r.bottom >= offset && r.right >= offset && r.top <= (html.clientHeight+offset) && r.left <= (html.clientWidth+offset) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment