Magnetic Button

Button that pulls toward the cursor, premium hover effect.

Tailwind CSS
<button class="magnetic-btn px-8 py-4 rounded-full bg-slate-900 text-white font-bold text-sm transition-transform duration-200">
  Hover me
</button>
<style>
.magnetic-btn { transition: transform 0.2s ease-out; }
</style>
<script>
document.querySelectorAll('.magnetic-btn').forEach(function(btn) {
  btn.addEventListener('mousemove', function(e) {
    var r = btn.getBoundingClientRect();
    var x = e.clientX - r.left - r.width / 2;
    var y = e.clientY - r.top - r.height / 2;
    btn.style.transform = 'translate(' + x * 0.25 + 'px,' + y * 0.25 + 'px)';
  });
  btn.addEventListener('mouseleave', function() {
    btn.style.transform = 'translate(0,0)';
  });
});
</script>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Emerald

Use emerald for positive actions like saving or confirming.

Tailwind CSS

Outline

The outline style suits secondary actions next to a solid primary button.

Tailwind CSS

Strong pull

The stronger pull feels playful; save it for hero CTAs, not forms.

Tailwind CSS

About this magnetic button

Magnetic buttons are the interaction that makes visitors go "how did they do that?" As your cursor approaches, the button leans toward it, then springs back when you leave. It is a staple of award-winning portfolio sites and premium landing pages, and the magnetic button effect is simpler than it looks.

The technique: on mousemove, measure the cursor's offset from the button center and translate the button by a fraction of that offset (0.25 here). On mouseleave, reset to zero. The CSS transition makes the movement smooth and springy.

Use it sparingly: one magnetic CTA per hero section is plenty. The 0.25 multiplier controls the pull strength; higher feels playful, lower feels refined. This needs JavaScript, so it is an enhancement, not a core interaction. The button works fine without it.

Hero CTAs compete with everything else on the page for a glance. A magnetic button earns that glance because it reacts before the click: drift the cursor near it and the button tilts in your direction, then eases back to rest when you move away. That pre-click response signals craft and makes the primary action feel alive. It is a small moment of delight that says this page was built with care.

The technique is offset math. On mousemove, the handler measures the cursor's distance from the button center using getBoundingClientRect: x equals clientX minus left minus half the width. It then sets transform to translate(x * 0.25, y * 0.25), so the button follows at quarter strength, creating a pull rather than sticking to the cursor. On mouseleave the transform resets to translate(0,0), and the CSS transition on transform makes both directions smooth and springy.

The 0.25 multiplier is the personality dial: 0.15 feels refined, 0.4 feels playful. Fold scale(1.05) into the transform string for extra pop on approach. Reserve the effect for one primary CTA per hero; when every button is magnetic, none of them is. Pair it with a soft glow shadow and the button becomes the obvious thing to click.

The usability mistake is cranking the multiplier so high that the button dodges the cursor and users chase a moving target to click it. A button that runs away is infuriating, not delightful. Keep the pull at 0.25 or below with a quick transition, so the button leans toward the visitor but never escapes them.

Where to use it

  • ✓ Hero section CTAs
  • ✓ Portfolio contact buttons
  • ✓ Landing page primary actions
  • ✓ Playful brand sites

Customization tips

Adjust the 0.25 multiplier: 0.15 is subtle, 0.4 is dramatic. Add scale(1.05) to the transform for extra pop. Combine with a glow shadow for maximum premium feel.

Design notes

When to use

Use it for one hero CTA per page on portfolio sites and premium landing pages. The button leaning toward the cursor is a delightful detail that makes visitors notice the primary action. Keep the pull subtle so it feels refined, not gimmicky.

What to avoid

Do not make every button magnetic; the novelty wears off fast and the page feels unstable. Do not use it on small targets, since a button that moves as you approach it is harder to click.

Accessibility

The pull is pointer-only, so keyboard users must get a fully usable static button. Disable the effect under prefers-reduced-motion and make sure the button never shifts away from its focused position.

Frequently asked questions

How does a magnetic button work?

JavaScript tracks the cursor position relative to the button center and translates the button toward the cursor by a fraction of the offset. A CSS transition smooths the movement.

Do magnetic buttons work on mobile?

No, they need a cursor. On touch devices the button simply does not move, which is fine. Treat it as a desktop enhancement.

Are magnetic buttons bad for accessibility?

The movement is decorative; the button remains fully clickable and keyboard-accessible. Respect prefers-reduced-motion by disabling the effect for users who request it.

How do I make a magnetic button with vanilla JavaScript?

On mousemove near the button, compute the offset between the cursor and the button center, then translate the button toward the cursor by a fraction of that offset. On mouseleave, clear the transform with a transition so it springs back. A lerp in a requestAnimationFrame loop makes the motion buttery.

How strong should the magnetic pull be?

A pull strength of 0.2 to 0.4 times the cursor offset feels natural, capped at about 20 to 30px of travel so the button never leaves its layout spot. Stronger values feel gimmicky and can make the button hard to click. Ease the return with a slightly bouncy cubic-bezier.

How do I disable the magnetic effect on touch devices?

Check window.matchMedia('(pointer: fine)').matches before attaching your mousemove listeners, and skip the effect entirely when it is false. Touch users get no benefit from cursor proximity, and the transform can interfere with tap targets. The button still works normally as a plain button.

Customize it with our tools