Created
February 24, 2019 20:52
-
-
Save oropesa/66ef9c649c1b35651c23c1bd8b0da01e to your computer and use it in GitHub Desktop.
Returns true if the element specified is visible in the viewport, false otherwise. Use Element.getBoundingClientRect() and the window.inner(Width|Height) values to determine if a given element is visible in the viewport. Omit the second argument to determine if the element is entirely visible, or specify true to determine if it is partially visi…
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
kconst elementIsVisibleInViewport = (el, partiallyVisible = false) => { | |
const { top, left, bottom, right } = el.getBoundingClientRect(); | |
const { innerHeight, innerWidth } = window; | |
return partiallyVisible | |
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && | |
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) | |
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment