Skip to content

Instantly share code, notes, and snippets.

@hsnyc
Created October 11, 2020 16:17
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 hsnyc/663a00eb78d8cc7e793b1cbda6d7b308 to your computer and use it in GitHub Desktop.
Save hsnyc/663a00eb78d8cc7e793b1cbda6d7b308 to your computer and use it in GitHub Desktop.
Check If an Element is Visible in the Viewport
/*
If the element is in the viewport, the function returns true. Otherwise, it returns false.
https://www.javascripttutorial.net/dom/css/check-if-an-element-is-visible-in-the-viewport/
*/
function isInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment