December 4, 2026
A magnetic button moves toward the pointer while it hovers and snaps back when it leaves. To build one, measure the pointer's distance from the centre of the button, move the button by a fraction of that distance, and reset it on leave. It needs no library. The free Enrichr Library has two finished versions you can copy.
What is the basic pattern?
<span class="magnet"><button><span>Get started</span></button></span>
const fine = matchMedia("(hover: hover) and (pointer: fine)").matches;
const calm = matchMedia("(prefers-reduced-motion: reduce)").matches;
if (fine && !calm) {
document.querySelectorAll(".magnet").forEach((wrap) => {
const btn = wrap.querySelector("button");
const label = btn.querySelector("span");
wrap.addEventListener("pointermove", (e) => {
const r = wrap.getBoundingClientRect();
const x = e.clientX - (r.left + r.width / 2);
const y = e.clientY - (r.top + r.height / 2);
btn.style.transform = `translate(${x * 0.3}px, ${y * 0.3}px)`;
label.style.transform = `translate(${x * 0.15}px, ${y * 0.15}px)`;
});
wrap.addEventListener("pointerleave", () => {
btn.style.transform = label.style.transform = "";
});
});
}
.magnet { display: inline-block; padding: 16px; }
.magnet button, .magnet button span {
display: inline-block;
transition: transform 0.4s cubic-bezier(0.3, 1.6, 0.5, 1);
}
Why is there a wrapper?
The wrapper stays still and catches the pointer. The button moves inside it. If you listen on the button itself, the button slides out from under the pointer, the pointer "leaves", the button snaps back, the pointer "enters" again, and the whole thing shakes. The padding on the wrapper also sets how far away the pull begins.
How do I get the springy snap back?
The curve cubic-bezier(0.3, 1.6, 0.5, 1) goes past its target and returns, which reads as a spring. For a real
spring that keeps its speed when the pointer changes direction, update the position every frame with a velocity and a damping
value instead of a CSS transition. That is what the Library version does.
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Magnetic buttons | Buttons that pull toward the pointer on a spring while the inner label drifts further for a parallax feel. |
| Wiggle magnet button | A pill button that never stops wiggling, gets pulled toward the pointer and snaps home. |
| CSS button hover set | Six pure-CSS button hovers for when you want no JavaScript at all. |
Each page has live controls, the HTML, CSS and JavaScript to copy, and a pen to fork on CodePen.
What should I be careful about?
- Keep it a real
<button>or<a>so the keyboard still works. - Skip the effect on touch screens and for visitors who ask for reduced motion, as the code above does.
- Use it on one or two main buttons. A page where everything is magnetic is tiring.
Where to go next
- How to make a custom cursor.
- How to make a 3D card tilt hover effect.
- The Enrichr Library: 175 free elements.
FAQ
How do I make a magnetic button without GSAP?
Listen for pointermove on the button, work out the pointer's offset from the button centre, and set a CSS transform: translate() to a fraction of that offset. On pointerleave, reset the transform and let a CSS transition with an overshooting curve carry it home.
Why does my magnetic button jitter?
Usually because the button moves out from under the pointer, which fires leave and enter events in a loop. Put the listener on a slightly larger wrapper that does not move, and transform the button inside it.
Should a magnetic button work on touch screens?
No. There is no hover on touch, so the effect has nothing to follow. Wrap the listeners in a check for (hover: hover) and (pointer: fine) and leave the button still everywhere else.
Is the magnetic effect bad for accessibility?
It is fine if the button stays a real <button> or link, keeps a visible focus style, and holds still when the visitor has asked for reduced motion.