Back to the library

Hover — magnetic, on a tether

Variant: Magnetic tether

A round button that leans toward the pointer before the pointer reaches it, and the whole design problem is the leash. The pull is a third of the pointer’s offset from centre, capped at eight pixels: uncapped, a target that tracks the hand that closely stops being a target and becomes something you chase. It trails the hand by about 90ms on ease-out, so it reads as answering rather than predicting, and the glyph rides half again as far as the shell on the same beats, which is the cheapest depth cue there is — the front of the button leans further than the back. Two thresholds, two signals: being inside the pull zone (drawn here as a dashed ring, invisible in production) earns the lean; being on the face earns the darker fill, so a pointer that merely passes close never claims it was over. Release is a plain 280ms ease-out home. A spring is the obvious reach here and the wrong one: on an eight-pixel lean its overshoot is a single pixel nobody sees, and the cap is the physics that actually matters. Reach for it on one or two primary actions per screen, never on a toolbar; ten magnetic buttons in a row is a field of things flinching at you.

hovercontrolgesturephysicsloop

The source

A complete HTML document. Paste it into an empty .html file and it plays — no stylesheet, script, font, or image to fetch.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Hover — magnetic, on a tether</title>
    <style>
      :root {
        /* Neutrals — fixed, never remixed. */
        --ivory: #faf9f5;
        --paper: #ffffff;
        --oat: #e3dacc;
        --slate: #141413;

        /* Knobs — the remix API. A knob exists iff declared here and used below. */
        --hue: 38.8;
        --lift: 0;
        --chroma: 1;
        --speed: 1;

        /* Accents — derived from the knobs with the family's offsets baked in,
           so one hue roll moves them together. Defaults reproduce the original hex. */
        --clay: oklch(calc(0.6724 + var(--lift)) calc(0.1308 * var(--chroma)) var(--hue));
        --clay-d: oklch(calc(0.5797 + var(--lift)) calc(0.127 * var(--chroma)) var(--hue));
        --olive: oklch(
          calc(0.6118 + var(--lift)) calc(0.0713 * var(--chroma)) calc(var(--hue) + 88.3)
        );
        --rust: oklch(
          calc(0.5408 + var(--lift)) calc(0.1357 * var(--chroma)) calc(var(--hue) - 10.3)
        );

        /* Clock — 3.6s, so 1% is 36ms. The pointer crosses into the pull zone
           at 20.8%, onto the face at 29.9%, off it at 58.5%, out of the zone at
           61.6%, and the button is home 280ms later at 69.4%. */
        --cycle: 3.6s;
        --t: calc(var(--cycle) / var(--speed));

        --_w: min(260px, 86vw);

        --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
        /* A hand is a sine, not a UI curve: it speeds up and slows down evenly.
           Every crossing time above was solved against this curve. */
        --_hand: cubic-bezier(0.45, 0, 0.55, 1);
      }

      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
        background: var(--ivory);
        font-family:
          system-ui,
          -apple-system,
          'Segoe UI',
          Roboto,
          sans-serif;
        color: var(--slate);
      }

      .stage {
        position: relative;
        width: var(--_w);
        height: 170px;
      }

      /* The pull zone, drawn. In production it is invisible and twice the
         button's diameter; here it shows only while the pointer is inside it,
         because otherwise the button appears to move before anything touched it. */
      .zone {
        position: absolute;
        left: calc(50% - 56px);
        top: 29px;
        width: 112px;
        height: 112px;
        border-radius: 50%;
        border: 1px dashed color-mix(in srgb, var(--slate) 22%, transparent);
        opacity: 0;
        animation: zone var(--t) var(--ease-out) infinite;
      }

      .mag {
        position: absolute;
        left: calc(50% - 28px);
        top: 57px;
        width: 56px;
        height: 56px;
        animation: pull var(--t) infinite;
      }

      .face {
        display: grid;
        place-items: center;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: var(--clay);
        box-shadow: 0 4px 12px color-mix(in srgb, var(--slate) 18%, transparent);
        animation: tint var(--t) ease infinite;
      }

      /* The glyph rides half again as far as the shell on the same beats, so
         the button reads as having depth — the front of it leans further than
         the back. Same keyframe times, so the two can never disagree. */
      .glyph {
        width: 22px;
        height: 22px;
        fill: none;
        stroke: var(--ivory);
        stroke-width: 2;
        stroke-linecap: round;
        stroke-linejoin: round;
        animation: lean var(--t) infinite;
      }

      .ghost {
        position: absolute;
        left: 0;
        top: 0;
        width: 14px;
        height: 18px;
        opacity: 0;
        transform: translate(calc(var(--_w) * 0.12), 150px);
        animation:
          travel var(--t) var(--_hand) infinite,
          presence var(--t) var(--ease-out) infinite;
      }

      .arrow {
        display: block;
        width: 100%;
        height: 100%;
        fill: var(--slate);
        stroke: var(--ivory);
        stroke-width: 1.4;
        stroke-linejoin: round;
      }

      @keyframes travel {
        0%,
        9.8% {
          transform: translate(calc(var(--_w) * 0.12), 150px);
        }
        26.9% {
          transform: translate(calc(var(--_w) * 0.5 - 26px), 104px);
        }
        44% {
          transform: translate(calc(var(--_w) * 0.5 + 22px), 76px);
        }
        56.2% {
          transform: translate(calc(var(--_w) * 0.5 + 6px), 64px);
        }
        68.4%,
        80.7% {
          transform: translate(calc(var(--_w) * 0.86), 26px);
        }
        81.9%,
        100% {
          transform: translate(calc(var(--_w) * 0.12), 150px);
        }
      }

      @keyframes presence {
        0% {
          opacity: 0;
        }
        4.9%,
        75.8% {
          opacity: 1;
        }
        80.7%,
        100% {
          opacity: 0;
        }
      }

      /* A third of the pointer's offset from centre, capped at eight pixels —
         the tether. Uncapped, a button that tracks the hand that closely stops
         being a target and becomes something you chase. It trails the pointer
         by ~90ms on ease-out, so it is answering the hand rather than predicting it.
         Release is a plain 280ms ease-out home: a spring here overshoots by a
         pixel nobody sees, and the cap is the physics that matters.
         Curves are literal in mixed-curve keyframes — a var() there is ignored. */
      @keyframes pull {
        0%,
        20.8% {
          transform: translate(0, 0);
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        29.3% {
          transform: translate(-6.5px, 4.5px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        46.4% {
          transform: translate(6.5px, -2.5px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        58.7% {
          transform: translate(2.5px, -6.5px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        61.6% {
          transform: translate(4px, -7px);
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        69.4%,
        100% {
          transform: translate(0, 0);
        }
      }

      @keyframes lean {
        0%,
        20.8% {
          transform: translate(0, 0);
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        29.3% {
          transform: translate(-3.25px, 2.25px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        46.4% {
          transform: translate(3.25px, -1.25px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        58.7% {
          transform: translate(1.25px, -3.25px);
          animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
        }
        61.6% {
          transform: translate(2.0px, -3.5px);
          animation-timing-function: cubic-bezier(0.23, 1, 0.32, 1);
        }
        69.4%,
        100% {
          transform: translate(0, 0);
        }
      }

      /* The colour belongs to the face, not the zone: being near the button
         earns the lean, being on it earns the hover state. Two thresholds,
         two signals, so a pointer passing close by never claims it was over. */
      @keyframes tint {
        0%,
        29.9% {
          background: var(--clay);
          animation-timing-function: ease;
        }
        34.2%,
        58.5% {
          background: var(--clay-d);
          animation-timing-function: ease;
        }
        62.3%,
        100% {
          background: var(--clay);
        }
      }

      @keyframes zone {
        0%,
        20.8% {
          opacity: 0;
        }
        25.7%,
        61.6% {
          opacity: 1;
        }
        66%,
        100% {
          opacity: 0;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* Nothing leans and the pointer parks on the face. What survives is the
           state, not the physics: the zone still lights and the face still
           darkens on the same beats, a little over half as often. */
        .mag,
        .glyph {
          animation: none;
        }

        .ghost {
          transform: translate(calc(var(--_w) * 0.5 + 6px), 80px);
          animation: parked calc(var(--t) * 1.6) var(--ease-out) infinite;
        }

        /* The parked pointer is only shown while the face is tinted, so the
           hover state never sits there without its cause, or outlives it. */
        @keyframes parked {
          0%,
          25.1% {
            opacity: 0;
          }
          29.9%,
          58.5% {
            opacity: 1;
          }
          62.3%,
          100% {
            opacity: 0;
          }
        }

        .face {
          animation-duration: calc(var(--t) * 1.6);
        }

        .zone {
          animation-duration: calc(var(--t) * 1.6);
        }
      }
    </style>
  </head>
  <body>
    <div
      class="stage"
      role="img"
      aria-label="A pointer drifts toward a round button; once inside a pull zone twice the button's size, the button leans a few pixels toward it and darkens when the pointer is on it; when the pointer leaves, the button springs back to its seat with one small overshoot"
    >
      <div class="zone"></div>
      <div class="mag">
        <div class="face">
          <svg class="glyph" viewBox="0 0 24 24" aria-hidden="true">
            <path d="M5 12h14M13 6l6 6-6 6" />
          </svg>
        </div>
      </div>

      <span class="ghost">
        <svg class="arrow" viewBox="0 0 14 18">
          <path d="M1 1 L1 14.8 L4.5 11.6 L6.7 16.6 L9.3 15.4 L7.1 10.5 L11.8 10.3 Z" />
        </svg>
      </span>
    </div>
  </body>
</html>