CSS3 动画animation属性

CSS3的animation属性用法详解:
animation翻译成中文具有“动画”的意思,确实如此,animation属性就是用来定义元素的动画效果,当然和使用flash或者js制作的动画相比要粗糙一些,但是能够满足我们基本的动画需求,所以掌握此属性也是大有必要的。
一.基本知识:
在阅读下面的文章之前,建议事先参阅CSS3的@keyframes用法详解一章节。
使用transition属性可以实现动画过渡效果,但是此属性有个缺点,那就是从初始值到终止值的过渡过程可控性很差,也就是说不能够更为细致的控制动画效果,而animation属性则可以结合@keyframes定义的动画名称,更为细致的控制动画过渡过程。此属性和border、background等属性一样是复合属性。
关于transition属性可以参阅CSS3的transition属性详解一章节。 
语法结构:

animation:[[ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [ animation-iteration-count ] || [ animation-direction ]] [ , [ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [ animation-iteration-count ] || [ animation-direction ] || [animation-play-state] || [animation-fill-mode]]*

参数解析:
1.animation-name:此属性规定元素所应用的动画名称,此名称是由@keyframes定义的。
2.animation-duration:此属性用于规定动画的持续时间。
3.animation-timing-function:用于规定动画的过渡类型。
4.animation-delay:用于规定动画的开始执行的延迟时间。
5.animation-iteration-count:用于规定动画的重复次数。
6.animation-direction:用于规定动画循环中是否会反向运动。
7.animation-play-state:规定动画是否正在运行或暂停。
8.animation-fill-mode:规定对象动画时间之外的状态。
特别说明:
1.如果提供多组属性值,以逗号进行分隔。 
2.对应的脚本特性为animation。

代码实例:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="//m.sbmmt.com/" /> 
<title>php中文网</title>
<style type="text/css">
div{
  width:100px;
  height:100px;
  background:red;
  position:relative;
   
  animation:theanimation 5s infinite alternate;
  -webkit-animation:theanimation 5s infinite alternate ;
  -moz-animation:theanimation 5s infinite alternate ;
  -o-animation:theanimation 5s infinite alternate ;
  -ms-animation:theanimation 5s infinite alternate ;
}
@keyframes theanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-webkit-keyframes theanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-moz-keyframes theanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-o-keyframes theanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-ms-keyframes theanimation{
  0% {left:0px;}
  100% {left:200px;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>

以上代码可以设置div元素的left属性值从0px到200px进行动画过渡,并且能够无限次的重复循环,且能够进行反向运动。

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="//m.sbmmt.com/" /> 
<title>php中文网</title>
<style type="text/css">
div{
  width:100px;
  height:100px;
  background:red;
  position:relative;
   
  animation:theanimation 4s infinite alternate;
  -webkit-animation:theanimation 4s infinite alternate ;
  -moz-animation:theanimation 4s infinite alternate ;
  -o-animation:theanimation 4s infinite alternate ;
  -ms-animation:theanimation 4s infinite alternate ;
}
@keyframes theanimation{
  0%{top:0px;left:0px;background:red;}
  25%{top:0px;left:100px;background:blue;}
  50%{top:100px;left:100px;background:yellow;}
  75%{top:100px;left:0px;background:green;}
  100%{top:0px;left:0px;background:red;}
}
@-moz-keyframes theanimation{
  0% {top:0px;left:0px;background:red;}
  25%{top:0px;left:100px;background:blue;}
  50%{top:100px;left:100px;background:yellow;}
  75%{top:100px;left:0px;background:green;}
  100%{top:0px;left:0px;background:red;}
}
@-webkit-keyframes theanimation{
  0%{top:0px;left:0px;background:red;}
  25%{top:0px;left:100px;background:blue;}
  50%{top:100px;left:100px;background:yellow;}
  75%{top:100px;left:0px;background:green;}
  100%{top:0px;left:0px;background:red;}
}
@-o-keyframes theanimation{
  0%{top:0px;left:0px;background:red;}
  25%{top:0px;left:100px;background:blue;}
  50%{top:100px;left:100px;background:yellow;}
  75%{top:100px;left:0px;background:green;}
  100%{top:0px;left:0px;background:red;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>

上面的代码就比第一个要复杂一些了,下面介绍一下它的运行过程。
1.整个动画的总时间设置为4秒。
2.@keyframes定义了动画的四个阶段,0%-25%时间段将left属性值从0设置为100px,背景色从red转换为blue,25%-50%、50%-75%和75%-100%也是同样的道理。
3.并且能够实现无限循环和反向运动效果。

<!DOCTYPE html>  
<html>  
<head>  
<meta charset=" utf-8">  
<meta name="author" content="//m.sbmmt.com/" />  
<title>php中文网</title> 
<style type="text/css"> 
div{
  width:100px;
  height:100px;
  background:red;
  position:relative;
     
  animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate;
  -webkit-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate;
  -moz-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate;
  -o-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate;
  -ms-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate;
}
@keyframes firstanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-webkit-keyframes firstanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-moz-keyframes firstanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-o-keyframes firstanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@-ms-keyframes firstanimation{
  0% {left:0px;}
  100% {left:200px;}
}
@keyframes secondanimation{
  0% {top:0px;}
  100% {top:200px;}
}
@-webkit-keyframes secondanimation{
  0% {top:0px;}
  100% {top:200px;}
}
@-moz-keyframes secondanimation{
  0% {top:0px;}
  100% {top:200px;}
}
@-o-keyframes secondanimation{
  0% {top:0px;}
  100% {top:200px;}
}
@-ms-keyframes secondanimation{
  0% {top:0px;}
  100% {top:200px;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>

以上代码一次性为div设置了多组动画属性,中间用逗号分隔。

继续学习
||
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="//m.sbmmt.com/" /> <title>php中文网</title> <style type="text/css"> div{ width:100px; height:100px; background:red; position:relative; animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -webkit-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -moz-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -o-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; -ms-animation:firstanimation 5s infinite alternate,secondanimation 2s infinite alternate; } @keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-webkit-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-moz-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-o-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @-ms-keyframes firstanimation{ 0% {left:0px;} 100% {left:200px;} } @keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-webkit-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-moz-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-o-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } @-ms-keyframes secondanimation{ 0% {top:0px;} 100% {top:200px;} } </style> </head> <body> <div></div> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

CSS3从入门到精通教程

  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载
汪汪

汪汪

使用transition属性可以实现动画过渡效果

5年前    添加回复 0

回复
我又来了

我又来了

都一样的效果

5年前    添加回复 0

回复
男神

男神

animation属性就是用来定义元素的动画效果

5年前    添加回复 0

回复
大神,求带!

大神,求带!

实现无限循环和反向运动效果

5年前    添加回复 0

回复
橱窗的光

橱窗的光

使用transition属性可以实现动画过渡效果,但是此属性有个缺点,那就是从初始值到终止值的过渡过程可控性很差,也就是说不能够更为细致的控制动画效果

5年前 0

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~