CSS的transition属性通过设置property、duration、timing-function和delay实现元素状态变化的平滑动画,如:hover时颜色渐变或尺寸缩放;可单独指定属性或使用all对多个属性应用过渡,并配合ease、linear等速度曲线控制动画节奏,提升交互体验。

CSS 的 transition 属性可以让元素在状态变化时实现平滑的动画效果,比如鼠标悬停时颜色渐变、按钮缩放等。它适合做简单的交互动画,不需要关键帧(keyframes),使用起来非常直观。
transition 是一个复合属性,可以设置以下子属性:
写法示例:
pre { transition: property duration timing-function delay; }例如:
立即学习“前端免费学习笔记(深入)”;
pre { transition: background-color 0.3s ease 0.1s; }常见的用途是配合 :hover 实现视觉反馈。比如让按钮背景色缓慢变化:
pre { .button { background-color: #007bff; color: white; padding: 10px 20px; border: none; transition: background-color 0.4s ease; } .button:hover { background-color: #0056b3; } }当鼠标移到按钮上时,背景色会在 0.4 秒内平滑变深。
如果想让多个属性都有过渡效果,可以分别列出,或使用 all:
pre { .box { width: 100px; height: 100px; background: red; transition: all 0.5s ease; } .box:hover { width: 150px; height: 150px; background: blue; transform: rotate(15deg); } }这里宽度、高度、背景色和旋转都会产生过渡动画。
控制动画节奏很重要。常用值有:
例如让动画更自然:
pre { transition: transform 0.3s ease-in-out; }基本上就这些。transition 不复杂但容易忽略细节,比如没设 duration 就不会动,或者属性不支持动画。掌握它能快速提升页面交互质感。
以上就是如何用css transition属性设置简单动画的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号