CSS中的animation属性可用于为许多其他CSS属性设置动画,例如颜色,背景色,高度或宽度。 每个动画都需要使用@keyframes这种at-rule语句定义,然后使用animation属性来调用它,如下所示:

.element {
animation: pulse 5s infinite;
} @keyframes pulse {
0% {
background-color: #001F3F;
}
100% {
background-color: #FF4136;
}
}

  

<!DOCTYPE html>
<html>
<head lang="zh-CN">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <title></title>
<style>
.element {
width: 100%;
height: 100%;
animation: pulse 5s infinite;
} @keyframes pulse {
0% {
background-color: #001F3F;
} 100% {
background-color: #FF4136;
}
}
html,
body {
height: 100%;
}
</style>
</head>
<body>
<div class="element"></div>
</body>
</html>

  

每个@keyframes的 at-rule CSS语句规则都定义了在动画过程中的特定时刻应该发生的情况。 例如,0%是动画的开始,而100%是动画的结束。可以通过简写animation属性或它的八个子属性控制这些关键帧,以更好地控制应该如何操纵这些关键帧。

子属性

animation-name:声明要操纵的@keyframes规则的名称。

animation-duration:动画完成一个周期所需的时间。

animation-timing-function:建立预设的加速曲线,例如缓动或线性。

animation-delay:加载元素到动画序列开始之间的时间。

animation-direction:设置循环后动画的方向。 其默认值在每个周期重置。

animation-iteration-count:应该执行动画的次数。

animation-fill-mode:设置在动画之前/之后应用的值。

例如,您可以将动画的最后状态设置为保留在屏幕上,或者可以将其设置为切换回动画开始之前的状态。

animation-play-state:暂停/播放动画。

可以如下面所示使用这些子属性:

@keyframes stretch {
/* 这里声明动画动作 */
} .element {
animation-name: stretch;
animation-duration: 1.5s;
animation-timing-function: ease-out;
animation-delay: 0s;
animation-direction: alternate;
animation-iteration-count: infinite;
animation-fill-mode: none;
animation-play-state: running;
} /* 等同于*/ .element {
animation:
stretch
1.5s
ease-out
0s
alternate
infinite
none
running;
}

  

