CSS Animations
创建 smooth CSS animations and transitions 使用 keyframes, transforms, and timing.
你将学到
- difference 之间 transitions and animations
- 如何 @keyframes 定义 multi-步骤 animations
- Transform and opacity as performant 属性到 animate
- Timing 函数, delays, and animation shorthand
概念讲解
CSS Animations
CSS can animate elements without any JavaScript. There are two tools: transitions for smooth changes between states (like a button slowly changing color on hover), and animations for complex, multi-step, or repeating sequences (like a loading spinner). Both run on the browser's compositor thread, so they can be smoother than JavaScript-driven animation.
Transitions
A transition smoothly interpolates a property when its value changes. Define which properties to transition and how long the change takes:
.button {
transition: transform 0.2s ease, background-color 0.3s ease;
}
.button:hover {
transform: scale(1.05);
background-color: #1d4ed8;
}
When the user hovers, the button grows and changes color over 0.2–0.3 seconds instead of snapping. Transitions are perfect for hover states, focus states, and any property change triggered by a class toggle.
@keyframes Animations
@keyframes define a sequence of styles at points along the timeline (0% to 100%). An animation applies those keyframes to an element:
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
animation: spin 1s linear infinite;
}
Animations can repeat (无限), alternate direction, delay start, and use multi-step keyframes (0%, 50%, 100%).
Performant Properties
Not all properties animate smoothly. Animating 宽度, 高度, 顶部, or margin triggers layout recalculation on every frame, which is expensive. The two properties that animate buttery-smooth are transform (translate, scale, rotate) and opacity — they are handled by the compositor without recalculating layout. If you need to move an element, animate transform: translateX(), not 左.
Timing Functions
The timing function controls the pace: linear (constant speed), ease (slow start and end — the default), ease-in, ease-out, ease-in-out, and cubic-bezier() for custom curves. ease-out is generally the most natural-feeling for UI — it starts fast and decelerates, giving a sense of responsiveness. For spring-like effects, use a custom cubic-bezier or the steps() function for a stuttered, frame-by-frame look.
示例代码
/* A card that lifts on hover with a smooth transition */
.card {
transition: transform 0.25s ease-out, box-shadow 0.25s ease-out;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
/* A fade-in-up entrance animation */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.notification {
animation: fadeInUp 0.4s ease-out forwards;
}
/* A pulsing loading dot */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.loading-dot {
animation: pulse 1.5s ease-in-out infinite;
}
card uses transition for hover lift 副作用 — 仅 transform and box-shadow 更改. fadeInUp 是 keyframe animation for entrance. pulse uses multi-步骤 keyframes (0%, 50%, 100%) 使用无限 repetition for 加载 indicator. 所有 animate transform/opacity for 性能.
动手试一试
- SVG Optimizer
SVGs 能是 animated 使用 CSS — optimize SVG 之前 animating 它的 paths.
- 镜像颜色 Picker
选取颜色 for animated gradients and keyframe 颜色停止.
常见错误
错误写法
Animating 布局属性如 宽度, 高度, or 顶部, causing janky frames.
正确做法
Animate transform and opacity 仅 — 它们运行 on compositor thread 不使用 triggering 布局. 到 move 元素, use transform: translateX/Y. 到 resize, use transform: scale. 到 fade, use opacity.
错误写法
Using linear timing for UI animations, 哪个 feels mechanical and unnatural.
正确做法
Use ease-out for 大多数 UI animations — 它开始快 and decelerates, feeling responsive and natural. Reserve linear for mechanical things 如 spinners. For spring 副作用, use 自定义 cubic-bezier curve.
相关资源
相关速查表
- CSS Cheatsheet
引用 for transitions, animations, keyframes, and timing 函数.
练习 CSS Animations
5 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。