能用的东西
Animated gradient
渐变色流动背景
A soft multi-color gradient background that keeps flowing sideways.
Also searched as: 背景颜色慢慢流动变化, 渐变背景缓缓移动, 首屏背景的颜色在流动, 彩色渐变一直在动的背景
- Placement
- Background
- Mood
- Soft, Refined
- Trigger
- 一直循环
- Site types
- 个人网站, 作品集, 工具站
How it works & accessibility
Tile one gradient twice on a layer twice as wide as the section and translate it left by exactly one tile in a linear loop, so the seam never shows; the layer sits under the content, ignores clicks, and stays still under reduced motion.
Best for: Background (一直循环).
Reduced motion: 不动,留一张静止的渐变
Mobile & touch: 和桌面一样;只动位移,手机上也不卡
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 一圈多久 | duration | 12000ms | — | — |
Ready-to-paste AI prompt
Add “Animated gradient” to the background in my project: Tile one gradient twice on a layer twice as wide as the section and translate it left by exactly one tile in a linear loop, so the seam never shows; the layer sits under the content, ignores clicks, and stays still under reduced motion.
Parameters: duration: 12000ms.
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/animated-gradient.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 渐变色流动背景(Animated gradient)· 只用 CSS,不需要脚本。区块的背景是几种颜色的渐变,颜色一直慢慢地往左流。
参考 StyleKit 的 background-gradient-shift、Animation Handbook 的 animated-gradient-background 的做法(都是 MIT),自己实现,没有复制代码。
它们挪的是背景位置(每一帧都要重画);这里改成挪一层位移:同一段渐变(头尾是同一种颜色)平铺两份,放在比区块宽一倍的一层上,
这一层匀速往左平移正好一份的宽度(自己宽度的 50%),再从头来,首尾画面一模一样、接得上;只动位移,交给显卡。
用法:区块加 class="ag",最前面放 <span class="ag-flow" aria-hidden="true"></span>,内容照常写在后面。
- 渐变层在内容下面(区块是一个独立的层叠上下文),不挡点击(pointer-events: none),区块把伸出去的那一半裁掉(overflow: hidden)。
- 区块要有定位(relative、absolute、sticky 都行):默认的 position: relative 写在 :where() 里,网站自己写的定位优先;
但别在区块上写 position: static(渐变层会跑到更外层的定位祖先里,可能溢出 overflow: hidden)。
- 颜色:--ag-c1、--ag-c2、--ag-c3,选相近、柔和的几种;文字颜色按最深、最浅的那种选,保证对比度够。
- 一圈多久:--ag-duration(默认 12 秒);流得太快会晃眼。
- 开启"减少动态"时:不动,留一张静止的渐变。 */
/* 默认值和定位写在 :where() 里(不占优先级):网站自己在 .ag 上改颜色、时长,或者区块自己写了定位(absolute、sticky 这些),
不管写在这段代码前面还是后面,都用网站自己的 */
:where(.ag) {
--ag-duration: 12000ms;
--ag-c1: #ffd8e4;
--ag-c2: #ffe9c9;
--ag-c3: #d3e5ff;
position: relative;
}
.ag {
overflow: hidden;
isolation: isolate;
}
.ag-flow {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 200%;
z-index: -1;
pointer-events: none;
background: linear-gradient(90deg, var(--ag-c1), var(--ag-c2), var(--ag-c3), var(--ag-c1)) 0 0 / 50% 100% repeat-x;
animation: ag-flow var(--ag-duration) linear infinite;
}
@keyframes ag-flow {
to { translate: -50% 0; }
}
@media (prefers-reduced-motion: reduce) {
.ag-flow { animation: none; }
}HTML
<!-- 渐变色流动背景:区块加 class="ag",最前面放渐变层,内容照常写在后面 -->
<section class="ag">
<span class="ag-flow" aria-hidden="true"></span>
<h1>你好,我是北煜ii</h1>
<p>独立设计师,做网站和小工具</p>
</section>