Created
August 26, 2016 18:50
-
-
Save jmahc/7cc908d51bc2c48316ad3ff0414e3a98 to your computer and use it in GitHub Desktop.
This gets the height of any element relative to what is visible on screen
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 getVisibleHeight(el) { | |
| var elementHeight = el.offsetHeight; | |
| var windowHeight = window.innerHeight; | |
| var rect = el.getBoundingClientRect(); | |
| var topValue = rect.top; | |
| var bottomValue = rect.bottom; | |
| return Math.max(0, topValue > 0 ? Math.min(elementHeight, windowHeight - topValue) : (bottomValue < windowHeight ? bottomValue : windowHeight)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment