全部效果点击与切换

圆形揭幕切换主题

Circular Theme Reveal

新主题从点击位置开始向外扩散,切换回去时圆形遮罩收拢,露出原主题。

点击右上角的太阳按钮

适用场景

用于仪表板或设计工具的主题切换,让用户看到新配色从控件所在位置展开。 需要大范围更换配色时,让新主题从实际操作点展开,用户能直观看出变化从哪里开始。

应用示例

配色工具把太阳图标放在画布旁。点一下,浅色圆从图标位置扩散到整个工作区。切回时,圆收拢,露出先前的配色。

AI 提示词

给【主题切换按钮】加上圆形揭幕效果。在受影响区域叠放两套主题,用 clip-path: circle() 对上层做遮罩。圆心取点击坐标或键盘激活时的按钮中心,半径延伸到最远角。保证切换时的对比度,减少动态效果模式直接应用主题。

示例代码

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

    .theme-scene {
      width: min(100%, 282px); height: 138px; position: relative; isolation: isolate;
      border-radius: 14px; overflow: hidden; border: 1px solid #75648f;
    }
    .theme-face {
      position: absolute; inset: 0; padding: 11px 12px;
      color: var(--theme-ink); background: var(--theme-bg);
    }
    .theme-dark { --theme-ink: #f0eaff; --theme-bg: #27224a; --theme-bar: #9987f2; --theme-line: #594b7f; --theme-green: #d0ecad; }
    .theme-light {
      --theme-ink: #36234d; --theme-bg: #f3ebff; --theme-bar: #815fc5; --theme-line: #d9c8ed; --theme-green: #3a7525;
      clip-path: circle(0px at 92% 16%);
    }
    .theme-metric { margin: 0; font-size: 10px; font-weight: 650; }
    .theme-number { margin: 5px 0 0; font-size: 23px; line-height: 1; font-weight: 760; }
    .theme-number small { font-size: 10px; margin-left: 3px; color: var(--theme-green); }
    .theme-chart {
      height: 57px; margin-top: 11px; display: grid; grid-template-columns: repeat(11, 1fr);
      align-items: end; gap: 4px; border-bottom: 1px solid var(--theme-line);
      background: repeating-linear-gradient(0deg, transparent 0 17px, var(--theme-line) 17px 18px);
    }
    .theme-chart span {
      display: block; height: var(--bar); border-radius: 3px 3px 0 0;
      background: linear-gradient(0deg, color-mix(in srgb, var(--theme-bar), transparent 67%), var(--theme-bar));
    }
    .theme-toggle {
      position: absolute; right: 9px; top: 7px; z-index: 1; width: 31px; height: 31px;
      display: grid; place-items: center; color: #f5edff; background: #3a305b;
      border-radius: 50%; border: 1px solid #a390ca; cursor: pointer; font-size: 18px;
    }
    .theme-toggle .theme-moon, .theme-scene[data-light="true"] .theme-sun { display: none; }
    .theme-scene[data-light="true"] .theme-moon { display: inline; }
    .theme-scene[data-light="true"] .theme-toggle { color: #513470; background: #e9dbfb; border-color: #a789c3; }
  </style>
</head>
<body>
  <main class="demo">
    <div class="theme-scene" data-light="false">
      <div class="theme-face theme-dark" aria-hidden="true">
      <p class="theme-metric">设计订阅</p>
      <p class="theme-number">1,284 <small>+18%</small></p>
      <div class="theme-chart">
        <span style="--bar: 29%"></span><span style="--bar: 38%"></span><span style="--bar: 32%"></span><span style="--bar: 49%"></span><span style="--bar: 45%"></span><span style="--bar: 57%"></span><span style="--bar: 61%"></span><span style="--bar: 70%"></span><span style="--bar: 65%"></span><span style="--bar: 79%"></span><span style="--bar: 82%"></span>
      </div>
    </div>
      <div class="theme-face theme-light" aria-hidden="true">
      <p class="theme-metric">设计订阅</p>
      <p class="theme-number">1,284 <small>+18%</small></p>
      <div class="theme-chart">
        <span style="--bar: 29%"></span><span style="--bar: 38%"></span><span style="--bar: 32%"></span><span style="--bar: 49%"></span><span style="--bar: 45%"></span><span style="--bar: 57%"></span><span style="--bar: 61%"></span><span style="--bar: 70%"></span><span style="--bar: 65%"></span><span style="--bar: 79%"></span><span style="--bar: 82%"></span>
      </div>
    </div>
      <button class="theme-toggle" type="button" aria-label="浅色主题" aria-pressed="false" data-testid="circular-theme-toggle">
        <span aria-hidden="true" class="theme-sun">☼</span><span aria-hidden="true" class="theme-moon">☾</span>
      </button>
    </div>
  </main>
  <script>
    const scene = document.querySelector('.theme-scene');
    const layer = scene.querySelector('.theme-light');
    const button = scene.querySelector('.theme-toggle');
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    let animation;

    button.addEventListener('click', (event) => {
      const bounds = layer.getBoundingClientRect();
      const control = button.getBoundingClientRect();
      const x = (event.detail ? event.clientX : control.left + control.width / 2) - bounds.left;
      const y = (event.detail ? event.clientY : control.top + control.height / 2) - bounds.top;
      const radius = Math.ceil(Math.hypot(Math.max(x, bounds.width - x), Math.max(y, bounds.height - y)));
      const isLight = scene.dataset.light === 'false';
      const finalClip = 'circle(' + (isLight ? radius : 0) + 'px at ' + x + 'px ' + y + 'px)';
      const currentClip = animation?.playState === 'running'
        ? getComputedStyle(layer).clipPath
        : 'circle(' + (isLight ? 0 : radius) + 'px at ' + x + 'px ' + y + 'px)';

      animation?.cancel();
      scene.dataset.light = String(isLight);
      button.setAttribute('aria-pressed', String(isLight));
      layer.style.clipPath = isLight ? 'none' : finalClip;
      if (reduced.matches || typeof layer.animate !== 'function') return;
      animation = layer.animate([{ clipPath: currentClip }, { clipPath: finalClip }], {
        duration: 520, easing: 'cubic-bezier(.24, .72, .27, 1)',
      });
    });
    reduced.addEventListener('change', () => animation?.cancel());
  </script>
</body>
</html>

UI Skills 原创示例