前言
作为前端开发者,CSS动画是我们必须掌握的核心技能。本文将系统介绍CSS动画的6种基础类型,包括位移(translate)、缩放(scale)、旋转(rotate)、尺寸变化(width/height)、背景/颜色变化(background/color)和透明度(opacity)。通过组合这些基础动画,我们可以创造出丰富多样的交互效果。
1. CSS基本动画的6种基础类型
类型 | 属性/函数 | 示例用途 |
---|---|---|
位移 | transform: translate |
元素滑动、弹跳效果 |
缩放 | transform: scale |
放大/缩小元素 |
旋转 | transform: rotate |
旋转图标、加载动画 |
尺寸变化 | width /height |
展开菜单、呼吸效果 |
背景变化 | background /color |
颜色渐变、状态提示 |
淡入淡出 | opacity |
显示/隐藏元素 |
效果展示:


2. 案例说明
2.1 位移(translate)
作用:让元素在X轴、Y轴或Z轴方向上移动。
xml
<!-- 位移动画 -->
<div class="demo-container">
<h2 class="demo-title">位移动画 (translate)</h2>
<div class="animation-box translate-box">移动</div>
<p>使用 transform: translateX/Y 实现元素位置移动</p>
</div>
<style>
body {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
padding: 20px;
background-color: #f5f5f5;
}
.demo-container {
background: white;
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.demo-title {
color: #333;
margin-top: 0;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.animation-box {
width: 100px;
height: 100px;
margin: 20px auto;
background-color: #3498db;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-weight: bold;
}
/* 位移动画 */
.translate-box {
animation: slideIn 2s infinite alternate;
}
@keyframes slideIn {
0% { transform: translateX(-100px); }
100% { transform: translateX(100px); }
}
</style>
效果说明 :元素在水平方向上来回滑动,从左侧-100px位置移动到右侧100px位置。alternate
属性使动画往返播放,infinite
让动画无限循环。
2.2 缩放(scale)
作用:放大或缩小元素尺寸。
xml
<!-- 缩放动画 -->
<div class="demo-container">
<h2 class="demo-title">缩放动画 (scale)</h2>
<div class="animation-box scale-box">缩放</div>
<p>使用 transform: scale() 实现元素大小缩放</p>
</div>
<style>
/* 缩放动画 */
.scale-box {
animation: scale 2s infinite alternate;
}
@keyframes scale {
0% { transform: scale(0.5); }
100% { transform: scale(1.5); }
}
</style>
效果说明:红色方块会在0.5倍到1.5倍大小之间循环缩放。常用于强调重要元素或创建呼吸效果。
2.3 尺寸变化(resize)
作用:直接改变元素的宽度或高度。
xml
<!-- 尺寸变化 -->
<div class="demo-container">
<h2 class="demo-title">尺寸变化 (resize)</h2>
<div class="animation-box resize-box">尺寸</div>
<p>使用 width/height 属性实现元素尺寸变化</p>
</div>
<style>
/* 尺寸变化 */
.resize-box {
animation: expand 2s infinite alternate;
}
@keyframes expand {
0% { width: 50px; height: 50px; }
100% { width: 150px; height: 150px; }
}
</style>
效果说明:方块宽度从50px扩展到150px,高度从50px收缩到150px。
2.4 旋转(rotate)
作用:使元素绕中心点旋转。
xml
<!-- 旋转动画 -->
<div class="demo-container">
<h2 class="demo-title">旋转动画 (rotate)</h2>
<div class="animation-box rotate-box">旋转</div>
<p>使用 transform: rotate() 实现元素旋转效果</p>
</div>
<style>
/* 旋转动画 */
.rotate-box {
animation: spin 2s infinite linear;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
效果说明:方块会以线性速度无限旋转360度。常用于加载指示器或刷新按钮。
2.5 背景变化(background)
作用:改变元素的背景色或背景图。
xml
<!-- 背景变化 -->
<div class="demo-container">
<h2 class="demo-title">背景变化 (background)</h2>
<div class="animation-box background-box">背景</div>
<p>使用 background-color 实现背景颜色渐变</p>
</div>
<style>
/* 背景变化 */
.background-box {
animation: colorChange 2s infinite alternate;
}
@keyframes colorChange {
0% { background-color: #3498db; }
100% { background-color: #e74c3c; }
}
</style>
效果说明:方块背景色元素的背景色从蓝色平滑过渡到红色。适合用于状态变化提示。
2.6 淡入淡出(fade)
作用:通过透明度变化实现元素的显现或消失。
xml
<!-- 淡入淡出 -->
<div class="demo-container">
<h2 class="demo-title">淡入淡出 (fade)</h2>
<div class="animation-box fade-box">淡入淡出</div>
<p>使用 opacity 实现元素的淡入淡出效果</p>
</div>
<style>
/* 淡入淡出 */
.fade-box {
animation: fadeInOut 3s infinite;
}
@keyframes fadeInOut {
0% { opacity: 0; }
50% { opacity: 1; }
100% { opacity: 0; }
}
</style>
效果说明:深方块会逐渐显现再消失,实现淡入淡出效果。常用于模态框或提示信息的显示隐藏。
总结
最后总结一下,掌握这6种CSS基础动画类型,能应对大多数常见的动效需求。在实际项目中,根据具体场景选择合适的动画类型和组合方式。
如有错误,请指正O^O!