CSS
CSS Animation & Transition 完全指南 2026
CSS transition 和 animation 的区别、使用方法、性能优化。打造流畅 UI 交互的实战指南。
Toolypet Team
Development Team
CSS Animation & Transition 完全指南 2026
按钮悬停效果、加载动画、页面转场……在现代网页开发中,动画是必不可少的。
学习如何仅用 CSS 就能创建流畅的交互效果,无需 JavaScript。
Transition vs Animation
| 特性 | Transition | Animation |
|---|---|---|
| 触发 | 需要状态变化 (:hover 等) | 可自动播放 |
| 关键帧 | 仅开始-结束 2个 | 无限制 |
| 循环 | 不可以 | 可无限循环 |
| 方向 | 仅正向 | 可反向、交替 |
| 用途 | 简单状态转换 | 复杂动画 |
CSS Transition
基本语法
transition: property duration timing-function delay;
| 属性 | 说明 | 默认值 |
|---|---|---|
property | 过渡属性 | all |
duration | 持续时间 | 0s |
timing-function | 缓动曲线 | ease |
delay | 延迟时间 | 0s |
基本示例
/* 单一属性 */
.button {
background: #3b82f6;
transition: background 0.3s ease;
}
.button:hover {
background: #2563eb;
}
/* 多个属性 */
.card {
transform: translateY(0);
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
}
时间函数
/* 内置函数 */
.ease { transition-timing-function: ease; } /* 默认 */
.linear { transition-timing-function: linear; } /* 匀速 */
.ease-in { transition-timing-function: ease-in; } /* 慢启动 */
.ease-out { transition-timing-function: ease-out; } /* 慢结束 */
.ease-in-out { transition-timing-function: ease-in-out; }
/* 自定义贝塞尔曲线 */
.custom { transition-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55); }
/* 步进 */
.steps { transition-timing-function: steps(4, end); }
常用贝塞尔曲线
/* 弹跳 */
.bounce { transition: all 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55); }
/* 平滑 */
.smooth { transition: all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94); }
/* 快速 */
.snap { transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); }
CSS Animation
基本语法
@keyframes 动画名称 {
from { /* 开始状态 */ }
to { /* 结束状态 */ }
}
/* 或者 */
@keyframes 动画名称 {
0% { /* 开始 */ }
50% { /* 中间 */ }
100% { /* 结束 */ }
}
.element {
animation: 名称 duration timing-function delay iteration-count direction fill-mode;
}
Animation 属性
| 属性 | 值 | 说明 |
|---|---|---|
animation-name | 关键帧名称 | 必需 |
animation-duration | 时间 | 必需 |
animation-timing-function | 时间函数 | ease |
animation-delay | 延迟时间 | 0s |
animation-iteration-count | 重复次数 | 1, infinite |
animation-direction | 方向 | normal, reverse, alternate |
animation-fill-mode | 结束状态 | none, forwards, backwards, both |
animation-play-state | 播放状态 | running, paused |
实战动画示例
1. 淡入
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fade-in {
animation: fadeIn 0.5s ease forwards;
}
/* 从上方淡入 */
@keyframes fadeInDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fade-in-down {
animation: fadeInDown 0.5s ease forwards;
}
2. 加载动画
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #e5e7eb;
border-top-color: #3b82f6;
border-radius: 50%;
animation: spin 1s linear infinite;
}
3. 脉冲效果
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.pulse {
animation: pulse 2s ease-in-out infinite;
}
/* 脉冲圆环 */
@keyframes pulseRing {
0% {
transform: scale(0.8);
opacity: 1;
}
100% {
transform: scale(2);
opacity: 0;
}
}
.pulse-ring {
position: relative;
}
.pulse-ring::before {
content: '';
position: absolute;
inset: 0;
border: 2px solid #3b82f6;
border-radius: 50%;
animation: pulseRing 1.5s ease-out infinite;
}
4. 弹跳
@keyframes bounce {
0%, 100% {
transform: translateY(0);
animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
}
50% {
transform: translateY(-25%);
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
}
}
.bounce {
animation: bounce 1s infinite;
}
5. 抖动(错误提示)
@keyframes shake {
0%, 100% { transform: translateX(0); }
10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
20%, 40%, 60%, 80% { transform: translateX(5px); }
}
.shake {
animation: shake 0.5s ease;
}
/* 用 JavaScript 触发 */
/* element.classList.add('shake'); */
6. 打字效果
@keyframes typing {
from { width: 0; }
to { width: 100%; }
}
@keyframes blink {
50% { border-color: transparent; }
}
.typing {
overflow: hidden;
white-space: nowrap;
border-right: 3px solid;
width: 0;
animation:
typing 3s steps(30) forwards,
blink 0.7s step-end infinite;
}
7. 骨架屏加载
@keyframes shimmer {
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
.skeleton {
background: linear-gradient(
90deg,
#f0f0f0 25%,
#e0e0e0 50%,
#f0f0f0 75%
);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
}
配合 Transform 使用
Transform 属性
/* 位移 */
transform: translateX(100px);
transform: translateY(50px);
transform: translate(100px, 50px);
/* 旋转 */
transform: rotate(45deg);
transform: rotateX(45deg); /* 3D */
transform: rotateY(45deg); /* 3D */
/* 缩放 */
transform: scale(1.5);
transform: scaleX(2);
/* 倾斜 */
transform: skew(10deg);
/* 组合 */
transform: translateX(100px) rotate(45deg) scale(1.2);
3D 效果
/* 在父元素设置透视 */
.perspective-container {
perspective: 1000px;
}
/* 卡片翻转 */
.card-3d {
transform-style: preserve-3d;
transition: transform 0.6s;
}
.card-3d:hover {
transform: rotateY(180deg);
}
.card-front, .card-back {
backface-visibility: hidden;
}
.card-back {
transform: rotateY(180deg);
}
性能优化
GPU 加速属性
/* ✅ GPU 加速(性能好) */
transform: translateX(100px);
transform: scale(1.1);
transform: rotate(45deg);
opacity: 0.5;
/* ❌ CPU 计算(触发重排) */
left: 100px;
width: 200px;
height: 200px;
margin: 10px;
will-change
/* 为动画目标提供提示 */
.animated {
will-change: transform, opacity;
}
/* 仅在悬停时 */
.card:hover {
will-change: transform;
}
/* 建议在动画后移除 */
避免重排
/* ❌ 应避免的模式 */
.bad {
animation: move 1s infinite;
}
@keyframes move {
to { left: 100px; } /* 触发重排 */
}
/* ✅ 推荐模式 */
.good {
animation: moveGood 1s infinite;
}
@keyframes moveGood {
to { transform: translateX(100px); } /* GPU 加速 */
}
无障碍访问
尊重用户的动效偏好
/* 当用户偏好减少动效时 */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* 或选择性处理 */
@media (prefers-reduced-motion: reduce) {
.animated {
animation: none;
}
.transition {
transition: none;
}
}
实用 UI 模式
按钮悬停
.button {
background: #3b82f6;
color: white;
padding: 12px 24px;
border: none;
border-radius: 8px;
cursor: pointer;
transition: all 0.2s ease;
}
.button:hover {
background: #2563eb;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(59,130,246,0.4);
}
.button:active {
transform: translateY(0);
}
下拉菜单
.dropdown {
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.2s ease;
}
.menu:hover .dropdown {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
模态框打开
.modal-overlay {
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal {
transform: scale(0.9) translateY(20px);
transition: transform 0.3s ease;
}
.modal-overlay.active .modal {
transform: scale(1) translateY(0);
}
依次出现
.item {
opacity: 0;
transform: translateY(20px);
animation: fadeInUp 0.5s ease forwards;
}
.item:nth-child(1) { animation-delay: 0.1s; }
.item:nth-child(2) { animation-delay: 0.2s; }
.item:nth-child(3) { animation-delay: 0.3s; }
@keyframes fadeInUp {
to {
opacity: 1;
transform: translateY(0);
}
}
常见问题
Q1: transition 不起作用
A: 检查以下几点:
- 属性是否可动画
- 从
display: none过渡时不起作用 - 是否明确定义了起始值和结束值
Q2: 如何保持动画结束后的状态?
A: 使用 animation-fill-mode: forwards
.element {
animation: fadeIn 1s ease forwards;
}
Q3: 性能不好
A:
- 只对
transform和opacity做动画 - 适当使用
will-change - 避免对太多元素做动画
Q4: CSS 动画还是 JavaScript 动画?
A:
- 简单过渡: CSS
- 复杂序列: JavaScript(如 GSAP)
- 响应用户输入: JavaScript
- 自动循环: CSS
总结
CSS 动画要点:
- Transition: 响应状态变化
- Animation: 自动播放,关键帧
- Transform: GPU 加速,性能优异
- 时间函数: ease, cubic-bezier
- 无障碍: 尊重 prefers-reduced-motion
相关工具
| 工具 | 用途 |
|---|---|
| Animation 生成器 | 生成关键帧 |
| Transform 生成器 | 变换效果 |
| 滤镜生成器 | CSS 滤镜 |
CSSanimationtransition动画UI交互
关于作者
Toolypet Team
Development Team
The Toolypet Team creates free, privacy-focused web tools for developers and designers. All tools run entirely in your browser with no data sent to servers.
Web DevelopmentCSS ToolsDeveloper ToolsSEOSecurity