Skip to content

Instantly share code, notes, and snippets.

@mciastek
Last active September 21, 2019 18:08
Show Gist options
  • Save mciastek/982581ca2c439e2395788533d8aad34e to your computer and use it in GitHub Desktop.
Save mciastek/982581ca2c439e2395788533d8aad34e to your computer and use it in GitHub Desktop.
const isInViewport = ({ top, height, windowHeight }) => {
const scroll = window.scrollY || window.pageYOffset;
const boundsTop = top + scroll;
const viewport = {
top: scroll,
bottom: scroll + windowHeight,
};
const bounds = {
top: boundsTop,
bottom: boundsTop + height,
};
return (bounds.bottom >= viewport.top && bounds.bottom <= viewport.bottom)
|| (bounds.top <= viewport.bottom && bounds.top >= viewport.top);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment