Copied!
Guide

How to make a magnetic button in plain JavaScript

A magnetic button leans toward the pointer and springs back when it leaves. It takes one pointer listener and one CSS transition. Here is the pattern and two finished versions to copy.

← Blog
Title card: Magnetic buttons. Preview of three pill buttons from the Enrichr Library.

December 4, 2026

3 min readAbout 15 lines of code

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.

Try it on any site. The free animation library checker shows you which animation libraries actually run on a page (GSAP, Lenis, Framer Motion). Paste a URL, no sign-up for your first lookup.

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?

ElementWhat it does
Magnetic buttonsButtons that pull toward the pointer on a spring while the inner label drifts further for a parallax feel.
Wiggle magnet buttonA pill button that never stops wiggling, gets pulled toward the pointer and snaps home.
CSS button hover setSix 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?

Where to go next

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.