Home / Learn / How do I build a magnetic button and a custom cursor?

How do I build a magnetic button and a custom cursor?

A magnetic button is pulled slightly toward the pointer when it gets close, which makes the button feel alive and distinctive. We write the distance math in a few lines, enable it only on devices with a mouse, and always keep the system cursor visible.

Steps

  1. Use matchMedia("(hover: hover) and (pointer: fine)") to enable the effect only on devices that can hover and have a precise pointer, not on touch screens.
  2. On pointermove compute the button center from getBoundingClientRect(), and the differences dx and dy between the pointer and that center.
  3. If the distance is under 120 pixels move the button with translate(dx x 0.3, dy x 0.3), otherwise return it. A CSS transition makes the motion smooth.
  4. Add a small dot that follows the pointer with transform and pointer-events: none, and keep it as an extra only: do not hide the system cursor with cursor: none.
  5. Under prefers-reduced-motion enable no motion at all, and hide the dot when the pointer leaves the page.

Sources

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #0b1220; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  .mag { padding: 1rem 2.2rem; border: 0; border-radius: 999px; background: #38bdf8; color: #082f49; font: 800 1.2rem system-ui, "Segoe UI", Tahoma, sans-serif; cursor: pointer; transition: transform .25s ease-out; }
  .cursor { position: fixed; inset: 0 auto auto 0; width: 14px; height: 14px; margin: -7px; border-radius: 50%; background: #f472b6; pointer-events: none; opacity: 0; transition: opacity .2s; z-index: 10; }
  .cursor.on { opacity: 1; }
  @media (prefers-reduced-motion: reduce) { .mag { transition: none; } }
</style>
<button class="mag" id="mag">تواصل معنا</button>
<div class="cursor" id="cur" aria-hidden="true"></div>
<script>
  // run only for a real hover-capable, precise pointer (mouse), and never for reduced motion.
  // The native cursor stays visible, the pink dot is only an extra.
  const fine = matchMedia('(hover: hover) and (pointer: fine)').matches;
  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  if (fine && !reduce) {
    const mag = document.getElementById('mag'), cur = document.getElementById('cur');
    addEventListener('pointermove', e => {
      cur.classList.add('on');
      cur.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`;
      const r = mag.getBoundingClientRect();
      const dx = e.clientX - (r.left + r.width / 2), dy = e.clientY - (r.top + r.height / 2);
      const near = Math.hypot(dx, dy) < 120;                      // pull only when the pointer is close
      mag.style.transform = near ? `translate(${dx * .3}px, ${dy * .3}px)` : '';
    });
    document.addEventListener('pointerleave', () => { cur.classList.remove('on'); mag.style.transform = ''; });
  }
</script>
</html>

can't find the motion you need? Request it here

Related articles: