Skip to content

Instantly share code, notes, and snippets.

@MJ111
Created August 17, 2017 08:02
Show Gist options
  • Save MJ111/d602130720a8435d70d777bcec2b8076 to your computer and use it in GitHub Desktop.
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
// 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