118 lines
3.0 KiB
JavaScript
118 lines
3.0 KiB
JavaScript
(() => {
|
|
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
|
if (window.Shopify && window.Shopify.designMode) return;
|
|
if (!window.anime || typeof window.anime.animate !== 'function' || typeof window.anime.onScroll !== 'function') return;
|
|
|
|
const { animate, onScroll, set } = window.anime;
|
|
const sections = [...document.querySelectorAll('#MainContent > .shopify-section')];
|
|
|
|
if (!sections.length) return;
|
|
|
|
document.documentElement.classList.add('motion');
|
|
|
|
const presets = [
|
|
{ y: 48, x: 0, scale: 1 },
|
|
{ y: 28, x: 0, scale: 1 },
|
|
{ y: 32, x: 0, scale: 0.97 },
|
|
{ y: 24, x: 0, scale: 1 },
|
|
];
|
|
|
|
const running = new WeakMap();
|
|
const phase = new WeakMap();
|
|
|
|
const stop = (section) => {
|
|
const current = running.get(section);
|
|
if (!current) return;
|
|
if (typeof current.cancel === 'function') current.cancel();
|
|
else if (typeof current.pause === 'function') current.pause();
|
|
running.delete(section);
|
|
};
|
|
|
|
const presetFor = (section, direction) => {
|
|
const base = presets[Number(section.dataset.motionIndex) % presets.length];
|
|
return {
|
|
x: base.x * direction,
|
|
y: base.y * direction,
|
|
scale: base.scale,
|
|
};
|
|
};
|
|
|
|
const play = (section, from, to, duration, ease) => {
|
|
stop(section);
|
|
set(section, from);
|
|
const animation = animate(section, {
|
|
x: to.x,
|
|
y: to.y,
|
|
scale: to.scale,
|
|
opacity: to.opacity,
|
|
duration,
|
|
ease,
|
|
});
|
|
running.set(section, animation);
|
|
return animation;
|
|
};
|
|
|
|
const show = (section, direction) => {
|
|
if (phase.get(section) === 'in') return;
|
|
phase.set(section, 'in');
|
|
section.style.pointerEvents = '';
|
|
const from = presetFor(section, direction);
|
|
play(section, {
|
|
x: from.x,
|
|
y: from.y,
|
|
scale: from.scale,
|
|
opacity: 0,
|
|
}, {
|
|
x: 0,
|
|
y: 0,
|
|
scale: 1,
|
|
opacity: 1,
|
|
}, 800, 'outCubic');
|
|
};
|
|
|
|
const hide = (section, direction) => {
|
|
if (phase.get(section) === 'out') return;
|
|
phase.set(section, 'out');
|
|
section.style.pointerEvents = 'none';
|
|
const to = presetFor(section, direction);
|
|
play(section, {
|
|
x: 0,
|
|
y: 0,
|
|
scale: 1,
|
|
opacity: 1,
|
|
}, {
|
|
x: to.x,
|
|
y: to.y,
|
|
scale: to.scale,
|
|
opacity: 0,
|
|
}, 550, 'inCubic');
|
|
};
|
|
|
|
sections.forEach((section, index) => {
|
|
section.dataset.motionIndex = String(index);
|
|
|
|
onScroll({
|
|
target: section,
|
|
axis: 'y',
|
|
repeat: true,
|
|
enter: 'bottom top',
|
|
leave: 'top bottom',
|
|
onEnterForward: () => show(section, 1),
|
|
onLeaveForward: () => hide(section, -1),
|
|
onEnterBackward: () => show(section, -1),
|
|
onLeaveBackward: () => hide(section, 1),
|
|
});
|
|
});
|
|
|
|
const revealVisible = () => {
|
|
const viewHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
sections.forEach((section) => {
|
|
const box = section.getBoundingClientRect();
|
|
const visible = box.bottom > viewHeight * 0.08 && box.top < viewHeight * 0.92;
|
|
if (visible) show(section, 1);
|
|
});
|
|
};
|
|
|
|
revealVisible();
|
|
})();
|