Created
June 8, 2018 10:21
-
-
Save w-jerome/9ea6100c9a1c85f18fed73f69408aced to your computer and use it in GitHub Desktop.
Javascript — Is in viewport
This file contains hidden or 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
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