myWow.js 1.8 KB

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