Skip to content
IndexRequest a UIAboutContact

Magnetic Button

A button that leans toward the pointer and springs back when it leaves.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Build a magnetic call-to-action button (160x56px pill, accent fill) with a text label inside a span. While the pointer is over a 60px-larger invisible zone around it, translate the button toward the pointer by 0.3 times the distance from the button's centre, clamped to 12px on each axis, and translate the label by 1.6 times that amount for a parallax effect. Apply transforms with translate3d in a pointermove handler (no transition while moving). On pointerleave animate back to zero over 500ms with cubic-bezier(0.34,1.56,0.64,1). Keep the real click target the button itself, with focus-visible ring and pressed scale 0.97. Disable everything for (hover: none) and prefers-reduced-motion.

About this pattern

Definition

A button whose body drifts a few pixels toward the cursor while the pointer is nearby, as if magnetised, and eases back to rest when the pointer leaves. The label moves slightly more than the background for depth.

Purpose & UX principles

  • Adds a feeling of physical response to an important call to action.
  • Purely decorative: it must never move the click target so far that clicking becomes harder.
  • Use sparingly on one or two hero actions, not on every button.

Interaction states

  • Rest: centred.
  • Hover: follows the pointer with a strength of 0.3 of the offset from its centre, capped at 12px.
  • Leave: returns to rest with a spring-like ease.
  • Focus-visible and pressed behave like a normal button.

Motion specification

  • Follow: per pointermove, transform translate3d, no transition (direct manipulation).
  • Release: 500ms cubic-bezier(0.34, 1.56, 0.64, 1).
  • Label parallax: 1.6x the background's offset.
  • Reduced motion: disabled entirely.

Responsive behaviour

  • Disable on touch (hover: none), where there is no hovering pointer.
  • Keep the maximum travel under 12px so the button never leaves its layout slot visually.

Code

HTML
<div class="zone" id="zone"><button class="mag" id="mag"><span id="lab">Get started</span></button></div>

Related patterns

Ripple Effect

↗
Ripple EffectButtons & Actions

Press Feedback

↗
Press FeedbackButtons & Actions

Primary & Secondary Button

↗
Primary & Secondary ButtonButtons & Actions