CSS 动画
学习 CSS 动画的基本概念、CSS 动画属性、关键帧动画以及实际应用示例,掌握如何在 CSS 中创建流畅的动画效果。
CSS 动画概述
CSS 动画是指通过 CSS 控制元素的属性变化,从而创建出视觉上的动画效果。CSS 提供了两种主要的动画方式:过渡(transition)和关键帧动画(animation)。
CSS 动画类型
- 过渡动画(Transition):在元素状态变化时(如 hover、focus 等),平滑地从一个状态过渡到另一个状态。
- 关键帧动画(Animation):通过定义关键帧,控制元素在不同时间点的状态,从而创建更复杂的动画效果。
CSS 过渡动画
1. transition 属性
transition 属性用于设置元素的过渡效果,包括过渡的属性、持续时间、时间函数和延迟时间。
| 属性 | 描述 | 示例 |
|---|---|---|
| transition-property | 指定要过渡的 CSS 属性 | transition-property: width; |
| transition-duration | 指定过渡的持续时间 | transition-duration: 1s; |
| transition-timing-function | 指定过渡的时间函数 | transition-timing-function: ease; |
| transition-delay | 指定过渡的延迟时间 | transition-delay: 0.5s; |
/* 使用 transition 简写属性 */
.element {
transition: width 1s ease 0.5s; /* 宽度属性过渡,持续1秒,缓入缓出效果,延迟0.5秒 */
}
/* 分别设置各个属性 */
.element {
transition-property: width; /* 指定要过渡的属性为宽度 */
transition-duration: 1s; /* 过渡持续时间为1秒 */
transition-timing-function: ease; /* 过渡时间函数为缓入缓出 */
transition-delay: 0.5s; /* 过渡延迟时间为0.5秒 */
}
/* 多个属性过渡 */
.element {
transition: width 1s ease, height 0.5s linear; /* 宽度和高度属性分别设置不同的过渡效果 */
}
/* 所有属性过渡 */
.element {
transition: all 1s ease; /* 所有可过渡属性都应用相同的过渡效果 */
}
2. transition-timing-function
transition-timing-function 属性用于指定过渡的时间函数,控制过渡的速度变化。
| 值 | 描述 |
|---|---|
| linear | 线性过渡,速度保持不变 |
| ease | 默认值,缓入缓出,开始和结束时较慢,中间较快 |
| ease-in | 缓入,开始时较慢,逐渐加快 |
| ease-out | 缓出,开始时较快,逐渐减慢 |
| ease-in-out | 缓入缓出,与 ease 类似 |
| cubic-bezier(n, n, n, n) | 自定义贝塞尔曲线 |
| steps(n, start|end) | 步进函数,将过渡分为 n 个步骤 |
/* 使用不同的时间函数 */
.linear {
transition: all 1s linear; /* 线性过渡,速度保持不变 */
}
.ease {
transition: all 1s ease; /* 缓入缓出,开始和结束时较慢,中间较快 */
}
.ease-in {
transition: all 1s ease-in; /* 缓入,开始时较慢,逐渐加快 */
}
.ease-out {
transition: all 1s ease-out; /* 缓出,开始时较快,逐渐减慢 */
}
.ease-in-out {
transition: all 1s ease-in-out; /* 缓入缓出,与ease类似 */
}
.cubic-bezier {
transition: all 1s cubic-bezier(0.42, 0, 0.58, 1); /* 自定义贝塞尔曲线过渡效果 */
}
.steps {
transition: all 1s steps(5, start); /* 步进函数,将过渡分为5个步骤 */
}
3. 过渡动画示例
<div class="transition-example">
鼠标悬停时变色并放大
</div>
<style>
.transition-example {
width: 200px; /* 设置元素宽度为200像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: blue; /* 设置背景颜色为蓝色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 4px; /* 设置边框圆角为4像素 */
transition: background-color 0.5s ease, transform 0.5s ease; /* 设置过渡效果:背景色和变换属性,持续0.5秒,缓入缓出 */
}
.transition-example:hover {
background-color: red; /* 鼠标悬停时背景颜色变为红色 */
transform: scale(1.1); /* 鼠标悬停时元素放大1.1倍 */
}
</style>
CSS 关键帧动画
1. @keyframes 规则
@keyframes 规则用于定义动画的关键帧,即动画在不同时间点的状态。
/* 定义关键帧动画 */
@keyframes fadeIn {
from {
opacity: 0; /* 动画开始时元素完全透明 */
}
to {
opacity: 1; /* 动画结束时元素完全不透明 */
}
}
/* 使用百分比定义多个关键帧 */
@keyframes pulse {
0% {
transform: scale(1); /* 动画开始时元素原始大小 */
}
50% {
transform: scale(1.1); /* 动画中间时元素放大1.1倍 */
}
100% {
transform: scale(1); /* 动画结束时元素恢复原始大小 */
}
}
2. animation 属性
animation 属性用于应用关键帧动画,包括动画名称、持续时间、时间函数、延迟时间、重复次数、方向、填充模式和播放状态。
| 属性 | 描述 | 示例 |
|---|---|---|
| animation-name | 指定要应用的关键帧动画名称 | animation-name: fadeIn; |
| animation-duration | 指定动画的持续时间 | animation-duration: 1s; |
| animation-timing-function | 指定动画的时间函数 | animation-timing-function: ease; |
| animation-delay | 指定动画的延迟时间 | animation-delay: 0.5s; |
| animation-iteration-count | 指定动画的重复次数 | animation-iteration-count: infinite; |
| animation-direction | 指定动画的播放方向 | animation-direction: alternate; |
| animation-fill-mode | 指定动画在播放前后的状态 | animation-fill-mode: forwards; |
| animation-play-state | 指定动画的播放状态 | animation-play-state: running; |
/* 使用 animation 简写属性 */
.element {
animation: fadeIn 1s ease 0.5s 2 alternate forwards running; /* 应用fadeIn动画,持续1秒,缓入缓出,延迟0.5秒,重复2次,交替方向,保持最后状态,正在运行 */
}
/* 分别设置各个属性 */
.element {
animation-name: fadeIn; /* 指定动画名称为fadeIn */
animation-duration: 1s; /* 动画持续时间为1秒 */
animation-timing-function: ease; /* 动画时间函数为缓入缓出 */
animation-delay: 0.5s; /* 动画延迟时间为0.5秒 */
animation-iteration-count: 2; /* 动画重复次数为2次 */
animation-direction: alternate; /* 动画方向为交替播放 */
animation-fill-mode: forwards; /* 动画结束后保持最后一帧状态 */
animation-play-state: running; /* 动画播放状态为正在运行 */
}
3. animation-iteration-count
指定动画的重复次数。
| 值 | 描述 |
|---|---|
| number | 数字,指定动画的重复次数 |
| infinite | 无限重复 |
4. animation-direction
指定动画的播放方向。
| 值 | 描述 |
|---|---|
| normal | 默认值,正常播放 |
| reverse | 反向播放 |
| alternate | 交替播放,奇数次正向,偶数次反向 |
| alternate-reverse | 反向交替播放,奇数次反向,偶数次正向 |
5. animation-fill-mode
指定动画在播放前后的状态。
| 值 | 描述 |
|---|---|
| none | 默认值,动画结束后回到初始状态 |
| forwards | 动画结束后保持最后一帧的状态 |
| backwards | 动画开始前应用第一帧的状态 |
| both | 同时应用 forwards 和 backwards 的效果 |
6. animation-play-state
指定动画的播放状态。
| 值 | 描述 |
|---|---|
| running | 默认值,动画正在播放 |
| paused | 动画已暂停 |
7. 关键帧动画示例
<div class="keyframe-example">
循环脉冲动画
</div>
<style>
/* 定义脉冲动画 */
@keyframes pulse {
0% {
transform: scale(1); /* 动画开始时元素原始大小 */
background-color: blue; /* 动画开始时背景颜色为蓝色 */
}
50% {
transform: scale(1.1); /* 动画中间时元素放大1.1倍 */
background-color: red; /* 动画中间时背景颜色为红色 */
}
100% {
transform: scale(1); /* 动画结束时元素恢复原始大小 */
background-color: blue; /* 动画结束时背景颜色恢复为蓝色 */
}
}
.keyframe-example {
width: 200px; /* 设置元素宽度为200像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: blue; /* 设置初始背景颜色为蓝色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 4px; /* 设置边框圆角为4像素 */
animation: pulse 2s ease-in-out infinite; /* 应用pulse动画,持续2秒,缓入缓出,无限重复 */
}
</style>
CSS 变换属性
transform 属性用于对元素进行旋转、缩放、平移和倾斜等变换,常与动画结合使用。
1. transform 函数
| 函数 | 描述 | 示例 |
|---|---|---|
| translate(x, y) | 平移元素 | transform: translate(50px, 20px); |
| translateX(x) | 沿 X 轴平移元素 | transform: translateX(50px); |
| translateY(y) | 沿 Y 轴平移元素 | transform: translateY(20px); |
| scale(x, y) | 缩放元素 | transform: scale(1.2, 1.5); |
| scaleX(x) | 沿 X 轴缩放元素 | transform: scaleX(1.2); |
| scaleY(y) | 沿 Y 轴缩放元素 | transform: scaleY(1.5); |
| rotate(angle) | 旋转元素 | transform: rotate(45deg); |
| rotateX(angle) | 沿 X 轴旋转元素 | transform: rotateX(45deg); |
| rotateY(angle) | 沿 Y 轴旋转元素 | transform: rotateY(45deg); |
| rotateZ(angle) | 沿 Z 轴旋转元素 | transform: rotateZ(45deg); |
| skew(x-angle, y-angle) | 倾斜元素 | transform: skew(10deg, 5deg); |
| skewX(angle) | 沿 X 轴倾斜元素 | transform: skewX(10deg); |
| skewY(angle) | 沿 Y 轴倾斜元素 | transform: skewY(5deg); |
| matrix(n, n, n, n, n, n) | 使用矩阵变换 | transform: matrix(1, 0, 0, 1, 50, 20); |
| perspective(n) | 设置透视效果 | transform: perspective(1000px); |
2. transform-origin 属性
指定变换的原点。
/* 设置变换原点 */
.origin-center {
transform-origin: center; /* 设置变换原点为元素中心 */
}
.origin-top-left {
transform-origin: top left; /* 设置变换原点为元素左上角 */
}
.origin-bottom-right {
transform-origin: bottom right; /* 设置变换原点为元素右下角 */
}
.origin-custom {
transform-origin: 50% 75%; /* 设置变换原点为元素水平中心,垂直75%位置 */
}
3. 变换示例
<div class="transform-example">
鼠标悬停时旋转并缩放
</div>
<style>
.transform-example {
width: 200px; /* 设置元素宽度为200像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: blue; /* 设置背景颜色为蓝色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 4px; /* 设置边框圆角为4像素 */
transition: transform 0.5s ease; /* 设置变换属性的过渡效果,持续0.5秒,缓入缓出 */
transform-origin: center; /* 设置变换原点为元素中心 */
}
.transform-example:hover {
transform: rotate(10deg) scale(1.1); /* 鼠标悬停时,元素旋转10度并放大1.1倍 */
}
</style>
CSS 动画实际应用示例
示例 1:淡入淡出动画
<div class="fade-example">
淡入淡出动画
</div>
<style>
@keyframes fadeInOut {
0%, 100% {
opacity: 0; /* 动画开始和结束时元素完全透明 */
}
50% {
opacity: 1; /* 动画中间时元素完全不透明 */
}
}
.fade-example {
width: 200px; /* 设置元素宽度为200像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: blue; /* 设置背景颜色为蓝色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 4px; /* 设置边框圆角为4像素 */
animation: fadeInOut 3s ease-in-out infinite; /* 应用fadeInOut动画,持续3秒,缓入缓出,无限重复 */
}
</style>
示例 2:旋转动画
<div class="rotate-example">
旋转动画
</div>
<style>
@keyframes rotate {
from {
transform: rotate(0deg); /* 动画开始时元素旋转0度 */
}
to {
transform: rotate(360deg); /* 动画结束时元素旋转360度 */
}
}
.rotate-example {
width: 100px; /* 设置元素宽度为100像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: red; /* 设置背景颜色为红色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 50%; /* 设置边框圆角为50%,形成圆形 */
animation: rotate 2s linear infinite; /* 应用rotate动画,持续2秒,线性速度,无限重复 */
}
</style>
示例 3:弹跳动画
<div class="bounce-example">
弹跳动画
</div>
<style>
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0); /* 这些时间点元素在原始位置 */
}
40% {
transform: translateY(-30px); /* 动画40%时元素向上移动30像素 */
}
60% {
transform: translateY(-15px); /* 动画60%时元素向上移动15像素 */
}
}
.bounce-example {
width: 100px; /* 设置元素宽度为100像素 */
height: 100px; /* 设置元素高度为100像素 */
background-color: green; /* 设置背景颜色为绿色 */
color: white; /* 设置文字颜色为白色 */
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
justify-content: center; /* 水平居中对齐 */
border-radius: 50%; /* 设置边框圆角为50%,形成圆形 */
animation: bounce 2s ease-in-out infinite; /* 应用bounce动画,持续2秒,缓入缓出,无限重复 */
}
</style>
示例 4:打字机效果
<div class="typewriter-example">
这是一个打字机效果
</div>
<style>
.typewriter-example {
width: 300px; /* 设置元素宽度为300像素 */
height: 50px; /* 设置元素高度为50像素 */
border-right: 2px solid #333; /* 添加右侧边框作为光标 */
white-space: nowrap; /* 禁止文字换行 */
overflow: hidden; /* 隐藏超出部分 */
animation: typing 3s steps(20, end), blink-caret 0.75s step-end infinite; /* 应用typing和blink-caret动画 */
}
@keyframes typing {
from {
width: 0; /* 动画开始时元素宽度为0 */
}
to {
width: 300px; /* 动画结束时元素宽度为300像素 */
}
}
@keyframes blink-caret {
from, to {
border-color: transparent; /* 动画开始和结束时光标透明 */
}
50% {
border-color: #333; /* 动画中间时光标显示 */
}
}
</style>
示例 5:加载动画
<div class="loading-example">
<div class="spinner"></div>
加载中...
</div>
<style>
.loading-example {
display: flex; /* 使用flex布局 */
align-items: center; /* 垂直居中对齐 */
gap: 10px; /* 设置元素间距为10像素 */
}
@keyframes spin {
0% {
transform: rotate(0deg); /* 动画开始时元素旋转0度 */
}
100% {
transform: rotate(360deg); /* 动画结束时元素旋转360度 */
}
}
.spinner {
width: 20px; /* 设置元素宽度为20像素 */
height: 20px; /* 设置元素高度为20像素 */
border: 2px solid #f3f3f3; /* 设置边框为2像素实线,颜色为浅灰色 */
border-top: 2px solid #3498db; /* 设置上边框为2像素实线,颜色为蓝色 */
border-radius: 50%; /* 设置边框圆角为50%,形成圆形 */
animation: spin 1s linear infinite; /* 应用spin动画,持续1秒,线性速度,无限重复 */
}
</style>
CSS 动画使用技巧
- 选择合适的动画类型:根据需要选择过渡动画或关键帧动画,简单的状态变化使用过渡动画,复杂的动画效果使用关键帧动画。
- 使用硬件加速:使用 transform 和 opacity 属性进行动画,可以触发硬件加速,提高动画性能。
- 合理设置动画持续时间:动画持续时间不宜过长或过短,一般在 0.3-1 秒之间,以确保动画效果流畅且不影响用户体验。
- 使用适当的时间函数:根据动画的性质选择合适的时间函数,如线性动画适合机械运动,缓入缓出适合自然运动。
- 避免过度使用动画:过多的动画会分散用户注意力,影响用户体验,应在适当的地方使用动画。
- 考虑浏览器兼容性:一些新的动画属性可能不被所有浏览器支持,需要考虑兼容性。
- 优化动画性能:避免在动画中使用会触发重排(reflow)的属性,如 width、height、top、left 等,尽量使用 transform 和 opacity。
- 使用 CSS 变量控制动画:使用 CSS 变量定义动画的参数,可以方便地控制和修改动画效果。
- 测试不同设备上的动画效果:确保动画在不同设备和浏览器上都能正常显示,避免在低性能设备上出现卡顿。
- 结合 JavaScript 使用:对于需要复杂控制的动画,可以结合 JavaScript 来控制动画的播放、暂停、重置等。
CSS 动画总结
- CSS 提供了两种主要的动画方式:过渡动画(transition)和关键帧动画(animation)。
- 过渡动画适用于简单的状态变化,如 hover、focus 等。
- 关键帧动画适用于更复杂的动画效果,可以控制动画在不同时间点的状态。
- transform 属性用于对元素进行旋转、缩放、平移和倾斜等变换,常与动画结合使用。
- 合理使用 CSS 动画可以增强用户体验,使界面更加生动和交互性。
- 注意动画性能和浏览器兼容性,避免过度使用动画导致的性能问题。
- 结合 JavaScript 可以实现更复杂的动画控制和交互效果。