全部效果自动播放与循环

星点闪烁

Twinkling Stars

静止背景上的多个小光点以不同的节奏亮起、变暗。

播放或暂停演示

适用场景

给太空主题插画或庆祝界面加上轻微的背景动态。 标题附近的星点可以更淡、变化更少,让闪烁只给画面增加夜空氛围。

应用示例

天文馆的夜场预告卡在插画四周布置几颗明暗变化的星点。节目时间和购票按钮则放在画面中安静的一侧。

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;
    }

    .star-field {
      position: relative; width: min(100%, 235px); height: 96px; border-radius: 14px;
      background: radial-gradient(ellipse at 75% 95%, #4b2e60, #1b1939 68%);
    }
    .star-field i {
      --x: 19%; --y: 30%; position: absolute; left: var(--x); top: var(--y); width: 9px; aspect-ratio: 1;
      background: #ffdca1; box-shadow: 0 0 9px #ffdca1; rotate: 45deg; opacity: .73;
      animation: twinkle 2.4s ease-in-out infinite alternate;
    }
    .star-field i:nth-child(2) { --x: 43%; --y: 17%; width: 5px; animation-delay: -.52s; }
    .star-field i:nth-child(3) { --x: 72%; --y: 47%; background: #dac8ff; animation-delay: -1.2s; }
    .star-field i:nth-child(4) { --x: 25%; --y: 71%; width: 4px; animation-delay: -.2s; }
    .star-field i:nth-child(5) { --x: 61%; --y: 74%; width: 4px; animation-delay: -1.1s; }
    .star-field i:nth-child(6) { --x: 86%; --y: 23%; width: 6px; animation-delay: -1.65s; }
    .star-field i:nth-child(7) { --x: 47%; --y: 49%; width: 5px; animation-delay: -1.83s; }
    @keyframes twinkle { from { opacity: .29; transform: scale(.7); } to { opacity: .94; transform: scale(1.16); } }

    .motion-example { display: grid; justify-items: center; width: 100%; gap: 10px; }
    .motion-specimen {
      display: grid;
      place-items: center;
      width: min(100%, 295px);
      height: 108px;
      position: relative;
      isolation: isolate;
    }
    .motion-example [data-motion] { animation-play-state: paused; }
    .motion-example[data-playing] [data-motion] { animation-play-state: running; }
    @media (prefers-reduced-motion: reduce) {
      .motion-example [data-motion] { animation: none; }
      .motion-example [data-loop-toggle] { display: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="motion-example">
      <div class="motion-specimen" aria-hidden="true"><div class="star-field"><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i><i data-motion></i></div></div>
      <button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
    </div>
  </main>
  <script>
    const example = document.querySelector('.motion-example');
    const toggle = example.querySelector('[data-loop-toggle]');

    toggle.addEventListener('click', () => {
      const playing = example.toggleAttribute('data-playing');
      toggle.textContent = playing ? "暂停演示" : "播放演示";
    });
  </script>
</body>
</html>

UI Skills 原创示例