深入理解CSS动画animation
前面的话
transition过渡是通过初始和结束两个状态之间的平滑过渡实现简单动画的;而animation则是通过关键帧@keyframes来实现更为复杂的动画效果。本文将介绍关于animation动画的相关知识
定义
和transition类似,animation也是一个复合属性,包括animation-name、animation-duration、animation-timing-function、animation-delay、animation-iteration-count、animation-direction、animation-play-state、animation-fill-mode共8个子属性
[注意]IE9-不支持;safari4-8、IOS3.2-8.4、android2.1-4.4.4需要添加-webkit-前缀
animation-name: 动画名称(默认值为none)
animation-duration: 持续时间(默认值为0)
animation-timing-function: 时间函数(默认值为ease)
animation-delay: 延迟时间(默认值为0)
animation-iteration-count: 循环次数(默认值为1)
animation-direction: 动画方向(默认值为normal)
animation-play-state: 播放状态(默认值为running)
animation-fill-mode: 填充模式(默认值为none)
div{
width: 300px;
height: 100px;
background-color: pink;
animation-name: test;
animation-duration: 3s;
animation-timing-function: ease;
animation-delay: 0s;
animation-iteration-count: infinite;
animation-direction: normal;
animation-play-state: running;
animation-fill-mode: none;
}
/* 关于keyframes关键帧的内容稍后介绍 */
@keyframes test{
0%{background-color: lightblue;}
30%{background-color: lightgreen;}
60%{background-color: lightgray;}
100%{background-color: black;}
}
关键帧
animation制作动画效果需要两步,首先用关键帧声明动画,再用animation调用动画
关键帧的语法是以@keyframes开头,后面紧跟着动画名称animation-name。from等同于0%,to等同于100%。百分比跟随的花括号里面的代码,代表此时对应的样式
@keyframes animation-name{
from | %{}
n%{}
to | %{}
}
【1】百分比顺序不一定非要从0%到100%排列,最终浏览器会自动按照0%-100%的顺序进行解析
[注意]0%不可以省略百分号
@keyframes test{
%{background-color: black;}
%{background-color: lightgray;}
%{background-color: lightgreen;}
%{background-color: lightblue;}
}
div{
width: 300px;
height: 100px;
background-color: pink;
animation-name: test;
animation-duration: 3s;
animation-iteration-count: infinite;
}
【2】如果存在负百分数或高于100%的百分数,则该关键帧将被忽略
/* -20%和120%对应的代码无效*/
@keyframes test{
-%{background-color: red;}
%{background-color: lightblue;}
%{background-color: lightgreen;}
%{background-color: lightgray;}
%{background-color: black;}
%{background-color: yellow;}
}
【3】如果0%或100%不指定关键帧,将使用该元素默认的属性值
/* 0%和100%对应的颜色是默认值pink*/
@keyframes test{
%{background-color: lightgreen;}
%{background-color: lightgray;}
}
【4】若存在多个@keyframes,浏览器只识别最后一个@keyframes里面的值
/* 后面覆盖前面 */
@keyframes test{
%{background-color: lightblue;}
%{background-color: lightgreen;}
%{background-color: lightgray;}
%{background-color: black;}
}
@keyframes test{
%{background-color: blue;}
%{background-color: green;}
%{background-color: gray;}
%{background-color: black;}
}
【5】空的keyframes规则是有效的,它们会覆盖前面有效的关键帧规则
/* 后面覆盖前面 */
@keyframes test{
%{background-color: lightblue;}
%{background-color: lightgreen;}
%{background-color: lightgray;}
%{background-color: black;}
}
@keyframes test{
}
动画属性
动画名称
animation-name
值: none | <single-animation-name> [, <single-animation-name> ]*
初始值: none
应用于: 所有元素
继承性: 无
<single-animation-name>: none | 自定义动画名称
【1】如果多个动画试图修改相同的属性,那么动画列表的后面覆盖前面
/* animation-name的顺序是test1,test2,且它们修改的是同样的属性,后面覆盖前面,所以test2有效,test1无效 */
div{
width: 300px;
height: 100px;
background-color: pink;
animation-name: test1,test2;
animation-duration: 3s;
animation-iteration-count: infinite;
}
@keyframes test2{
%{background-color: blue;}
%{background-color: green;}
%{background-color: gray;}
%{background-color: black;}
}
@keyframes test1{
%{background-color: lightblue;}
%{background-color: lightgreen;}
%{background-color: lightgray;}
%{background-color: black;}
}
【2】如果动画的其他7个子属性和动画名称的长度不同,动画名称列表的长度决定最终的长度,多余的值无余,缺少的值按照顺序进行重复
div{
width: 300px;
height: 100px;
position: relative;
background-color: pink;
animation-name: test1,test2,test3;
animation-duration: 3s,1s;
animation-iteration-count: infinite;
}
@keyframes test1{
%{background-color: lightblue;}
%{background-color: lightgreen;}
%{background-color: lightgray;}
%{background-color: black;}
}
@keyframes test2{
%{font-size: 20px;}
%{font-size: 30px;}
%{font-size: 40px;}
%{font-size: 50px;}
}
@keyframes test3{
%{left: 0px;}
%{left: 30px;}
%{left: 40px;}
%{left: 50px;}
}
<div>测试文字</div>
持续时间
持续时间指完成动画的时间
animation-duration
值: <time> [, <time>]*
初始值: 0s
应用于: 所有元素
继承性: 无
animation-duration: <time>[,<time>]*
0s意味着动画没有时间,持续时间不能为负值
animation-name: test1,test2;
/*test1的持续时间设置为负值,将使得整个动画持续时间都失效,因此test2也将没有动画效果 */
animation-duration: -1s,1s;
时间函数
animation-timing-function
值: <single-timing-function> [, <single-timing-function>]*
初始值: ease
应用于: 所有元素
继承性: 无
animation的时间函数类似于transition的时间函数。时间函数可以应用于整个动画中,也可以应用于关键帧的某两个百分比之间
div{
width: 300px;
height: 100px;
position: relative;
background-color: pink;
animation-name: test;
animation-duration: 5s;
animation-iteration-count: infinite;
}
@keyframes test{
%{left: 0px;animation-timing-function: ease;}
%{left: 50px;animation-timing-function: linear;}
%{left: 100px;animation-timing-function: ease-in;}
%{left: 150px;animation-timing-function: ease-out;}
%{left: 200px;animation-timing-function: step-start;}
%{left: 250px;animation-timing-function: step-end;}
}
循环次数
animation-iteration-count
值: infinite | <number>[,infinite | <number>]*
初始值: 1
应用于: 所有元素
继承性: 无
默认为1,可以是整数也可以小数,但不能是0和负数。如果为infinite则表示无限次动画
动画方向
动画方向用来定义是否动画需要反向播放
animation-direction
值: <single-animation-direction>[,<single-animation-direction> ]*
初始值: normal
应用于: 所有元素
继承性: 无
<single-animation-direction> = normal | reverse | alternate | alternate-reverse
normal: 正向播放
reverse: 反向播放
alternate: 若动画只播放一次,则和正向播放一样。若播放两次以上,偶数次效果为反向播放
alternate-reverse: 若动画只播放一次,则和反向播放一样。若播放两次以上,偶数次效果为正向播放
[注意]safari浏览器不支持reverse属性和alternate-reverse属性
动画状态
animation-play-state
值:running | paused[,running | paused]*
初始值: running
应用于: 所有元素
继承性: 无
running表示播放中,paused表示动画暂停
延迟时间
定义延迟多少时间后动画开始播放
animation-delay
值: <single-animation-delay>[,<single-animation-delay> ]*
初始值: 0s
应用于: 所有元素
继承性: 无
<single-animation-delay>= <time>[,<time>]*
[注意]该延迟时间是指整个动画的延迟时间,而不是每个循环的延迟时间,只在动画开始时进行一次时间延迟
如果该值是负值,则表示动画的起始时间从0s变为延迟时间的绝对值
填充模式
定义动画开始帧之前和结束帧之后的动作
[注意]android2.1-3不支持animation-fill-mode
animation-fill-mode
值: <single-animation-fill-mode>[,<single-animation-fill-mode> ]*
初始值: none
应用于: 所有元素
继承性: 无
<single-animation-fill-mode> = none | forwards | backwards | both
none: 动画结束后,元素移动到初始状态
[注意]初始状态并不是指0%的元素状态,而是元素本身属性值
forwards: 元素停在动画结束时的位置
[注意]动画结束时的位置并不一定是100%定义的位置,因为动画有可能反向运动,也有可能动画的次数是小数
backwards:在animation-delay的时间内,元素立刻移动到动画开始时的位置。若元素无animation-delay时,与none的效果相同
[注意]动画开始时的位置也不一定是0%定义的位置,因为动画有可能反向运动。
both: 同时具有forwards和backwards的效果
[注意]当持续时间animation-duration为0s时,animation-fill-mode依然适用,当animation-fill-mode的值为backwards时,动画填充在任何animation-delay的阶段。当animation-fill-mode的值为forwards时,动画将保留在100%的关键帧上
多值
animation
值: <single-animation>[,<single-animation> ]*
初始值: 无
应用于: 所有元素
继承性: 无
<single-animation> = <single-animation-name> || <single-animation-duration> || <single-animation-timing-function> || <single-animation-delay> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || <single-animation-play-state>
[注意]持续时间在前,延迟时间在后,若只存在一个时间,则是持续时间
div{
width: 300px;
height: 100px;
background-color: pink;
animation: 1s test1,infinite test2 2s 1s;
}
@keyframes test1{
%{background-color: red;}
%{background-color: blue;}
%{background-color: green;}
}
@keyframes test2{
%{color: white;}
}
API
animation涉及到的事件有animationstart、animationend、animationiteration三个。这三个事件的bubbles都是yes,cancelable都是no
[注意]对于safari浏览器,animation的事件为webkitAnimationStart、webkitAnimationEnd、webkitAnimationIteration
[注意]动画事件只支持DOM2级事件处理程序的写法
animationstart
发生在动画开始时
【1】如果存在delay,且delay为正值,则元素等待延迟完毕后,再触发该事件
【2】如果delay为负值,则元素先将初始值变为delay的绝对值时,再触发该事件
oSb.addEventListener('animationstart',function(){
this.innerHTML = '动画开始';
this.style.background = 'lightgreen';
},false);
animationend
发生在动画结束时
test.addEventListener('animationend',function(){
this.style.background="lightgreen";
this.innerHTML = '动画结束';
},false);
animationiteration
发生在动画的一次循环结束时,只有当iteration-count循环次数大于1时,触发该事件
var i = ;
oSb.addEventListener('animationiteration',function(){
i++;
this.innerHTML = i;
},false);
【补充】
只有改变animation-name时,才会使animation动画效果重新触发
oSb.style.animationName = 'none';
setTimeout(function(){
oSb.style.animationName = 'test';
},);
属性
这三个事件的事件对象,都有animationName和elapsedTime属性这两个私有属性
animationName属性:返回产生过渡效果的CSS属性名
elapsedTime属性:动画已经运行的秒数
[注意]对于animationstart事件,elapsedTime属性等于0,除非animation-delay属性等于负值
<style>
#test{height:100px;width:300px;background-color:lightblue;animation:anim 2s 3;}
@keyframes anim{
0%{height: 100px;}
50%{height: 50px;}
100%{height: 0;}
}
</style> <button id='reset'>还原</button>
<div id="test"></div>
<script>
reset.onclick = function(){
history.go();
}
test.addEventListener("animationstart", listener, false);
test.addEventListener("animationend", listener, false);
test.addEventListener("animationiteration", listener, false);
function listener(e){
e = e || event;
var li = document.createElement("li");
switch(e.type) {
case "animationstart":
li.innerHTML = "Started: elapsed time is " + e.elapsedTime;
break;
case "animationend":
li.innerHTML = "Ended: elapsed time is " + e.elapsedTime;
break;
case "animationiteration":
li.innerHTML = "New loop started at time " + e.elapsedTime;
break;
}
test.appendChild(li);
}
</script>
深入理解CSS动画animation的更多相关文章
- amazeui学习笔记--css(常用组件15)--CSS动画Animation
amazeui学习笔记--css(常用组件15)--CSS动画Animation 一.总结 1.css3动画封装:CSS3 动画封装,浏览器需支持 CSS3 动画. Class 描述 .am-anim ...
- css动画-animation各个属性详解(转)
CSS3的animation很容易就能实现各种酷炫的动画,虽然看到别人的成果图会觉得很难,但是如果掌握好各种动画属性,做好酷炫吊炸天的动画都不在话下,好,切入正题. 一.动画属性: 动画属性包括:①a ...
- CSS动画 animation与transition
一.区分容易混淆的几个属性和值 先区分一下css中的几个属性:animation(动画).transition(过渡).transform(变形).translate(移动). CSS3中的trans ...
- css动画 animation
今天用css做了一个简单的三角上下移动的一个小动画,说白了就是在改变该物体的height值.除了这个方法,还可以用js. 一.在用css写动画时,一定要记住兼容性问题.如何解决该兼容性?在前面加内核前 ...
- CSS动画animation
transition: 过渡动画transition-property: 属性transition-duration: 间隔transition-timing-function: 曲线transiti ...
- css 动画animation基本属性(干货)
/* 动画名称 */ animation-name: cloud; /* 属性定义动画完成一个周期所需要的时间,以秒或毫秒计 */ animation-duration:1s; /* 属性定义动画何时 ...
- CSS动画-transition/animation
HTML系列: 人人都懂的HTML基础知识-HTML教程(1) HTML元素大全(1) HTML元素大全(2)-表单 CSS系列: CSS基础知识筑基 常用CSS样式属性 CSS选择器大全48式 CS ...
- 使用css动画实现领积分效果
最近项目中要做一个领积分的效果,根据老板的描述,这个效果类似于支付宝蚂蚁森林里的领取能量.整体效果是就是在树周围飘着几个积分元素,上下滑动,类似星星闪烁,点击领取后,沿着树中心的位置滑动并消失,树上的 ...
- 前端深入之css篇丨2020年前,彻底掌握css动画【animation】
写在前面 马上就2020年了,不知道小伙伴们今年学习了css3动画了吗? 说起来css动画是一个很尬的事,一方面因为公司用css动画比较少,另一方面大部分开发者习惯了用JavaScript来做动画,所 ...
随机推荐
- 几个比较”有意思“的JS脚本
1.获取内网和公网真实IP地址(引用地址) <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- C++标准库实现WAV文件读写
在上一篇文章RIFF和WAVE音频文件格式中对WAV的文件格式做了介绍,本文将使用标准C++库实现对数据为PCM格式的WAV文件的读写操作,只使用标准C++库函数,不依赖于其他的库. WAV文件结构 ...
- c# 基础 object ,new操作符,类型转换
参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...
- myeclipse 内存不够用报错PermGen space 和 An internal error has occurred.
最近项目中又增加了新的模块,项目的代码又多了不少.运行的时候总是报如下错误 Exception in thread "http-apr-80-exec-6" java.lang.O ...
- 小程序用户反馈 - HotApp小程序统计仿微信聊天用户反馈组件,开源
用户反馈是小程序开发必要的一个功能,但是和自己核心业务没关系,主要是产品运营方便收集用户的对产品的反馈.HotApp推出了用户反馈的组件,方便大家直接集成使用 源码下载地址: https://gith ...
- 为什么 Android Studio 工程文件夹占用空间这么大?我们来给它减减肥
偶然中发现Android Studio的工程文件夹比ADT Bundle的大很多.用Android Studio新建一个空工程,工程文件夹大小为30M,运行一次后大小为40M.同样用ADT Bundl ...
- 猫哥网络编程系列:详解 BAT 面试题
从产品上线前的接口开发和调试,到上线后的 bug 定位.性能优化,网络编程知识贯穿着一个互联网产品的整个生命周期.不论你是前后端的开发岗位,还是 SQA.运维等其他技术岗位,掌握网络编程知识均是岗位的 ...
- 在 Linux 打造属于自己的 Vim
Linux 系统中很多东西都是以脚本代码.配置文件的形式存在,使用 Linux 系统时,需经常对这些文件进行编辑.很显然,如果没有文本编辑器,江湖之路寸步难行. 我的选择是 Vim.Vim 是 Lin ...
- ASP.NET MVC 5 Web编程5 -- 页面传值的方式
本篇文章将讲述MVC的页面传值方式,具体包括:后端向前端传值(Controller向View传值):前端向后端传值(View向Controller传值):Action与Action之间的传值. 回顾 ...
- BCL中String.Join的实现
在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...