CSS动画(animation)详解

通过《CSS过渡》一节的学习我们知道,利用 transition 属性可以实现简单的过渡动画,但过渡动画仅能指定开始和结束两个状态,整个过程都是由特定的函数来控制的,不是很灵活。本节我们再来介绍一种更为复杂的动画 —— animation。

CSS 中的动画类似于 flash 中的逐帧动画,表现细腻并且非常灵活,使用 CSS 中的动画可以取代许多网页中的动态图像、Flash 动画或者 JavaScript 实现的特殊效果。

@keyframes 规则

要创建 CSS 动画,您首先需要了解 @keyframes 规则,@keyframes 规则用来定义动画各个阶段的属性值,类似于 flash 动画中的关键帧,语法格式如下:

@keyframes animationName {from {properties: value;}percentage {properties: value;}to {properties: value;}
}
// 或者
@keyframes animationName {0% {properties: value;}percentage {properties: value;}100% {properties: value;}
}

语法说明如下:

  • animationName:表示动画的名称;
  • from:定义动画的开头,相当于 0%;
  • percentage:定义动画的各个阶段,为百分比值,可以添加多个;
  • to:定义动画的结尾,相当于 100%;
  • properties:不同的样式属性名称,例如 color、left、width 等等。

下面我们来看一个简单的 @keyframes 规则示例:

@keyframes ball {0% { top: 0px; left: 0px;}25% { top: 0px; left: 350px;}50% { top: 200px; left: 350px;}75% { top: 200px; left: 0px;}100% { top: 0px; left: 0px;}
}

动画创建好后,还需要将动画应用到指定的 HTML 元素。要将动画应用到指定的 HTML 元素需要借助 CSS 属性,CSS 中提供了如下所示的动画属性:

  • animation-name:设置需要绑定到元素的动画名称;
  • animation-duration:设置完成动画所需要花费的时间,单位为秒或毫秒,默认为 0;
  • animation-timing-function:设置动画的速度曲线,默认为 ease;
  • animation-fill-mode:设置当动画不播放时(动画播放完或延迟播放时)的状态;
  • animation-delay:设置动画开始之前的延迟时间,默认为 0;
  • animation-iteration-count:设置动画被播放的次数,默认为 1;
  • animation-direction:设置是否在下一周期逆向播放动画,默认为 normal;
  • animation-play-state:设置动画是正在运行还是暂停,默认是 running;
  • animation:所有动画属性的简写属性。

下面就来详细介绍一下上述属性的使用。

animation-name

animation-name 属性用来将动画绑定到指定的 HTML 元素,属性的可选值如下:

描述
keyframename 要绑定到 HTML 元素的动画名称,可以同时绑定多个动画,动画名称之间使用逗号进行分隔
none 表示无动画效果,
示例代码如下:
<!DOCTYPE html>
<html>
<head><style>@keyframes ball {0% { top: 0px; left: 0px;}25% { top: 0px; left: 350px;}50% { top: 200px; left: 350px;}75% { top: 200px; left: 0px;}100% { top: 0px; left: 0px;} }div {width: 100px;height: 100px;border-radius: 50%;border: 3px solid black;position: relative;animation-name: ball;}</style>
</head>
<body><div></div>
</body>
</html>

注意:要想让动画成功播放,您还需要定义 animation-duration 属性,否则会因为 animation-duration 属性的默认值为 0,导致动画并不会播放。

animation-duration

animation-duration 属性用来设置动画完成一个周期所需要花费的时间,单位为秒或者毫秒。示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes ball {0% { top: 0px; left: 0px;}25% { top: 0px; left: 350px;}50% { top: 200px; left: 350px;}75% { top: 200px; left: 0px;}100% { top: 0px; left: 0px;} }div {width: 100px;height: 100px;border-radius: 50%;border: 3px solid black;position: relative;animation-name: ball;animation-duration: 2s;}</style>
</head>
<body><div></div>
</body>
</html>

运行结果如下图所示:

提示:动画若想成功播放,必须要定义 animation-name 和 animation-duration 属性。

animation-timing-function

animation-timing-function 属性用来设置动画播放的速度曲线,通过速度曲线的设置可以使动画播放的更为平滑。animation-timing-function 属性的可选值如下表所示:

描述
linear 动画从开始到结束的速度是相同的
ease 默认值,动画以低速开始,然后加快,在结束前变慢
ease-in 动画以低速开始
ease-out 动画以低速结束
ease-in-out 动画以低速开始,并以低速结束
cubic-bezier(n, n, n, n) 使用 cubic-bezier() 函数来定义动画的播放速度,参数的取值范围为 0 到 1 之间的数值

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes ball {0% {left: 0px;}50% {left: 350px;}100% {left: 0px;}}div {width: 100px;height: 100px;border-radius: 50%;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: ball;animation-duration: 2s;}.one {animation-timing-function: ease;}.two {animation-timing-function: ease-in;}.three {animation-timing-function: ease-out;}.four {animation-timing-function: ease-in-out;}</style>
</head>
<body><div class="one">ease</div><div class="two">ease-in</div><div class="three">ease-out</div><div class="four">ease-in-out</div>
</body>
</html>

