Created
June 6, 2019 11:10
-
-
Save TimoM/c504d3b2cc95161371d449afeca36334 to your computer and use it in GitHub Desktop.
Check if element 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 isVisibleInViewport(elem) | |
| { | |
| var y = elem.offsetTop; | |
| var height = elem.offsetHeight; | |
| while ( elem = elem.offsetParent ) | |
| y += elem.offsetTop; | |
| var maxHeight = y + height; | |
| var isVisible = ( y < ( window.pageYOffset + window.innerHeight ) ) && ( maxHeight >= window.pageYOffset ); | |
| return isVisible; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment