myWow.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const offset = 10;
  2. const signCalss = 'mywow';
  3. const animateClass = 'animate__animated';
  4. export default defineNuxtPlugin((nuxtApp) => {
  5. const innerHeight = window.innerHeight - offset;
  6. const onAnimated = () => {
  7. const wowEl = document.querySelectorAll(`.${signCalss}`);
  8. try {
  9. if (wowEl && wowEl.length > 0) {
  10. wowEl.forEach(el => {
  11. // const elClassStr = el.classList.value || '';
  12. if (!el.classList.contains(animateClass)) {
  13. // 元素距离可视区顶部的距离 = 元素距离页面顶部的距离 - 页面向上滚动的距离
  14. const ks_h = el.offsetTop - window.scrollY;
  15. // 当元素到达 加载动画区域以内时,动态处理动画class
  16. if (ks_h <= innerHeight && ks_h > offset) {
  17. // console.log("el.getAttribute('animate')", el)
  18. const animateParams = el.getAttribute('animate-param').split(',')
  19. el.classList.remove(signCalss);
  20. if (animateParams && animateParams.length > 0) {
  21. let str = animateClass;
  22. animateParams.forEach(ap => {
  23. el.classList.add(`animate__${ap}`)
  24. })
  25. el.classList.add(animateClass)
  26. }
  27. }
  28. }
  29. })
  30. }
  31. } catch { }
  32. }
  33. window.addEventListener("scroll", onAnimated);
  34. nuxtApp.provide('onAnimated', onAnimated);
  35. })