运行结果如下图所示:

animation-fill-mode

animation-fill-mode 属性用来设置当动画不播放时(开始播放之前或播放结束之后)动画的状态(样式),属性的可选值如下:

描述
none 不改变动画的默认行为
forwards 当动画播放完成后,保持动画最后一个关键帧中的样式
backwards 在 animation-delay 所指定的时间段内,应用动画第一个关键帧中的样式
both 同时遵循 forwards 和 backwards 的规则

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes box {0% {transform: rotate(0);}50% {transform: rotate(0.5turn);}100% {transform: rotate(1turn);}}div {width: 100px;height: 100px;border-radius: 50%;float: left;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: box;animation-duration: 2s;animation-iteration-count: 1;animation-fill-mode: forwards;}</style>
</head>
<body><div>forwards</div>
</body>
</html>

animation-delay

animation-delay 属性用来定义动画开始播放前的延迟时间,单位为秒或者毫秒,属性的语法格式如下:

animation-delay: time;

其中参数 time 就是动画播放前的延迟时间,参数 time 既可以为正值也可以为负值。参数值为正时,表示延迟指定时间开始播放;参数为负时,表示跳过指定时间,并立即播放动画。

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes ball {0% {left: 0px;}50% {left: 350px;}100% {left: 0px;}}div {width: 100px;height: 100px;border-radius: 50%;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: ball;animation-duration: 2s;}.one {animation-delay: 0.5s;}.two {animation-delay: -0.5s;}</style>
</head>
<body><div class="one">0.5s</div><div class="two">-0.5s</div>
</body>
</html>

运行结果如下图所示:

animation-iteration-count

animation-iteration-count 属性用来定义动画播放的次数,属性的可选值如下:

描述
n 使用具体数值定义动画播放的次数,默认值为 1
infinite 表示动画无限次播放

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes box {0% {transform: rotate(0);}50% {transform: rotate(0.5turn);}100% {transform: rotate(1turn);}}div {width: 100px;height: 100px;float: left;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: box;animation-duration: 2s;}.one {animation-iteration-count: 1;}.two {margin-left: 50px;animation-iteration-count: infinite;}</style>
</head>
<body><div class="one">1</div><div class="two">infinite</div>
</body>
</html>

运行结果如下图所示:


animation-direction

animation-direction 属性用来设置是否轮流反向播放动画,属性的可选值如下:

描述
normal 以正常的方式播放动画
reverse 以相反的方向播放动画
alternate 播放动画时,奇数次(1、3、5 等)正常播放,偶数次(2、4、6 等)反向播放
alternate-reverse 播放动画时,奇数次(1、3、5 等)反向播放,偶数次(2、4、6 等)正常播放

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes box {0% {transform: rotate(0);}50% {transform: rotate(0.5turn);}100% {transform: rotate(1turn);}}div {width: 100px;height: 100px;float: left;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: box;animation-duration: 2s;animation-iteration-count: infinite;}.one {animation-direction: reverse;}.two {margin-left: 50px;animation-direction: alternate;}</style>
</head>
<body><div class="one">reverse</div><div class="two">alternate</div>
</body>
</html>

运行结果如下图所示:

animation-play-state

animation-play-state 属性用来设置动画是播放还是暂停,属性的可选值如下:

描述
paused 暂停动画的播放
running 正常播放动画

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes box {0% {transform: rotate(0);}50% {transform: rotate(0.5turn);}100% {transform: rotate(1turn);}}div {width: 100px;height: 100px;float: left;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation-name: box;animation-duration: 2s;animation-iteration-count: infinite;}.one {animation-play-state: running;}.two {margin-left: 50px;animation-play-state: paused;}</style>
</head>
<body><div class="one">running</div><div class="two">paused</div>
</body>
</html>

运行结果如下图所示:

animation

animation 属性是 animation-name、animation-duration、animation-timing-function、animation-delay、animation-iteration-count、animation-direction、animation-fill-mode、animation-play-state 几个属性的简写形式,通过 animation 属性可以同时定义上述的多个属性,语法格式如下:

animation: animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction animation-fill-mode animation-play-state;

其中每个参数分别对应上面介绍的各个属性,如果省略其中的某个或多个值,则将使用该属性对应的默认值。

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>@keyframes box {0% {transform: rotate(0);}50% {transform: rotate(0.5turn);}100% {transform: rotate(1turn);}}div {width: 100px;height: 100px;border-radius: 50%;float: left;border: 3px solid black;text-align: center;line-height: 100px;position: relative;animation: box 2s linear 0s infinite alternate;}</style>
</head>
<body><div>animation</div>
</body>
</html>

运行结果如下图所示:

