Advertisement
Guest User

Untitled

a guest
May 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. const onScreen = elm => {
  2.  
  3.  
  4. const getViewportY = () => {
  5.  
  6. const top = window.pageYOffset || document.documentElement.scrollTop;
  7. const bottom = top + document.documentElement.clientHeight;
  8.  
  9. return { top, bottom }
  10. }
  11.  
  12.  
  13. const getElmCoordinates = elm => {
  14. const { top, bottom } = elm.getBoundingClientRect();
  15. return { top, bottom }
  16. }
  17.  
  18.  
  19. const viewport = getViewportY();
  20.  
  21. const elmPosition = getElmCoordinates(elm);
  22.  
  23. const top = elmPosition.top + viewport.top;
  24. const bottom = elmPosition.bottom + viewport.top;
  25.  
  26.  
  27. const onScreenFlags = [
  28. (top >= viewport.top && top <= viewport.bottom),
  29. (bottom >= viewport.top && bottom <= viewport.bottom)
  30. ];
  31.  
  32. const atLeastPartlyOnScreen = onScreenFlags.includes(true);
  33.  
  34. return atLeastPartlyOnScreen;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement