153 lines
4.5 KiB
JavaScript
153 lines
4.5 KiB
JavaScript
import { animate } from '../../assets/vendor/animejs/modules/animation/index.js';
|
|
import { splitText } from '../../assets/vendor/animejs/modules/text/index.js';
|
|
import { remove, set, stagger } from '../../assets/vendor/animejs/modules/utils/index.js';
|
|
|
|
const titles = document.querySelectorAll('[data-anime-scroll-title]');
|
|
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
if (titles.length && !reduceMotion) {
|
|
const initTitleAnimation = (title) => {
|
|
const gradientSources = Array.from(title.querySelectorAll('[class*="bg-gradient-to-r"]')).map((element, index) => {
|
|
element.setAttribute('data-anime-title-gradient', String(index));
|
|
return {
|
|
index,
|
|
backgroundImage: window.getComputedStyle(element).backgroundImage,
|
|
continuous: element.hasAttribute('data-anime-title-gradient-continuous'),
|
|
};
|
|
});
|
|
|
|
const split = splitText(title, {
|
|
words: {
|
|
wrap: 'clip',
|
|
class: 'solutions-title-word',
|
|
},
|
|
accessible: true,
|
|
});
|
|
const words = split.words;
|
|
|
|
if (!words.length) {
|
|
return;
|
|
}
|
|
|
|
title.classList.add('anime-scroll-title-ready');
|
|
|
|
const applyTitleGradients = () => {
|
|
gradientSources.forEach((gradient) => {
|
|
const gradientElement = Array.from(
|
|
title.querySelectorAll(`[data-anime-title-gradient="${gradient.index}"]`)
|
|
).find((element) => element.querySelector('[data-word]'));
|
|
|
|
if (!gradientElement) {
|
|
return;
|
|
}
|
|
|
|
const gradientRect = gradientElement.getBoundingClientRect();
|
|
|
|
gradientElement.querySelectorAll('[data-word]').forEach((word) => {
|
|
word.style.backgroundImage = gradient.backgroundImage;
|
|
|
|
if (!gradient.continuous || gradientRect.width <= 0) {
|
|
return;
|
|
}
|
|
|
|
const wordRect = word.getBoundingClientRect();
|
|
word.style.backgroundSize = `${gradientRect.width}px 100%`;
|
|
word.style.backgroundPosition = `${gradientRect.left - wordRect.left}px 0`;
|
|
word.style.backgroundRepeat = 'no-repeat';
|
|
});
|
|
});
|
|
};
|
|
|
|
applyTitleGradients();
|
|
|
|
if ('ResizeObserver' in window && gradientSources.some((gradient) => gradient.continuous)) {
|
|
const gradientResizeObserver = new ResizeObserver(() => {
|
|
window.requestAnimationFrame(applyTitleGradients);
|
|
});
|
|
gradientResizeObserver.observe(title);
|
|
}
|
|
|
|
set(words, { opacity: 0, y: '110%' });
|
|
|
|
const reveal = (origin) => {
|
|
remove(words);
|
|
set(words, { opacity: 0, y: origin });
|
|
animate(words, {
|
|
opacity: [0, 1],
|
|
y: [origin, '0%'],
|
|
duration: 760,
|
|
delay: stagger(65),
|
|
ease: 'out(4)',
|
|
});
|
|
};
|
|
|
|
const conceal = (destination) => {
|
|
remove(words);
|
|
animate(words, {
|
|
opacity: 0,
|
|
y: destination,
|
|
duration: 440,
|
|
delay: stagger(34, { from: 'last' }),
|
|
ease: 'in(3)',
|
|
});
|
|
};
|
|
|
|
let lastScrollPosition = window.scrollY;
|
|
let isVisible = false;
|
|
let introObserver = null;
|
|
|
|
const revealWhenReady = (origin) => {
|
|
if (title.id !== 'home-hero-title' || !document.getElementById('xmartica-intro')) {
|
|
reveal(origin);
|
|
return;
|
|
}
|
|
|
|
introObserver = new MutationObserver(() => {
|
|
if (!document.getElementById('xmartica-intro')) {
|
|
introObserver.disconnect();
|
|
introObserver = null;
|
|
if (isVisible) {
|
|
reveal(origin);
|
|
}
|
|
}
|
|
});
|
|
introObserver.observe(document.body, { childList: true });
|
|
};
|
|
|
|
const viewportObserver = new IntersectionObserver((entries) => {
|
|
entries.forEach((entry) => {
|
|
const currentScrollPosition = window.scrollY;
|
|
const isScrollingBackward = currentScrollPosition < lastScrollPosition;
|
|
|
|
if (entry.isIntersecting && !isVisible) {
|
|
isVisible = true;
|
|
revealWhenReady(isScrollingBackward ? '-110%' : '110%');
|
|
} else if (!entry.isIntersecting && isVisible) {
|
|
if (introObserver) {
|
|
introObserver.disconnect();
|
|
introObserver = null;
|
|
}
|
|
conceal(isScrollingBackward ? '110%' : '-110%');
|
|
isVisible = false;
|
|
}
|
|
|
|
lastScrollPosition = currentScrollPosition;
|
|
});
|
|
}, {
|
|
threshold: 0.2,
|
|
});
|
|
|
|
viewportObserver.observe(title);
|
|
};
|
|
|
|
const initTitleAnimations = () => {
|
|
titles.forEach(initTitleAnimation);
|
|
};
|
|
|
|
if (document.fonts && document.fonts.ready) {
|
|
document.fonts.ready.then(initTitleAnimations);
|
|
} else {
|
|
initTitleAnimations();
|
|
}
|
|
}
|