Created
June 17, 2017 08:55
-
-
Save sarfarazansari/64c18e3a8f065cd33e5713c783ec6009 to your computer and use it in GitHub Desktop.
Check for html element is visible in viewport by pure javascript (vanilla)
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 isVisibleInViewport(id) { | |
var element = document.getElementById(id); | |
var rect = element.getBoundingClientRect(); | |
return ( | |
rect.top >= 0 && | |
rect.left >= 0 && | |
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && | |
rect.right <= (window.innerWidth || document.documentElement.clientWidth) | |
); | |
} | |
//init | |
console.log(isInViewport("my-element")); | |
//return true or false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment