Guest User

Untitled

a guest
Jun 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. function isInViewport(selector, addClass, descBot, descTop, mobBot, mobTop) {
  2.  
  3. let viewport = document.querySelectorAll(selector);
  4.  
  5. function checkPort() {
  6.  
  7. viewport.forEach((element, index) => {
  8.  
  9. // calculate distanse element from sides of browser
  10. let positionTop = element.getBoundingClientRect().top,
  11. positionBot = element.getBoundingClientRect().bottom;
  12.  
  13. // console.log(positionTop, positionBot)
  14.  
  15. if (window.innerWidth > 767) {
  16.  
  17. dispersion(descBot = 300, descTop = 400);
  18.  
  19. } else {
  20.  
  21. dispersion(mobBot = 200, mobTop = 200);
  22.  
  23. }
  24.  
  25. // function calc dispersion
  26. function dispersion(bottom, top) {
  27.  
  28. if (bottom < positionBot && positionTop < top) {
  29. element.classList.add(addClass);
  30. } else {
  31. element.classList.remove(addClass);
  32. }
  33.  
  34. }
  35.  
  36.  
  37. })
  38.  
  39.  
  40. }
  41.  
  42. document.addEventListener('scroll', checkPort);
  43. }
  44.  
  45. // usage is in you port
  46. isInViewport('.js-viewport', 'about__row--in-view-port');
  47. // or
  48. // isInViewport('.js-viewport', 'about__row--in-view-port', 400, 300, 200, 200);
Add Comment
Please, Sign In to add comment