全部效果悬停与跟随

悬停字符解码

Hover Scramble Decode

字符短暂轮换成随机字形,然后从左到右依次恢复为原文。

移动指针、点按或使用键盘

适用场景

适用于科技风标题或标签的短时点缀。 适合读者可以稍等片刻的短标签,并让字符尽快稳定。辅助技术应能取得真实的项目名。

应用示例

电子创客社团的项目画廊中,指针经过某张项目卡片时,标签字符快速变换,随后逐个定格成「信号地图」。链接位置不受影响。

AI 提示词

给【目标位置】添加悬停字符解码。按字形拆分原文,并在限定时长内逐个恢复。辅助技术始终读取原文,重播时取消上一轮动画。保留键盘操作,并提供减少动态效果时的状态。

示例代码

保存为 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;
    }

    .pointer-stage {
      --x: 50%; --y: 50%; --dx: 0; --dy: 0;
      display: grid; place-items: center; position: relative;
      width: min(100%, 280px); height: clamp(106px, 68vh, 174px);
      overflow: hidden; isolation: isolate;
      border: 1px solid #b8a8ff70; border-radius: 13px;
      background: #23213e; color: #fcf9ff; cursor: pointer;
      touch-action: manipulation;
    }
    .pointer-stage:focus-visible { outline-offset: 3px; }
    .pointer-stage .tag {
      position: absolute; z-index: 4; bottom: 7px; left: 8px; color: #fff;
      background: #17152fe8; border-radius: 5px; padding: 3px 7px;
      font-size: 10px; line-height: 1.4;
    }

    .pointer-stage { background:repeating-linear-gradient(0deg,#0000 0 4px,#9fe4d11c 5px),linear-gradient(140deg,#113f46,#15192e); }
    .cipher { font:650 clamp(19px,10vw,30px) ui-monospace,monospace; letter-spacing:.1em; color:#a2f3d4; text-shadow:0 0 13px #82f1bd8c; }

    @media (prefers-reduced-motion: reduce) {
      .lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }
  </style>
</head>
<body>
  <main class="demo">
    <button type="button" aria-label="解码字符" aria-pressed="false" class="lab pointer-stage"><span class="cipher" aria-hidden="true">夜色降临</span></button>
  </main>
  <script>
    const stage = document.querySelector('.pointer-stage');
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
    function setPosition(x, y) {
      stage.style.setProperty('--x', (x * 100) + '%');
      stage.style.setProperty('--y', (y * 100) + '%');
      stage.style.setProperty('--dx', 2 * x - 1);
      stage.style.setProperty('--dy', 2 * y - 1);
    }
    function position(event) {
      const bounds = stage.getBoundingClientRect();
      return [clamp((event.clientX - bounds.left) / bounds.width, 0, 1), clamp((event.clientY - bounds.top) / bounds.height, 0, 1)];
    }
    stage.addEventListener('pointermove', (event) => {
      if (event.pointerType !== 'touch' && !reduced.matches) setPosition(...position(event));
    });
    stage.addEventListener('pointerleave', () => {
      if (stage.getAttribute('aria-pressed') !== 'true') setPosition(.5, .5);
    });
    stage.addEventListener('click', (event) => {
      const active = stage.getAttribute('aria-pressed') !== 'true';
      stage.setAttribute('aria-pressed', String(active));
      setPosition(...(active && event.detail ? position(event) : active ? [.68, .38] : [.5, .5]));
    });

    const glyphs = Array.from("夜色降临");
    const pool = Array.from("云光码星夜景水山");
    const cipher = stage.querySelector('.cipher');
    let scrambleFrame = 0;
    function decode() {
      cancelAnimationFrame(scrambleFrame);
      if (reduced.matches) { cipher.textContent = glyphs.join(''); return; }
      const start = performance.now();
      function advance(now) {
        const elapsed = now - start;
        const settled = Math.floor(elapsed / (650 / glyphs.length));
        const tick = Math.floor(elapsed / 44);
        cipher.textContent = glyphs.map((glyph, index) => index < settled ? glyph : pool[(tick * 3 + index * 5) % pool.length]).join('');
        if (settled < glyphs.length) scrambleFrame = requestAnimationFrame(advance);
      }
      scrambleFrame = requestAnimationFrame(advance);
    }
    stage.addEventListener('pointerenter', (event) => { if (event.pointerType !== 'touch') decode(); });
    stage.addEventListener('focus', () => { if (stage.matches(':focus-visible')) decode(); });
    stage.addEventListener('click', decode);
  </script>
</body>
</html>

UI Skills 原创示例

相关参考