<!DOCTYPE html>
<html> <head lang="zh-CN">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <title></title>
<style>
.element {
height: 250px;
width: 250px;
margin: 0 auto;
background-color: red;
animation-name: stretch;
animation-duration: 1.5s;
animation-timing-function: ease-out;
animation-delay: 0;
animation-direction: alternate;
animation-iteration-count: infinite;
animation-fill-mode: none;
animation-play-state: running;
} @keyframes stretch {
0% {
transform: scale(.3);
background-color: red;
border-radius: 100%;
} 50% {
background-color: orange;
} 100% {
transform: scale(1.5);
background-color: yellow;
}
} body,
html {
height: 100%;
} body {
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head> <body>
<div class="element"></div>
</body> </html>

  

以下是这些子属性中每个属性可以采用的值的完整列表:

 
animation-timing-function
ease, ease-out, ease-in, ease-in-out, linear, cubic-bezier(x1, y1, x2, y2) (e.g. cubic-bezier(0.5, 0.2, 0.3, 1.0))
animation-duration
Xs or Xms
animation-delay
Xs or Xms
animation-iteration-count
X
animation-fill-mode
forwards, backwards, both, none
animation-direction
normal, alternate
animation-play-state
paused, running, running
 

多个步骤

如果动画具有相同的开始和结束属性,则在@keyframes中用逗号分隔0%和100%值很有用:

@keyframes pulse {
0%, 100% {
background-color: yellow;
}
50% {
background-color: red;
}
}

  

多个动画

您也可以用逗号分隔值,以在选择器上声明多个动画。 在下面的示例中,我们想在@keyframe中更改圆的颜色,同时还要将其与另一边左右轻轻一碰。

.element {
animation:
pulse 3s ease infinite alternate,
nudge 5s linear infinite alternate;
}

  

<!DOCTYPE html>
<html> <head lang="zh-CN">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <title></title>
<style>
.element {
height: 400px;
width: 400px;
background-color: red;
animation:
pulse 3s ease infinite alternate,
nudge 5s linear infinite alternate;
border-radius: 100%;
} @keyframes pulse { 0%,
100% {
background-color: red;
} 50% {
background-color: orange;
}
} @keyframes nudge { 0%,
100% {
transform: translate(0, 0);
} 50% {
transform: translate(150px, 0);
} 80% {
transform: translate(-150px, 0);
}
} html,
body {
height: 100%;
} body {
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head> <body>
<div class="element"></div>
</body> </html>

  

性能

对大多数属性进行动画处理是性能方面的考虑,因此在对任何属性进行动画处理之前,我们应谨慎行事。 但是,可以安全地对某些组合进行动画处理:

transform: translate()

transform: scale()

transform: rotate()

opacity

哪些属性可以设置动画?

MDN有一个可以设置动画的CSS属性列表。 可设置动画的属性倾向于颜色和数字。 不可动画属性的一个示例是背景图像。

CSS3之animation属性的更多相关文章

  1. css3中animation属性animation-timing-function知识点以及其属性值steps()

    在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...

  2. css3使用animation属性实现炫酷效果

    animation-name 动画名称,可以有多个值,用逗号隔开,表示绑定了多个动画 animation-name属性为动画指定一个名称 animation-name兼容主流的浏览器,不过还是需要加前 ...

  3. css3 animation 属性众妙

    转自:凹凸实验室(https://aotu.io/notes/2016/11/28/css3-animation-properties/) 本文不会详细介绍每个 css3 animation 属性(需 ...

  4. CSS3中动画属性transform、transition和animation

    Transform:变形 在网页设计中,CSS被习惯性的理解为擅长表现静态样式,动态的元素必须借助于javascript才可以实现,而CSS3的出现改变了这一思维方式.CSS3除了增加革命性的创新功能 ...

  5. CSS3学习之 animation 属性

    发现animation这个新属性很有趣,在此学习,并整理下!  浏览器支持: Internet Explorer 10.Firefox 以及 Opera 支持 animation 属性: Safari ...

  6. CSS3中动画属性transform、transition 和 animation

    CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明:        transform   从字面来看transform的释义为改变,使 ...

  7. CSS3 Transform、Transition和Animation属性总结

    CSS3的三个与变形和动画啊相关的属性: Transform 浏览器支持情况: Internet Explorer 10.Firefox.Opera 支持 transform 属性. Internet ...

  8. CSS3 animation属性中的steps实现GIF动图(逐帧动画)

    相信 animation 大家都用过很多,知道是 CSS3做动画用的.而我自己就只会在 X/Y轴 上做位移旋转,使用 animation-timing-function 规定动画的速度曲线,常用到的 ...

  9. css3中的animation属性

    作用:通过给元素添加animation属性,可以赋予该元素动画效果. <!DOCTYPE html><html>    <head>        <styl ...

随机推荐

  1. Swoole实时任务异步调用Demo

    server.php <?php class Server { private $serv; private $logFilePath = "/data/wwwroot/houtai/ ...

  2. java -inally转

    1.不管有木有出现异常,finally块中代码都会执行: 2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没有返 ...

  3. Vue 全宇宙最浪 VSCode 配置、插件

    别人的那一堆配置.插件我就不写了- 首先进入官网下载并安装. 基本配置 在编译器 文件 >> 首选项 >> 设置,可以拷贝相面的选项后搜索相关配置 设置制表符等于空格数为 2: ...

  4. 本地环境Django配置问题

    Django本地环境出现的问题 当你的前端出现这个问题的时候 你只需要吧setting.py 中的DEBUG 改为 True,即可 我原来是是DEBUG = False 本人亲测有用!!!

  5. java 环境变量配置(win10)

    到官网下载jdk,链接https://www.oracle.com/technetwork/java/javase/downloads/index.html 安装好,进行环境变量配置,打开环境变量 1 ...

  6. jquery的实时触发事件(textarea实时获取中文个数)

    jquery的实时触发事件(textarea实时获取中文个数) (2014-09-16 11:49:50) 转载▼ 标签: 实时触发事件 中文个数 onpropertychange oninput o ...

  7. 解了这14道C语言谜题后,所有人都失声了!我来带你深入了解C!

    本文展示了14个C语言的迷题以及答案,代码应该是足够清楚的,而且有相当的一些例子可能是我们日常工作可能会见得到的.通过这些迷题,希望你能更了解C语言. 如果你不看答案,不知道是否有把握回答各个谜题?让 ...

  8. 【原创】xenomai3.1+linux构建linux实时操作系统-基于X86_64和arm

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 一.概要 二.环境准备 1.1 安装内核编译工 ...

  9. 运行bee run之后出现的错误以及解决方法Failed to build the application:

      运行bee run之后出现的错误以及解决方法 创建一个beego项目 bee new myapp 在该项目执行下面的代码 bee run 出现的问题 2020/04/22 21:12:07 INF ...

  10. Windows和Mac两种操作系统下CSS不兼容问题的解决

    这两天碰到一个问题,就是一个小图标的大小和定位的位置在不同的操作系统下是不一样的. 查了下资料,自己解决出来了,整理如下: html: <i :class="['cursor-poin ...