Last active
February 26, 2020 13:07
-
-
Save ncesar/0c073ed9bbda710b760d2afafb588583 to your computer and use it in GitHub Desktop.
Check if a React component is out the viewport
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
const checkIfComponentIsOutViewport = () => { | |
console.log('executed'); | |
if (componentRef.current) { | |
const componentCurrentPosition = componentRef.current.getBoundingClientRect(); | |
if (componentCurrentPosition.top < 0) { | |
console.log('top is out'); | |
} | |
if ( | |
componentCurrentPosition.bottom > | |
(window.innerHeight || document.documentElement.clientHeight) | |
) { | |
console.log('bottom is out'); | |
} | |
if (componentCurrentPosition.left < 0) { | |
// Left side is out of viewoprt | |
} | |
if (componentCurrentPosition.right > (window.innerWidth || document.documentElement.clientWidth)) { | |
// Right side is out of viewport | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment