# 星空粒子飘动（Starfield particles）

- 俗称：星空背景粒子漂浮；深色背景星点闪烁；轻量背景颗粒飘动；只用CSS的星空装饰
- 用在：背景（品牌官网、个人网站、工具和应用）
- 触发：一直循环
- 感觉：柔和、科技感
- 版本：1，更新于 2026-11-17

## 原理

在隔离层叠上下文的背景容器里放置多颗带 aria-hidden="true" 和 pointer-events: none 的小圆点，通过 CSS 变量设置飘动时长和幅度，用 @keyframes 仅驱动 translate 与 opacity 两个合成层属性，各颗星按比例错开 animation-delay，避免重排且不引入 JS/canvas 开销；手机端自动减少星点数量，减少动态时保持静止。

适合：深色首屏背景、AI 产品宣传区、极客个人主页，在文字下方营造静谧的太空星点氛围。

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `duration` | 一圈时长 | 5000ms | 2000–12000ms |
| `distance` | 飘动幅度 | 10px | 4–28px |

## 质量要求

- 开启"减少动态"时：关闭飘动和明暗呼吸动画，星点静止在原处作为静态底纹。
- 手机上：窄屏下自动隐藏部分星点（减少颗粒密度），仅动合成层属性保持流畅。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

## 提示词（可以直接交给 AI）

```text
请在我的项目里给背景加上「星空粒子飘动」（Starfield particles）效果：在隔离层叠上下文的背景容器里放置多颗带 aria-hidden="true" 和 pointer-events: none 的小圆点，通过 CSS 变量设置飘动时长和幅度，用 @keyframes 仅驱动 translate 与 opacity 两个合成层属性，各颗星按比例错开 animation-delay，避免重排且不引入 JS/canvas 开销；手机端自动减少星点数量，减少动态时保持静止。
参数：一圈时长 5000ms，飘动幅度 10px。
要求：保留原有的内容和布局；系统开启"减少动态"时直接显示最终状态；脚本出错或没有执行时内容仍然可见。
参考：https://donglang.linter.top/effects/starfield.md（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

```html
<section class="stf" style="padding:48px;background:#141519;color:#ffffff;border-radius:14px;">
  <span class="stf-dots" aria-hidden="true">
    <i></i><i></i><i></i><i></i><i></i><i></i>
  </span>
  <h2>星空背景区</h2>
  <p>细小星点在文字下方缓缓飘动。</p>
</section>
```

CSS：

```css
/* 星空粒子飘动（Starfield particles）· 只用 CSS，不需要脚本。 */
:where(.stf) {
  --stf-duration: 5000ms;
  --stf-distance: 10px;
  --stf-color: currentColor;
  position: relative;
}

.stf {
  isolation: isolate;
}

.stf-dots {
  position: absolute;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}

.stf-dots > i {
  position: absolute;
  border-radius: 50%;
  background: var(--stf-color);
  animation: stf-drift var(--stf-duration) ease-in-out infinite;
}

.stf-dots > i:nth-child(2) { animation-delay: calc(var(--stf-duration) * 0.16); }
.stf-dots > i:nth-child(3) { animation-delay: calc(var(--stf-duration) * 0.32); }
.stf-dots > i:nth-child(4) { animation-delay: calc(var(--stf-duration) * 0.48); }
.stf-dots > i:nth-child(5) { animation-delay: calc(var(--stf-duration) * 0.64); }
.stf-dots > i:nth-child(6) { animation-delay: calc(var(--stf-duration) * 0.80); }

:where(.stf-dots > i:nth-child(1)) { left: 12%; top: 24%; width: 4px; height: 4px; }
:where(.stf-dots > i:nth-child(2)) { left: 28%; top: 72%; width: 5px; height: 5px; }
:where(.stf-dots > i:nth-child(3)) { left: 48%; top: 18%; width: 3px; height: 3px; }
:where(.stf-dots > i:nth-child(4)) { left: 68%; top: 68%; width: 5px; height: 5px; }
:where(.stf-dots > i:nth-child(5)) { left: 82%; top: 28%; width: 4px; height: 4px; }
:where(.stf-dots > i:nth-child(6)) { left: 90%; top: 78%; width: 3px; height: 3px; }

@keyframes stf-drift {
  0%, 100% {
    translate: 0 0;
    opacity: 0.35;
  }
  50% {
    translate: 0 calc(var(--stf-distance) * -1);
    opacity: 0.95;
  }
}

@media (max-width: 720px) {
  .stf-dots > i:nth-child(n + 5) {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .stf-dots > i {
    animation: none;
    opacity: 0.5;
  }
}
```

不需要 JS：这个效果只用 CSS（参数写在 CSS 变量里）。

## 出处

- 第四批备选（竞品 2 家收录），本站自研纯 CSS 合成层星点飘动，不依赖 canvas 与脚本
