全部效果悬停与跟随

光点跟随轨迹

Cursor Trail

指针经过的位置留下逐渐消退的光点,展示短暂的移动轨迹。

在区域内移动,或点按留下光点

适用场景

适合绘制区域或探索式功能的短暂指针反馈。 拖动痕迹适合放在鼓励用户连续移动的区域内,持续时间要短。保留普通指针,方便定位。

应用示例

在线写字课的练习板里,指针移动后会留下短暂的点状轨迹。学习者能看到刚才那一笔在哪里拐弯,几秒内痕迹就淡出。

AI 提示词

为【目标位置】添加 Cursor Trail(光点跟随轨迹)。复用一组光点,以有限频率记录指针位置,让每个光点在较短时间内淡出,并在减少动态效果模式下关闭。在减少动态效果模式下保持内容可读,并保留键盘操作。

示例代码

保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。

查看 HTML
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>光点跟随轨迹</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 { min-height: 114px; overflow: hidden; }
    .target > span { pointer-events: none; color: #e2d5f8; }
    .dot { position: absolute; pointer-events: none; width: 9px; height: 9px; border-radius: 50%; background: #ceacff; box-shadow: 0 0 12px #bca5ff; }
    @media (prefers-reduced-motion: reduce) { .dot { display: none; } }
  </style>
</head>
<body>
  <main class="demo">
    <button type="button" class="target" aria-label="光点轨迹"><span>划出轨迹</span></button>
  </main>
  <script>
    const target = document.querySelector('.target');
    const reduce = matchMedia('(prefers-reduced-motion: reduce)');
    const dots = Array.from({ length: 12 }, () => {
      const dot = document.createElement('i');
      dot.className = 'dot';
      dot.hidden = true;
      target.append(dot);
      return dot;
    });
    let index = 0;
    let lastMove = 0;
    function trail(x, y) {
      if (reduce.matches) return;
      const dot = dots[index++ % dots.length];
      dot.getAnimations().forEach((animation) => animation.cancel());
      dot.hidden = false;
      dot.style.left = x + 'px';
      dot.style.top = y + 'px';
      dot.animate(
        [{ opacity: 1, transform: 'translate(-50%,-50%) scale(1)' }, { opacity: 0, transform: 'translate(-50%,-50%) scale(.2)' }],
        { duration: 390, fill: 'forwards' }
      );
    }
    target.addEventListener('pointermove', (event) => {
      const now = performance.now();
      if (now - lastMove < 30) return;
      lastMove = now;
      const bounds = target.getBoundingClientRect();
      trail(event.clientX - bounds.left, event.clientY - bounds.top);
    });
    target.addEventListener('click', (event) => {
      const bounds = target.getBoundingClientRect();
      trail(event.detail ? event.clientX - bounds.left : bounds.width / 2, event.detail ? event.clientY - bounds.top : bounds.height / 2);
    });
  </script>
</body>
</html>

UI Skills 原创示例