- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z 索引
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode 双向
- CSS - min-content
- CSS - 所有
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移
- CSS - 重音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 动画
CSS 动画允许在不同样式之间创建平滑过渡,无需使用 JavaScript。例如,
什么是 CSS 动画?
在 CSS 中,我们可以根据时间长度、用户交互或状态变化动态更改元素的样式,这称为 CSS 动画。它使用 `@keyframes` 规则来创建动画,并使用 animation 属性将其应用于元素。
目录
@keyframes 规则
`@keyframes` 规则用于定义动画的关键帧,指定动画元素在动画的不同阶段的外观。考虑以下定义关键帧规则的代码。
.box{
animation: colorChange 5s infinite;
}
@keyframes colorChange {
0% {
background-color: red;
}
50% {
background-color: green;
}
100% {
background-color: blue;
}
}
此代码将为具有类 .box 的元素定义动画,动画名称为 colorChange,持续 5 秒,并且无限次重复。
关键帧规则是为名为 colorChange 的动画定义的。
- 在动画总持续时间的 0%(即 0 秒)时,背景颜色将为红色。
- 在总时间的 50%(即 2.5 秒)时,背景颜色更改为绿色。
- 在总持续时间的 100%(即 5 秒)时,颜色更改为蓝色。
动画延迟属性
我们可以使用 animation-delay 属性设置动画开始的延迟。查看以下示例
您还可以为 animation-delay 属性设置负值。如果您使用负值 -n,则动画将开始,就像它已经播放了 n 秒一样。
示例
在此示例中,球将在 2 秒后开始向左移动。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.ball {
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
position: absolute;
left: 0;
animation-name: moveRight;
/* Set duration */
animation-duration: 2s;
/* Set delay for animation */
animation-delay: 2s;
}
@keyframes moveRight {
to {
left: calc(100% - 50px);
}
}
</style>
</head>
<body>
<div class="container">
<div class="ball"></div>
</div>
</body>
</html>
设置动画迭代次数
我们可以使用 animation-iteration-count 属性设置动画重复的次数。
CSS 规范不支持此属性的负值。它可以取正整数(例如,1、2、3 等)或关键字“infinite”作为值。
示例
在此示例中,我们将球的迭代次数设置为无限。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.ball {
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
position: absolute;
left: 0;
animation-name: moveRight;
/* Set duration */
animation-duration: 2s;
/* Set number of time animation repeats */
animation-iteration-count: infinite;
}
@keyframes moveRight {
to {
left: calc(100% - 50px);
}
}
</style>
</head>
<body>
<div class="container">
<div class="ball"></div>
</div>
</body>
</html>
动画方向属性
我们可以使用 animation-direction 属性指定动画运行的方向。
以下是 animation-direction 属性的有效值
- normal: 动画正常播放(向前)。这是默认值。
- reverse: 动画反向播放(向后)。
- alternate: 动画先向前播放,然后向后播放。
- alternate-reverse: 动画先向后播放,然后向前播放。
示例
在此示例中,我们使用内联 CSS 设置动画方向属性。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.ball {
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
position: relative;
left:0;
animation-name: moveRight ;
animation-duration: 2s;
animation-iteration-count: infinite;
}
@keyframes moveRight {
to {
left: calc(100% - 50px);
}
}
</style>
</head>
<body>
<h2>animation-direction: normal</h2>
<div class="ball"
style="animation-direction: normal; ">
</div>
<h2>animation-direction: reverse</h2>
<div class="ball"
style="animation-direction: reverse;">
</div>
<h2>animation-direction: alternate</h2>
<div class="ball"
style="animation-direction: alternate;">
</div>
<h2>animation-direction: alternate-reverse</h2>
<div class="ball"
style="animation-direction: alternate-reverse;">
</div>
</body>
</html>
动画计时函数
在 CSS 中,animation-timing-function 属性用于定义动画的速度曲线。它可以取以下值。
- ease: 动画将缓慢开始,然后加速,然后缓慢结束(默认值)。
- linear: 从开始到结束速度相同的动画。
- ease-in: 动画缓慢开始。
- ease-out: 动画缓慢结束。
- ease-in-out: 动画缓慢开始和结束。
- cubic-bezier(n,n,n,n): 这让我们可以为速度定义自己的值。要了解更多信息,请查看 三次贝塞尔曲线函数 文章。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.ball {
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
position: relative;
left:0;
animation-name: moveRight ;
animation-duration: 2s;
animation-iteration-count: infinite;
}
@keyframes moveRight {
to {
left: calc(100% - 50px);
}
}
</style>
</head>
<body>
<h2>linear</h2>
<div class="ball"
style="animation-timing-function: linear;">
</div>
<h2>ease</h2>
<div class="ball"
style="animation-timing-function: ease;">
</div>
<h2>ease-in</h2>
<div class="ball"
style="animation-timing-function: ease-in;">
</div>
<h2>ease-out</h2>
<div class="ball"
style="animation-timing-function: ease-out;">
</div>
<h2>ease-in-out</h2>
<div class="ball"
style="animation-timing-function: ease-in-out;">
</div>
<h2>cubic-bezier(0.25, 0.1, 0.25, 1)</h2>
<div class="ball"
style="animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);">
</div>
</body>
</html>
设置动画填充模式
animation-fill-mode 属性指定动画未播放时(开始之前、结束之后或两者)目标元素的样式。
animation-fill-mode 属性可以具有以下值
- none: 动画不会在开始之前或结束之后应用任何样式。这是默认值。
- forwards: 在动画结束时,元素将保持最后一个关键帧规则设置的样式。
- backwards: 在动画结束时,元素将保持第一个关键帧规则设置的样式。
- both: 动画将遵循 forwards 和 backwards 的规则。
查看以下代码的输出以了解更多信息
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.box {
padding: 10px;
background-color: green;
color: white;
font-size: 16px;
margin: 20px;
animation-duration: 2s;
animation-name: changeColor;
}
/* Animation Definition */
@keyframes changeColor {
0% {
background-color: blue;
}
100% {
background-color: red ;
}
}
/* Animation Fill Modes */
.none {
animation-fill-mode: none;
}
.forwards {
animation-fill-mode: forwards;
}
.backwards {
animation-fill-mode: backwards;
animation-delay: 2s;
}
.both {
animation-fill-mode: both;
animation-delay: 2s;
}
</style>
</head>
<body>
<div class="box none">None</div>
<div class="box forwards">Forwards</div>
<div class="box backwards">Backwards</div>
<div class="box both">Both</div>
</body>
</html>
动画简写属性
在 CSS 中,animation 属性是以下属性的简写
- animation-name: 设置动画的名称。
- animation-duration: 设置动画的持续时间。
- animation-timing-function: 定义动画的速度曲线。
- animation-delay: 设置动画开始之前的延迟。
- animation-iteration-count: 设置动画重复的次数。
- animation-direction: 定义动画执行的方向。
- animation-fill-mode: 描述动画的预运行和后运行样式。
- animation-play-state: 描述动画的播放/暂停状态。
示例
<html lang="en">
<head>
<style>
.box {
padding: 20px;
background-color: #3498db;
color: white;
font-size: 16px;
/* Name, duration, timing function, delay, repeat, direction, fill mode */
animation: changeColor 2s ease-in-out 1s infinite alternate both;
}
/* Animation Definition */
@keyframes changeColor {
0% {
background-color: #3498db;
}
100% {
background-color: #e74c3c;
}
}
</style>
</head>
<body>
<div class="box">Animate Me!</div>
</body>
</html>
CSS 动画属性列表
以下是 animation 属性的子属性
| 属性 | 描述 | 示例 |
|---|---|---|
| animation-composition | 指示当多个动画对同一属性具有同时影响时要应用的合成操作。 | |
| animation-delay | 指示动画是否应从动画开始处或途中某个位置开始,以及元素加载与动画序列开始之间应经过多长时间。 | |
| animation-direction | 指示动画的初始迭代是向前还是向后,以及之后的迭代是否应继续沿相同方向或每次执行序列时都改变方向。 | |
| animation-duration | 指示动画完成一个循环需要多长时间。 | |
| animation-fill-mode | 描述动画应用于其目标的预运行和后运行样式。 | |
| animation-iteration-count | 指示动画应重复多少次。 | |
| animation-name | 它给出描述动画关键帧的 @keyframes at-rule 的名称。 | |
| animation-play-state | 指示动画序列应播放还是暂停。 | |
| animation-timing-function | 描述用于指定动画中关键帧过渡的加速曲线。 |