Created
October 11, 2020 16:17
-
-
Save hsnyc/663a00eb78d8cc7e793b1cbda6d7b308 to your computer and use it in GitHub Desktop.
Check If an Element is Visible in the Viewport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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