CSS基础动画keyframes

前言

作为前端开发者,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!

相关推荐
li35745 小时前
将已有 Vue 项目通过 Electron 打包为桌面客户端的完整步骤
前端·vue.js·electron
Icoolkj5 小时前
VuePress 与 VitePress 深度对比:特性、差异与选型指南
前端·javascript·vue.js
excel6 小时前
CNN 分层详解:卷积、池化到全连接的作用与原理
前端
excel6 小时前
CNN 多层设计详解:从边缘到高级特征的逐层学习
前端
西陵7 小时前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
大前端helloworld7 小时前
从初中级如何迈入中高级-其实技术只是“入门卷”
前端·面试
东风西巷9 小时前
Balabolka:免费高效的文字转语音软件
前端·人工智能·学习·语音识别·软件需求
萌萌哒草头将军9 小时前
10个 ES2025 新特性速览!🚀🚀🚀
前端·javascript·vue.js
半夏陌离9 小时前
SQL 入门指南:排序与分页查询(ORDER BY 多字段排序、LIMIT 分页实战)
java·前端·数据库