LESSON / 35
关键帧:定义一段时间里的变化
@keyframes 描述 0% 到 100% 的形态;animation 决定播放多久、几次、往哪边、结束后停在哪。
1. 四个基础动画
勾选「暂停」可以冻结当前帧,方便观察。
pulse
bounce
spin
shake
2. 多阶段关键帧
小球按 0/25/50/75/100% 走一个方框,同时改变颜色。
@keyframes orbit {
0% { transform: translate(0, 0); background: #2563eb; }
25% { transform: translate(160px, 0); background: #7c3aed; }
50% { transform: translate(160px, 120px); background: #db2777; }
75% { transform: translate(0, 120px); background: #f59e0b; }
100% { transform: translate(0, 0); background: #2563eb; }
}
3. 方向与填充
同一个进度条:normal、reverse、alternate。
4. 动画属性
| 属性 | 作用 | 本页值 |
|---|---|---|
animation-name | 关键帧名称 | pulse |
animation-duration | 一轮时长 | 1.6s |
animation-iteration-count | 播放次数 | infinite |
animation-direction | 方向 | alternate |
animation-fill-mode | 前后状态 | both |
animation-play-state | 播放/暂停 | running |
5. 注意
- 关键帧里的属性要能插值;`display` 不行。
- 无限动画只用于装饰;不要让用户等它结束。
- `prefers-reduced-motion: reduce` 下停止动画。
- 动画不改变布局;需要占位时用静态元素。