Skip to content

Instantly share code, notes, and snippets.

@lovetingyuan
Created January 24, 2019 04:59
Show Gist options
  • Save lovetingyuan/c6f01116b5eabdf8f600d079d01944d4 to your computer and use it in GitHub Desktop.
Save lovetingyuan/c6f01116b5eabdf8f600d079d01944d4 to your computer and use it in GitHub Desktop.
is element in viewport?
const 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