CSS3 animation 属性详解
核心知识点
animation 属性概述
animation 是 CSS3 中的一个复合属性,用于定义元素的动画效果。它允许开发者将多个动画相关的属性值组合在一个声明中,包括动画名称、持续时间、时间函数、延迟、重复次数、方向和填充模式等。
animation 属性语法
animation: name duration timing-function delay iteration-count direction fill-mode play-state;其中各个值的含义:
name:指定要绑定到选择器的关键帧名称duration:指定动画完成一个周期所需的时间timing-function:指定动画的速度曲线delay:指定动画开始前的延迟时间iteration-count:指定动画应该播放的次数direction:指定动画是否应该反向播放fill-mode:指定动画在播放前后如何应用样式play-state:指定动画是否正在运行或已暂停
animation 属性取值
| 子属性 | 描述 | 默认值 | 可能值 |
|---|---|---|---|
| animation-name | 动画名称 | none | 自定义关键帧名称或 none |
| animation-duration | 动画持续时间 | 0s | 时间值(如 1s, 500ms) |
| animation-timing-function | 动画速度曲线 | ease | ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier(n,n,n,n) |
| animation-delay | 动画延迟时间 | 0s | 时间值(如 1s, 500ms) |
| animation-iteration-count | 动画重复次数 | 1 | 数字或 infinite |
| animation-direction | 动画方向 | normal | normal, reverse, alternate, alternate-reverse |
| animation-fill-mode | 动画填充模式 | none | none, forwards, backwards, both |
| animation-play-state | 动画播放状态 | running | running, paused |
实用案例分析
基本动画示例
示例 1:简单的淡入淡出动画
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 animation 基本示例</title>
<style>
/* 定义关键帧 */
@keyframes fadeInOut {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
/* 应用动画 */
.box {
width: 200px;
height: 200px;
background-color: #3498db;
margin: 50px auto;
/* 应用 animation 属性 */
animation: fadeInOut 3s ease-in-out infinite;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>效果说明:
- 定义了一个名为
fadeInOut的关键帧动画,实现元素的淡入淡出效果 - 使用
animation属性将动画应用到.box元素 - 动画持续时间为 3 秒,使用
ease-in-out时间函数,无限次重复
示例 2:复杂动画组合
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 animation 复杂示例</title>
<style>
/* 定义旋转关键帧 */
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* 定义缩放关键帧 */
@keyframes scale {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
/* 应用旋转动画 */
.box1 {
width: 100px;
height: 100px;
background-color: #e74c3c;
margin: 50px;
display: inline-block;
/* 应用旋转动画 */
animation: rotate 4s linear infinite;
}
/* 应用缩放动画 */
.box2 {
width: 100px;
height: 100px;
background-color: #2ecc71;
margin: 50px;
display: inline-block;
/* 应用缩放动画 */
animation: scale 2s ease-in-out infinite;
}
/* 应用组合动画 */
.box3 {
width: 100px;
height: 100px;
background-color: #f39c12;
margin: 50px;
display: inline-block;
/* 应用多个动画,用逗号分隔 */
animation: rotate 4s linear infinite, scale 2s ease-in-out infinite;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>效果说明:
- 定义了两个关键帧动画:
rotate(旋转)和scale(缩放) .box1元素应用了旋转动画,持续 4 秒,线性速度,无限次重复.box2元素应用了缩放动画,持续 2 秒,缓入缓出速度,无限次重复.box3元素同时应用了两个动画,实现旋转和缩放的组合效果
实际应用场景
场景 1:加载动画
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 加载动画</title>
<style>
/* 定义加载动画关键帧 */
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* 应用加载动画 */
.loader {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
margin: 100px auto;
/* 应用动画 */
animation: spin 1s linear infinite;
}
</style>
</head>
<body>
<div class="loader"></div>
</body>
</html>效果说明:
- 定义了一个名为
spin的关键帧动画,实现元素的 360 度旋转 - 使用
animation属性将动画应用到.loader元素 - 动画持续时间为 1 秒,使用
linear时间函数,无限次重复 - 通过边框样式和动画效果,创建了一个经典的加载动画
场景 2:悬停效果增强
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 悬停动画效果</title>
<style>
/* 定义悬停动画关键帧 */
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
/* 按钮样式 */
.btn {
display: inline-block;
padding: 10px 20px;
background-color: #3498db;
color: white;
text-decoration: none;
border-radius: 5px;
margin: 50px;
transition: background-color 0.3s ease;
}
/* 悬停时应用动画 */
.btn:hover {
background-color: #2980b9;
animation: bounce 1s ease;
}
</style>
</head>
<body>
<a href="#" class="btn">点击我</a>
</body>
</html>效果说明:
- 定义了一个名为
bounce的关键帧动画,实现元素的弹跳效果 - 为
.btn元素设置了基本样式和过渡效果 - 当鼠标悬停在按钮上时,应用
bounce动画,使按钮产生弹跳效果
代码优化建议
性能优化
- 对于复杂动画,建议使用
transform和opacity属性,因为它们可以利用 GPU 加速 - 避免在动画中使用会触发重排的属性,如
width、height、top、left等
- 对于复杂动画,建议使用
兼容性处理
- 虽然现代浏览器已经广泛支持 CSS3 动画,但为了兼容旧版浏览器,可以添加浏览器前缀
- 例如:
-webkit-animation、-moz-animation、-o-animation
动画控制
- 使用
animation-play-state属性可以暂停和恢复动画 - 结合 JavaScript 可以实现更复杂的动画控制逻辑
- 使用
总结
animation 属性是 CSS3 中一个强大的动画工具,它允许开发者创建各种复杂的动画效果,而不需要依赖 JavaScript 或其他脚本语言。通过合理使用 animation 属性及其子属性,开发者可以为网页添加生动的视觉效果,提升用户体验。
在实际开发中,建议根据具体需求选择合适的动画参数,并注意性能优化,以确保动画效果流畅且不影响页面性能。