CSS动画(animation)详解相关推荐

  1. css动画(详解带图)

    css动画 过渡 过渡 (transition)的作用 常用属性 实例 动画 动画的简介 设置关键帧 动画常用属性 实例 变形 变形(transform)的简介 常用属性(transform) Z轴平 ...

  2. animate.css动画样式详解

    一.使用步骤 <!-- <link rel="stylesheet" href="https://www.jq22.com/jquery/animate-3. ...

  3. CSS3动画属性 animation详解(看完就会)

    CSS3动画属性 animation 文章包含个人理解错误请指出   往期文章 [css高级]变量详解 轮播图swiper框架的基本使用 [Transform3D]转换详解(看完就会) [css动画] ...

  4. 利用Animation控件制作帧动画过程详解

    利用Animation控件制作帧动画过程详解 前言 通过Animation控件来达到序列图的播放(素材和示例视频在文章末尾) 一.序列图 (此素材为已经分割好的序列图) 如若序列图在一张图片上,请参考 ...

  5. CSS3动画特效详解

    大家都知道网页的3大组成部分,分别是结构,表现和行为.就像我们要盖一座房子,要地基要打的扎实,房子的架构要稳固,而我们网页当中的地基和架构就是Html,当我们盖好了房子,最终的美观度还是看我们的装修风 ...

  6. HTML使用CSS的方法详解

    HTML使用CSS的方法详解 在尝试学习 CSS 之前,您应该了解 HTML 的基础知识,或者同时学习 HTML 和 CSS. html是超文本编辑语言,是用来定义页面结构的,html有很多图文或者视 ...

  7. Android 动画框架详解,第 1 部分

    2019独角兽企业重金招聘Python工程师标准>>> Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果,本文将向读者阐述 Android 的动画框 ...

  8. Unity动画系统详解5:BlendTree混合树是什么?

    摘要:"Animator中有一个功能,用来解决多个动画之间的混合,经常用于移动动画之间的混合,这个功能叫做BlendTree,混合树." 洪流学堂,让你快人几步.你好,我是跟着大智 ...

  9. unity velocity_Unity动画系统详解5:BlendTree混合树是什么?

    摘要:"Animator中有一个功能,用来解决多个动画之间的混合,经常用于移动动画之间的混合,这个功能叫做BlendTree,混合树." 洪流学堂,让你快人几步.你好,我是跟着大智 ...

  10. T4M插件放入unity后怎么找不到_Unity动画系统详解4:如何用代码控制动画?

    摘要:通过上一篇咱们知道了播放动画需要使用Animator,那么如何用代码控制动画呢? 洪流学堂,让你快人几步.你好,我是跟着大智学Unity的萌新,我叫小新,这几周一起来复(yu)习(xi)动画系统 ...

最新文章

  1. R语言使用ggpubr包的ggarrange函数组合多张结论图(水平并排组合)
  2. 第十六节:语法总结(3)(C#6.0和C#7.0新语法)
  3. 复旦大学计算机应用能力,复旦大学计算机应用能力水平测试(FudanComputerSkills.doc...
  4. [BZOJ2725/Violet 6]故乡的梦
  5. 超一流 | 从XLNet的多流机制看最新预训练模型的研究进展
  6. es创建索引设置字段不分词_java整合es指定字段不分词搜索
  7. python是什么语言-终于明白python语言的特点是什么
  8. (原创)c#学习笔记03--变量和表达式03--变量04--变量的声明和赋值
  9. linux系统的总父目录,Linux虚拟文件系统-资料路径名的解析(2)-回退父目录
  10. 安卓设备投影到电脑PC端的简单实现
  11. 简体中文与繁体中文的互相转换
  12. linux25g网卡设定,工程师笔记|刷新Mellnaox 25G网卡固件的方法,你get了吗
  13. 淘宝首页交互5--选项卡
  14. android手机病毒多杀毒,手机中毒不用慌!教你几招安卓手机怎么彻底的进行杀毒...
  15. centos7.2安装dcos
  16. Android实现手机内存存储功能
  17. MB10F-ASEMI整流桥MB10F
  18. 两大权威机构点赞百度AI,“技术为王”时代的中国旗帜
  19. dataTable固定列固定行
  20. 由蓝眼睛岛问题引发的思考

热门文章

  1. 本地电脑与实验室服务器的连接
  2. php构造函数的继承方法
  3. Android多媒体实现拍照,调用相册,音频等功能
  4. SSO 单点登录实现方案
  5. 黑马程序员,黑马论坛----JavaEE+云计算6期,就业率96.83%,平均薪水7563元!
  6. 一位浙大教授的激情演讲[上]
  7. Py之twilio:python库之twilio的简介、安装、使用方法之详细攻略
  8. twilio_使用Android Things和Twilio构建短信通知系统
  9. “深绿” 及 AlphaGo 对QQ分分彩源码出租指挥与控制智能化的启示
  10. python文本统计单词_Python实现文本单词统计