Created
February 5, 2014 11:08
-
-
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
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
// 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