Magnetic Button
A button that leans toward the pointer and springs back when it leaves.
AI prompt
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
<div class="zone" id="zone"><button class="mag" id="mag"><span id="lab">Get started</span></button></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.zone{padding:60px;display:grid;place-items:center}
.mag{width:160px;height:56px;border:0;border-radius:99px;background:var(--accent);color:var(--accent-ink);font:inherit;font-weight:700;font-size:15px;cursor:pointer;will-change:transform}
.mag span{display:block;will-change:transform}
.mag.rel,.mag.rel span{transition:transform .5s cubic-bezier(.34,1.56,.64,1)}
.mag:active{scale:.97}
.mag:focus-visible{outline:2px solid var(--accent);outline-offset:4px}const zone=document.getElementById('zone'),mag=document.getElementById('mag'),lab=document.getElementById('lab');
if(matchMedia('(hover:hover)').matches&&!matchMedia('(prefers-reduced-motion:reduce)').matches){
const cl=(v,m)=>Math.max(-m,Math.min(m,v));
zone.addEventListener('pointermove',e=>{const r=mag.getBoundingClientRect(),dx=e.clientX-(r.left+r.width/2),dy=e.clientY-(r.top+r.height/2);
mag.classList.remove('rel');const x=cl(dx*.3,12),y=cl(dy*.3,12);
mag.style.transform='translate3d('+x+'px,'+y+'px,0)';lab.style.transform='translate3d('+x*1.6+'px,'+y*1.6+'px,0)';});
zone.addEventListener('pointerleave',()=>{mag.classList.add('rel');mag.style.transform='';lab.style.transform='';});
}One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Magnetic Button — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.zone{padding:60px;display:grid;place-items:center}
.mag{width:160px;height:56px;border:0;border-radius:99px;background:var(--accent);color:var(--accent-ink);font:inherit;font-weight:700;font-size:15px;cursor:pointer;will-change:transform}
.mag span{display:block;will-change:transform}
.mag.rel,.mag.rel span{transition:transform .5s cubic-bezier(.34,1.56,.64,1)}
.mag:active{scale:.97}
.mag:focus-visible{outline:2px solid var(--accent);outline-offset:4px}
</style></head>
<body><div class="stage">
<div class="zone" id="zone"><button class="mag" id="mag"><span id="lab">Get started</span></button></div>
</div>
<script>
const zone=document.getElementById('zone'),mag=document.getElementById('mag'),lab=document.getElementById('lab');
if(matchMedia('(hover:hover)').matches&&!matchMedia('(prefers-reduced-motion:reduce)').matches){
const cl=(v,m)=>Math.max(-m,Math.min(m,v));
zone.addEventListener('pointermove',e=>{const r=mag.getBoundingClientRect(),dx=e.clientX-(r.left+r.width/2),dy=e.clientY-(r.top+r.height/2);
mag.classList.remove('rel');const x=cl(dx*.3,12),y=cl(dy*.3,12);
mag.style.transform='translate3d('+x+'px,'+y+'px,0)';lab.style.transform='translate3d('+x*1.6+'px,'+y*1.6+'px,0)';});
zone.addEventListener('pointerleave',()=>{mag.classList.add('rel');mag.style.transform='';lab.style.transform='';});
}
</script>
</body></html>