# CSS 过渡

CSS 过渡是 CSS 的一项特性，它使属性变化在指定的时间段内逐渐发生，而不是瞬间完成，中间状态由浏览器根据可配置的持续时间、延迟和缓动函数绘制。CSS 过渡在 CSS 标准的 CSS 过渡模块中规定，实践中通过 `transition` 简写属性及其子属性来控制。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 将瞬时的属性变化转变为在设定持续时间内从旧值到新值的逐渐过渡<sup>[1](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions)</sup> |
| 简写语法 | `transition: <property> <duration> <timing-function> <delay>;`<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup> |
| 初始值 | `transition-property: all`、`transition-duration: 0s`、`transition-timing-function: ease`、`transition-delay: 0s`、`transition-behavior: normal`<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup> |
| 浏览器支持 | `transition` 简写属性自 2015 年 9 月起在各浏览器中可用<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup> |
| 起源 | WebKit 于 2007 年提出 CSS 过渡和 CSS 动画；两者后来均成为 W3C 标准<sup>[4](https://webkit.org/blog/10266/web-animations-in-safari-13-1/)</sup> |
| 当前规范 | Level 1 为 W3C 工作草案（2013 年 11 月 19 日草案）；Level 2 新增 `transition-behavior` 和 `@starting-style`，于 2023 年 9 月作为首次公开工作草案发布<sup>[5](https://www.w3.org/standards/history/css-transitions-1/)</sup><sup> • </sup><sup>[6](https://www.w3.org/TR/2023/WD-css-transitions-2-20230905/)</sup> |
| 性能指导 | 仅对 `opacity` 和 `transform` 做动画，可让浏览器在合成器线程上运行动画，脱离主线程<sup>[7](https://web.dev/articles/animations-and-performance?authuser=63)</sup> |

## 过渡的工作原理

当 CSS 属性的计算值发生变化时，浏览器通常立即应用新值。若为该属性配置了过渡，浏览器则在指定持续时间内对起始值和结束值进行插值。这种两个状态之间的过渡通常被称为隐式过渡，因为起始与结束之间的状态由浏览器隐式定义。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>

CSS 过渡 Level 1 规范以样式变更事件来定义其机制。在每个样式变更事件中，实现将变更前样式（截至上一事件的计算值）与变更后样式进行比较，并对每个值发生变化且被过渡属性覆盖的属性启动过渡。过渡运行期间，它向 CSS 层叠贡献一个当前值；在延迟阶段，该值计算为起始值；在持续阶段，该值计算为起始值 \( V_{a} \) 与结束值 \( V_{b} \) 的插值，其中进度 \( p \) 取自时间函数对 \( (\text{当前时间} - \text{起始时间}) / (\text{结束时间} - \text{起始时间}) \) 的输出。<sup>[8](https://www.w3.org/TR/css-transitions/)</sup>

过渡由多种状态变化触发：`:hover` 或 `:active` 等伪类、通过 [JavaScript](https://www.edgepedia.org/zh/javascript) 进行的类或样式更改，以及媒体查询变化。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup>

## 过渡属性

四个子属性，加上 Level 2 中新增的第五个，共同控制过渡：<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>

- `transition-property` 指定要进行动画处理的属性。只有列出的属性才会进行动画处理；其他属性的变化保持瞬时。特殊值 `all` 覆盖所有发生变化的属性。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>
- `transition-duration` 设定变化持续多长时间。单一持续时间适用于所有列出的属性，或用逗号分隔的列表为每个属性指定各自的持续时间。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>
- `transition-timing-function` 定义中间值的计算方式，通常为由四个控制点描述的三次[贝塞尔曲线](https://www.edgepedia.org/zh/bezier-curve)；`ease`、`linear`、`ease-in`、`ease-out` 等关键字映射到常见曲线。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>
- `transition-delay` 设定属性变化与过渡开始之间等待多长时间。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>
- `transition-behavior` 在 Level 2 中新增，取关键字 `allow-discrete` 以允许 `display` 等离散动画属性的过渡。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup>

简写按属性、持续时间、时间函数、延迟的顺序组合它们。当给出两个时间值时，第一个赋给持续时间，第二个赋给延迟。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup><sup> • </sup><sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup> 每个子属性接受逗号分隔的列表，因此单条规则可以为不同属性指定不同持续时间；在 `transition: all 200ms, opacity 400ms` 中，除 `opacity` 耗时 400 毫秒外，所有变化的属性均在 200 毫秒内过渡。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)</sup>

并非所有属性都可过渡。有些属性完全不可动画，规范建议不要进行以 `auto` 值为起点或终点的动画过渡，因为根据浏览器不同可能产生不可预测的结果。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup>

## 事件

过渡的创建、开始、完成和取消会生成 DOM 事件，每个正在过渡的属性各一个，每个事件携带属性名和过渡持续时间。`transitionend` 事件让脚本检测过渡已完成。如果过渡在完成前被中止，例如因为元素被设为 `display: none` 或动画属性的值再次变化，则该事件不会触发。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)</sup><sup> • </sup><sup>[8](https://www.w3.org/TR/css-transitions/)</sup>

## 进入和退出动画：@starting-style 与 transition-behavior

默认情况下，过渡不会在元素的首次样式更新时启动，因此新插入的元素，或从 `display: none` 变为显示值的元素，没有可供过渡的前一样式。CSS 过渡 Level 2 通过两项特性解决此问题。<sup>[6](https://www.w3.org/TR/2023/WD-css-transitions-2-20230905/)</sup>

`@starting-style` @ 规则定义在上一次样式变更事件未建立变更前样式时用于过渡的起始值。例如，一个带有 `transition: background-color 1.5s` 和 `background-color: green` 的 `h1` 可以声明 `@starting-style { h1 { background-color: transparent; } }`，使其首次渲染时从透明淡入到绿色。<sup>[6](https://www.w3.org/TR/2023/WD-css-transitions-2-20230905/)</sup>

`transition-behavior: allow-discrete` 值使 `display` 等离散动画属性可过渡；当此类值进行过渡时，它们在 50% 进度处翻转，这允许在 `display` 从 `block` 翻转为 `none` 之前运行 `opacity` 淡出。<sup>[6](https://www.w3.org/TR/2023/WD-css-transitions-2-20230905/)</sup> `transition-behavior` 本身在 Chrome 117、Safari 17.4 和 Firefox 129 中获得支持，而专门对 `display` 的过渡在 Chrome 117 和 Safari 18 中受支持，Firefox 尚不支持。<sup>[9](https://github.com/mdn/browser-compat-data/blob/main/css/properties/transition-behavior.json)</sup>

## 性能

对属性做动画的开销取决于具体是哪个属性。触发布局或绘制的属性会强制在主线程上工作，样式计算、布局、绘制和 JavaScript 都在主线程上运行；为保持滚动和动画流畅，主线程上的所有工作应在 60 Hz 刷新率下每帧约 16.67 毫秒内完成。<sup>[7](https://web.dev/articles/animations-and-performance?authuser=63)</sup><sup> • </sup><sup>[10](https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/How_browsers_work)</sup> 将动画限制为 `opacity` 和 `transform` 可使浏览器在合成器线程上处理它们，因此即使主线程忙于高开销任务，它们仍能继续运行。<sup>[7](https://web.dev/articles/animations-and-performance?authuser=63)</sup> `will-change` 属性告知浏览器在变化发生前预先准备优化，但过度使用会浪费资源。<sup>[7](https://web.dev/articles/animations-and-performance?authuser=63)</sup>

## 无障碍

部分用户会因动画内容感到分心或恶心；由交互触发的运动可能引发前庭功能障碍。`prefers-reduced-motion` 媒体查询报告用户的系统级运动偏好，W3C 的 WCAG 技术 SCR40 描述了在 JavaScript 中评估它以抑制运动动画；这与成功标准 2.3.3“交互触发的动画”相关。<sup>[11](https://www.w3.org/WAI/WCAG21/Techniques/client-side-script/SCR40)</sup> 在 CSS 中，常见模式是将产生运动的过渡包裹在 `@media (prefers-reduced-motion: reduce)` 中并减少或移除它们，保留透明度淡入淡出和颜色过渡作为大幅位移的更安全替代。<sup>[12](https://www.uiuxatlas.com/lessons/motion/prefers-reduced-motion-and-motion-accessibility/)</sup>

## 与其他动画工具的关系

CSS 过渡处理两个状态之间的变化；CSS 动画处理用 `@keyframes` 定义的多关键帧动画。两者都是 Web Animations 模型之上的声明式层：在 WebKit 中，CSS 过渡和 CSS 动画在内部表示为 Web Animations 对象，Web Animations API 的 `getAnimations()` 方法返回一个 `CSSTransition` 对象，脚本可以像对待用 `element.animate()` 创建的任何动画一样对其进行定位、暂停或控制。<sup>[13](https://webkit.org/blog/8343/web-animations-in-webkit/)</sup> 对于 JavaScript 驱动的工作，Web Animations API 避免了在动态创建过渡之前强制样式失效以设置起始值和结束值的变通做法。<sup>[13](https://webkit.org/blog/8343/web-animations-in-webkit/)</sup>

## References

1. [CSS transitions – CSS | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions)
2. [Using CSS transitions – CSS | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Transitions/Using)
3. [transition CSS property – CSS | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/transition)
4. [Web Animations in Safari 13.1 – WebKit](https://webkit.org/blog/10266/web-animations-in-safari-13-1/)
5. [CSS Transitions Module Level 1 publication history – W3C](https://www.w3.org/standards/history/css-transitions-1/)
6. [CSS Transitions Level 2 – W3C First Public Working Draft, 5 September 2023](https://www.w3.org/TR/2023/WD-css-transitions-2-20230905/)
7. [Animations and performance – web.dev](https://web.dev/articles/animations-and-performance?authuser=63)
8. [CSS Transitions Module Level 1 – W3C](https://www.w3.org/TR/css-transitions/)
9. [mdn/browser-compat-data: transition-behavior.json](https://github.com/mdn/browser-compat-data/blob/main/css/properties/transition-behavior.json)
10. [Populating the page: how browsers work – MDN](https://developer.mozilla.org/en-US/docs/Web/Performance/Guides/How_browsers_work)
11. [SCR40: Using the CSS prefers-reduced-motion query in JavaScript to prevent motion – W3C WAI](https://www.w3.org/WAI/WCAG21/Techniques/client-side-script/SCR40)
12. [prefers-reduced-motion & Motion Accessibility – UI/UX Atlas](https://www.uiuxatlas.com/lessons/motion/prefers-reduced-motion-and-motion-accessibility/)
13. [Web Animations in WebKit – WebKit](https://webkit.org/blog/8343/web-animations-in-webkit/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › CSS layout and styling features

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/css-transitions

English original: https://www.edgepedia.org/css-transitions
