Skip to content

Instantly share code, notes, and snippets.

@fulippo
Forked from mataspetrikas/gist:2300296
Created September 7, 2012 07:36
Show Gist options
  • Save fulippo/3664125 to your computer and use it in GitHub Desktop.
Save fulippo/3664125 to your computer and use it in GitHub Desktop.
Check if the DOM node is visible in the viewport
function elementInViewport(el) {
var rect = el.getBoundingClientRect()
return rect.top < (window.innerHeight || document.body.clientHeight) && rect.left < (window.innerWidth || document.body.clientWidth);
}
// and then you can use it:
alert(elementInViewport(document.getElementById('inner')));
// or
alert(elementInViewport($('#inner')[0]));​
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment