utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * source: https://pawelgrzybek.com/page-scroll-in-vanilla-javascript/
  3. *
  4. */
  5. export function scrollIt (destination, duration = 200, easing = 'linear', callback = null) {
  6. const easings = {
  7. linear(t) {
  8. return t;
  9. },
  10. easeInQuad(t) {
  11. return t * t;
  12. },
  13. easeOutQuad(t) {
  14. return t * (2 - t);
  15. },
  16. easeInOutQuad(t) {
  17. return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
  18. },
  19. easeInCubic(t) {
  20. return t * t * t;
  21. },
  22. easeOutCubic(t) {
  23. return (--t) * t * t + 1;
  24. },
  25. easeInOutCubic(t) {
  26. return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
  27. },
  28. easeInQuart(t) {
  29. return t * t * t * t;
  30. },
  31. easeOutQuart(t) {
  32. return 1 - (--t) * t * t * t;
  33. },
  34. easeInOutQuart(t) {
  35. return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
  36. },
  37. easeInQuint(t) {
  38. return t * t * t * t * t;
  39. },
  40. easeOutQuint(t) {
  41. return 1 + (--t) * t * t * t * t;
  42. },
  43. easeInOutQuint(t) {
  44. return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
  45. }
  46. };
  47. const start = window.pageYOffset;
  48. const startTime = 'now' in window.performance ? performance.now() : new Date().getTime();
  49. const documentHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);
  50. const windowHeight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
  51. const destinationOffset = typeof destination === 'number' ? destination : destination.offsetTop;
  52. const destinationOffsetToScroll = Math.round(documentHeight - destinationOffset < windowHeight ? documentHeight - windowHeight : destinationOffset);
  53. if ('requestAnimationFrame' in window === false) {
  54. window.scroll(0, destinationOffsetToScroll);
  55. if (callback) {
  56. callback();
  57. }
  58. return;
  59. }
  60. function scroll() {
  61. const now = 'now' in window.performance ? performance.now() : new Date().getTime();
  62. const time = Math.min(1, ((now - startTime) / duration));
  63. const timeFunction = easings[easing](time);
  64. window.scroll(0, Math.ceil((timeFunction * (destinationOffsetToScroll - start)) + start));
  65. if (window.pageYOffset === destinationOffsetToScroll) {
  66. if (callback) {
  67. callback();
  68. }
  69. return;
  70. }
  71. requestAnimationFrame(scroll);
  72. }
  73. scroll();
  74. }
  75. /**
  76. *
  77. * source: https://stackoverflow.com/a/16270434
  78. */
  79. export function isElementInViewport (el) {
  80. const rect = el.getBoundingClientRect();
  81. return rect.bottom > 0 &&
  82. rect.right > 0 &&
  83. rect.left < (window.innerWidth || document.documentElement.clientWidth) &&
  84. rect.top < (window.innerHeight || document.documentElement.clientHeight);
  85. }