Motion Lab / Navs / magnetic-links / playful
// Mechanisme: nav-magnetic-links-playful
import gsap from 'https://esm.sh/gsap@3.12.5';
document.querySelectorAll('[data-magnetic]').forEach(el => {
el.addEventListener('mousemove', e => {
const r = el.getBoundingClientRect();
const x = (e.clientX - r.left - r.width / 2) * 0.5;
const y = (e.clientY - r.top - r.height / 2) * 0.5;
gsap.to(el, { x, y, duration: 0.5, ease: 'back.out(1.7)' });
});
el.addEventListener('mouseleave', () => gsap.to(el, { x: 0, y: 0, duration: 0.7, ease: 'elastic.out(1,0.4)' }));
}); <!-- Skeleton: nav-magnetic-links-playful --> <nav> <a data-magnetic href="#">Link 1</a> <a data-magnetic href="#">Link 2</a> </nav>
/* Styling: nav-magnetic-links-playful */
[data-magnetic] { display: inline-block; padding: 0.5rem 1.5rem; border-radius: 999px; background: #FF4A1C; color: white; }