全部效果点击与切换

图标路径变形

SVG Icon Morph

切换状态时,一个图标的 SVG 路径平滑变成另一个图标的形状。

点按按钮切换图标形状

适用场景

在播放控件、工具栏或小型开关中呈现状态切换。 让两个图标的坐标结构与路径段类型对应,并在操作改变时更新控件的无障碍标签。

应用示例

播客播放器把播放三角平滑变成两根暂停竖线,同时更新读屏软件能读到的控件操作。

也叫

  • 路径变形
  • 图标插值

AI 提示词

切换时让【图标 A】和【图标 B】进行 SVG 路径变形。兼容的路径用约 200 毫秒插值,其他路径用交叉淡化。减少动态效果模式下直接切换。

示例代码

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

    .demo {
      background: radial-gradient(ellipse at 50% 15%, #484354, #252333 65%, #1c1b28);
    }
    .morph-toolbar {
      display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px; width: min(100%, 250px);
      padding: 5px; border: 1px solid #716a7c; border-radius: 20px;
      background: #24222e; box-shadow: 0 13px 22px #16132090, inset 0 1px #ffffff14;
    }
    .morph-toggle {
      display: grid; gap: 8px; place-items: center; padding: 16px 8px 12px;
      border: 0; border-radius: 15px; color: #efebfc; background: #393647;
      font-size: 11px; font-weight: 650; cursor: pointer;
    }
    .morph-toggle:first-child { color: #83dcff; }
    .morph-toggle:last-child { color: #ffd197; }
    .morph-toggle:hover, .morph-toggle[aria-pressed="true"] { background: #4b465d; }
    .morph-toggle svg { width: 32px; height: 32px; fill: currentColor; }
    .morph-toggle span { color: #efebfc; }
    @media (min-height: 250px) {
      .morph-toggle { padding-block: 24px; }
      .morph-toggle svg { width: 44px; height: 44px; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="morph-toolbar">
      <button class="morph-toggle" type="button" aria-pressed="false">
        <svg viewBox="0 0 32 32" aria-hidden="true"><path d="M8 5 L17 10 L17 22 L8 27 Z M17 10 L27 16 L27 16 L17 22 Z"></path></svg>
        <span>播放</span>
      </button>
      <button class="morph-toggle" type="button" aria-pressed="false">
        <svg viewBox="0 0 32 32" aria-hidden="true"><path d="M7 14 L14 14 L14 7 L18 7 L18 14 L25 14 L25 18 L18 18 L18 25 L14 25 L14 18 L7 18 Z"></path></svg>
        <span>添加</span>
      </button>
    </div>
  </main>
  <script>
    const pairs = [{"base":"M8 5 L17 10 L17 22 L8 27 Z M17 10 L27 16 L27 16 L17 22 Z","target":"M8 5 L14 5 L14 27 L8 27 Z M19 5 L25 5 L25 27 L19 27 Z","initial":"播放","toggled":"暂停"},{"base":"M7 14 L14 14 L14 7 L18 7 L18 14 L25 14 L25 18 L18 18 L18 25 L14 25 L14 18 L7 18 Z","target":"M5 16 L8 13 L12 17 L14 19 L17 16 L24 9 L27 12 L19 20 L14 25 L11 22 L8 19 L5 16 Z","initial":"添加","toggled":"已添加"}];
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const numbers = /-?[0-9.]+/g;

    document.querySelectorAll('.morph-toggle').forEach((button, index) => {
      const { base, target, initial, toggled } = pairs[index];
      const path = button.querySelector('path');
      const label = button.querySelector('span');
      let frame;
      let fade;
      let pressed = false;

      function animateTo(destination) {
        cancelAnimationFrame(frame);
        fade?.cancel();
        if (reduced.matches) {
          path.setAttribute('d', destination);
          return;
        }

        const startingPath = path.getAttribute('d');
        if (startingPath.replace(numbers, '#') !== destination.replace(numbers, '#')) {
          const outgoing = path.cloneNode();
          path.after(outgoing);
          path.setAttribute('d', destination);
          const exit = outgoing.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 200, fill: 'forwards' });
          exit.finished.then(() => outgoing.remove(), () => outgoing.remove());
          fade = path.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 200 });
          return;
        }

        const start = startingPath.match(numbers).map(Number);
        const end = destination.match(numbers).map(Number);
        const began = performance.now();
        function draw(time) {
          const progress = Math.min((time - began) / 200, 1);
          const eased = progress * progress * (3 - 2 * progress);
          let coordinate = 0;
          path.setAttribute('d', destination.replace(numbers, () => {
            const value = start[coordinate] + (end[coordinate] - start[coordinate]) * eased;
            coordinate += 1;
            return value.toFixed(2);
          }));
          if (progress < 1) frame = requestAnimationFrame(draw);
          else path.setAttribute('d', destination);
        }
        frame = requestAnimationFrame(draw);
      }

      button.addEventListener('click', () => {
        pressed = !pressed;
        button.setAttribute('aria-pressed', String(pressed));
        label.textContent = pressed ? toggled : initial;
        animateTo(pressed ? target : base);
      });
      reduced.addEventListener('change', () => {
        if (reduced.matches) animateTo(pressed ? target : base);
      });
    });
  </script>
</body>
</html>

UI Skills 原创示例