Magnetic Button

마그네틱 버튼

A button that gets pulled toward the cursor as it approaches, like a magnet — it feels responsive before you even click.

Also known as: Magnetic effectCursor-pull button
···
html
<button class="mag" id="btn"><span>Hover</span></button>
css
.fx-dot{position:absolute;left:0;top:0;width:12px;height:12px;margin:-6px 0 0 -6px;border-radius:50%;
  background:var(--accent);box-shadow:0 0 0 5px color-mix(in srgb,var(--accent) 18%,transparent),0 2px 6px rgba(0,0,0,.3);
  pointer-events:none;z-index:9999;transition:opacity .25s}
.mag{position:relative;padding:clamp(12px,6vmin,20px) clamp(22px,9vmin,36px);border:none;border-radius:999px;
  background:var(--accent);color:#fff;font-weight:700;font-size:clamp(12px,4vmin,16px);cursor:pointer;
  transition:transform .15s ease-out;box-shadow:0 10px 24px color-mix(in srgb,var(--accent) 35%,transparent)}
.mag span{display:block;transition:transform .15s ease-out}
js
const fxDot=document.createElement('div');fxDot.className='fx-dot';document.body.appendChild(fxDot);
let fxAuto=true,fxX=innerWidth/2,fxY=innerHeight/2;
function fxHide(){fxDot.style.opacity='0';}
addEventListener('pointerdown',()=>{fxAuto=false;fxHide();},{capture:true});
addEventListener('pointermove',e=>{fxAuto=false;fxHide();fxX=e.clientX;fxY=e.clientY;},{capture:true});
function fxWalk(x,y,ms){
  return new Promise(res=>{
    const sx=fxX,sy=fxY,start=performance.now();
    function step(now){
      if(!fxAuto)return res();
      const p=Math.min(1,(now-start)/ms),e=1-Math.pow(1-p,3);
      fxX=sx+(x-sx)*e;fxY=sy+(y-sy)*e;
      fxDot.style.transform='translate('+fxX+'px,'+fxY+'px)';
      if(p<1)requestAnimationFrame(step);else res();
    }
    requestAnimationFrame(step);
  });
}
function fxWait(ms){
  return new Promise(res=>{
    const start=performance.now();
    function step(now){ if(!fxAuto)return res(); if(now-start<ms)requestAnimationFrame(step);else res(); }
    requestAnimationFrame(step);
  });
}

const btn=document.getElementById('btn');
const label=btn.querySelector('span');
function render(x,y){
  const r=btn.getBoundingClientRect();
  const cx=r.left+r.width/2, cy=r.top+r.height/2;
  const dx=x-cx, dy=y-cy, dist=Math.hypot(dx,dy);
  const radius=Math.max(r.width,r.height)*1.7;
  if(dist<radius){
    const p=1-dist/radius;
    btn.style.transform='translate('+(dx*0.4*p)+'px,'+(dy*0.4*p)+'px)';
    label.style.transform='translate('+(dx*0.18*p)+'px,'+(dy*0.18*p)+'px)';
  } else {
    btn.style.transform='translate(0,0)';
    label.style.transform='translate(0,0)';
  }
}
let t=0;
(function loop(){
  if(fxAuto){
    t+=0.025;
    fxX=innerWidth/2+Math.sin(t)*innerWidth*0.4;
    fxY=innerHeight/2+Math.cos(t*1.5)*innerHeight*0.4;
    fxDot.style.transform='translate('+fxX+'px,'+fxY+'px)';
  }
  render(fxX,fxY);
  requestAnimationFrame(loop);
})();

Each frame, measure the distance between the button's center and the cursor. Inside a radius, nudge the button toward the cursor with `transform: translate()` — closer means more pull, and it springs back once the cursor leaves the radius.

Moving the inner label a bit further than the button itself (say 40% for the button, 18% for the text) creates a layered, parallax feel. Keep the transition short (0.1–0.2s) so it reads as instant, finger-like response.

Set the radius and max offset too generously and the button starts jumping around distractingly. A radius of 1.5–2x the button size and a max offset of 30–40% of it is a safe range.

When to use

Reserve it for one or two focal CTAs — a hero action, a "click here" moment. Apply it to every button in a list and it becomes fatiguing.