Skip to content
Knsept
Free playbook

Magnetic button

A button that leans toward the cursor and springs back. Press-and-drag on touch, gentler with reduced motion.

JavaScriptUI Widgets Updated Oct 2, 2026
Live demo Reduced motion is on, so effects are toned down
<button class="magnet" type="button">
  <span class="magnet-label">
    <b class="for-mouse">Hover near me</b>
    <b class="for-touch">Press and drag</b>
  </span>
</button>

Move your cursor toward the button. Inside the attraction zone, the button follows a fraction of the pointer's offset, and its label follows half as far for a parallax feel. CSS handles the easing, so the script only sets transforms.

On touch screens there's no hover, so the button follows your finger while you press and drag it. touch-action: none stops that drag from scrolling the page.

With reduced motion on, the pull drops to about a third and the label parallax is skipped. It still responds, just calmly.

Tune it: pullStrength controls how far it travels, and attractionZone is the padding around the button that attracts.

Read the full walkthrough: A magnetic button in 30 lines of vanilla JS.

More from the library

Buttons live

Hold-to-confirm button

A safer button for destructive actions. Press and hold until the bar fills; let go early to cancel.

Buttons live

Ripple button

A bright press ripple that starts exactly where you click or tap, and from the center for keyboard users.

Buttons live

Glowing gradient border

A button with a rotating gradient border and soft glow, done with one registered CSS property and no extra elements.