transition-duration用于设置CSS过渡效果的持续时间,取值为秒(s)或毫秒(ms),常与transition-property和timing-function配合使用,可简写为transition。例如:button{transition:background-color 0.3s ease;}实现背景色0.3秒平滑变化。支持为多个属性设置不同持续时间,如width 0.5s、height 0.8s。需配合:hover等触发条件生效,默认值为0s,仅适用于可渐变属性如颜色、尺寸等,非连续属性如display不支持过渡。合理使用可提升界面交互流畅性。

在CSS中,transition-duration 属性用于定义元素从一种样式过渡到另一种样式所花费的时间。换句话说,它控制动画的持续时间。如果希望某个属性变化(比如颜色、宽度、位置等)不是立即完成,而是平滑地进行,就可以使用这个属性。
transition-duration: 0.5s; transition-duration: 200ms; transition-duration: 1s;
例如,让一个按钮的背景色在 0.3 秒内平滑变化:
button {
background-color: blue;
transition-property: background-color;
transition-duration: 0.3s;
transition-timing-function: ease;
}
<p>button:hover {
background-color: red;
}</p>更简洁的写法是使用 transition 简写:
立即学习“前端免费学习笔记(深入)”;
button {
background-color: blue;
transition: background-color 0.3s ease;
}
<p>button:hover {
background-color: red;
}</p>例如,宽度变化用 0.5 秒,高度用 0.8 秒:
.box {
width: 100px;
height: 100px;
transition-property: width, height;
transition-duration: 0.5s, 0.8s;
}
<p>.box:hover {
width: 200px;
height: 150px;
}</p>transition-duration 默认值是 0s,意味着没有延迟,变化立即完成。
只有能“渐变”的属性才支持过渡,比如颜色、尺寸、透明度等。像 display 这类非连续属性无法过渡。
基本上就这些。合理使用 transition-duration 能让界面交互更自然流畅。
以上就是在css中如何用transition-duration控制过渡时间的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号