Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anandanand84/0adbf1b4a78f15bbf23a11a1be4002ee to your computer and use it in GitHub Desktop.
Save anandanand84/0adbf1b4a78f15bbf23a11a1be4002ee 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