Files
2026-11-02 10:26:32 -06:00

67 lines
1.8 KiB
JavaScript

import { animate } from '../../assets/vendor/animejs/modules/animation/index.js';
import { remove, set } from '../../assets/vendor/animejs/modules/utils/index.js';
const cards = document.querySelectorAll('#soluciones [data-practice-card-tilt]');
const canTilt = window.matchMedia('(hover: hover) and (pointer: fine)').matches;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (cards.length && canTilt && !reduceMotion) {
cards.forEach((card) => {
const maxRotation = 6;
let frame = 0;
let pointerX = 0;
let pointerY = 0;
set(card, { rotateX: 0, rotateY: 0, scale: 1 });
card.classList.add('practice-card-tilt-ready');
const renderTilt = () => {
frame = 0;
const bounds = card.getBoundingClientRect();
const normalizedX = ((pointerX - bounds.left) / bounds.width) * 2 - 1;
const normalizedY = ((pointerY - bounds.top) / bounds.height) * 2 - 1;
const rotateX = normalizedY * -maxRotation;
const rotateY = normalizedX * maxRotation;
remove(card);
animate(card, {
rotateX,
rotateY,
scale: 1.018,
duration: 360,
ease: 'out(3)',
});
};
card.addEventListener('pointerenter', () => {
card.classList.add('is-tilting');
});
card.addEventListener('pointermove', (event) => {
pointerX = event.clientX;
pointerY = event.clientY;
if (!frame) {
frame = window.requestAnimationFrame(renderTilt);
}
});
card.addEventListener('pointerleave', () => {
if (frame) {
window.cancelAnimationFrame(frame);
frame = 0;
}
card.classList.remove('is-tilting');
remove(card);
animate(card, {
rotateX: 0,
rotateY: 0,
scale: 1,
duration: 700,
ease: 'out(4)',
});
});
});
}