All effectsHover & follow

Magnetic Button

磁吸按钮

The visible button moves a short distance toward the pointer inside its hit area.

Hover, focus, or tap the preview

Where to use it

Add a small responsive movement to a hero call to action. Give the visible artwork room to move within a fixed hit area and keep the movement small near other controls.

Practical example

On an architecture tour page, the Book a tour button pulls a few pixels toward the pointer as it moves inside the hit area. The link stays easy to click from the same spot.

AI prompt

Add Magnetic Button to [target]. Measure the pointer relative to the fixed hit-area center and apply a clamped translation to the visual child. Return the child to center on leave. Keep the content readable in reduced-motion mode and preserve keyboard navigation.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Magnetic Button</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .target {
      position: relative;
      display: grid;
      place-items: center;
      width: min(100%, 208px);
      min-height: 84px;
      padding: 10px;
      color: #f5efff;
      border: 1px solid #aa91ee80;
      border-radius: 13px;
      background: #29213f;
      cursor: pointer;
      font: 680 15px system-ui, sans-serif;
    }
    .target[aria-pressed="true"] { outline: 2px solid #d9c9ff; outline-offset: 3px; }
    @media (prefers-reduced-motion: reduce) {
      .target, .target *, .target::before, .target::after, .target *::before, .target *::after {
        transition: none !important;
        animation: none !important;
      }
    }

    .target { background: #241c36; border-style: dashed; min-height: 109px; }
    .target span { padding: 11px 16px; border-radius: 8px; background: #d4c1ff; color: #251541;
      transition: transform .18s ease; transform: translate(calc(var(--dx, 0) * 18px), calc(var(--dy, 0) * 12px)); }
    @media (prefers-reduced-motion: reduce) { .target span { transform: none; } }
  </style>
</head>
<body>
  <main class="demo">
    <button class="target" type="button" aria-pressed="false" aria-label="Magnetic button"><span>Magnetic button</span></button>
  </main>
  <script>
    const target = document.querySelector('.target');
    target.addEventListener('click', () => {
      target.setAttribute('aria-pressed', String(target.getAttribute('aria-pressed') !== 'true'));
    });

    const zone = document.querySelector('.target');
    const reduce = matchMedia('(prefers-reduced-motion: reduce)');
    function update(event) {
      if (reduce.matches) return;
      const rect = zone.getBoundingClientRect();
      const x = Math.min(rect.width, Math.max(0, event.clientX - rect.left));
      const y = Math.min(rect.height, Math.max(0, event.clientY - rect.top));
      zone.style.setProperty('--x', x + 'px');
      zone.style.setProperty('--y', y + 'px');
      zone.style.setProperty('--dx', ((x / rect.width - .5) * 2).toFixed(3));
      zone.style.setProperty('--dy', ((y / rect.height - .5) * 2).toFixed(3));
    }
    zone.addEventListener('pointermove', update);
    zone.addEventListener('pointerdown', update);
    zone.addEventListener('pointerleave', () => {
      if (zone.getAttribute('aria-pressed') === 'true') return;
      for (const property of ['--x', '--y', '--dx', '--dy']) zone.style.removeProperty(property);
    });
  </script>
</body>
</html>

Original example by UI Skills