Skip to content

Instantly share code, notes, and snippets.

@w-jerome
Created June 8, 2018 10:21
Show Gist options
  • Save w-jerome/9ea6100c9a1c85f18fed73f69408aced to your computer and use it in GitHub Desktop.
Save w-jerome/9ea6100c9a1c85f18fed73f69408aced to your computer and use it in GitHub Desktop.
Javascript — Is in viewport
function isInViewport (el) {
var rect = el.getBoundingClientRect();
return (
rect.left >= 0 && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ||
rect.left < 0 && rect.right >= 0 ||
rect.left >= 0 && rect.left < (window.innerWidth || document.documentElement.clientWidth) ||
rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) ||
rect.top < 0 && rect.bottom >= 0 ||
rect.top >= 0 && rect.top < (window.innerHeight || document.documentElement.clientHeight)
);
}
isInViewport(document.querySelector('.element'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment