Created
August 17, 2017 08:02
-
-
Save MJ111/d602130720a8435d70d777bcec2b8076 to your computer and use it in GitHub Desktop.
Check if the element is in the middle of window at scroll event
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
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect | |
function handleScroll() { | |
const position = document.getElementById('content').getBoundingClientRect() | |
// check if the element is in the middle of viewport(window) | |
if (position.top < window.innerHeight/2 && position.bottom > 0 && position.bottom > window.innerHeight/2) { | |
// do what you want to do | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment