Skip to content

Instantly share code, notes, and snippets.

@MuhammadKhizar7
Created December 27, 2020 12:57
Show Gist options
  • Save MuhammadKhizar7/7b3bf5f5c238c51f8709fd2d0a8158e3 to your computer and use it in GitHub Desktop.
Save MuhammadKhizar7/7b3bf5f5c238c51f8709fd2d0a8158e3 to your computer and use it in GitHub Desktop.
It check if element in viewport or not
function isInViewPort(element) {
// Get the bounding client rectangle position in the viewport
const bounding = element.getBoundingClientRect();
// Checking part. Here the code checks if it's *fully* visible
// Edit this part if you just want a partial visibility
return bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth) &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment