Css3 3d animation Step1---介绍如何实现css动画

转载地址:http://www.iteye.com/topic/1116417


前言:

好久没写文章了,今天看到webQQ又一次升级。我用的chrome的画面切换甚至用到了css 3d animation+3dtransform。

 

这是本系列的第一篇文章,用于介绍仅用css实现3d动画。也许这篇文章也许过于超前,因为大部分的浏览器器,甚至包括FF,都还没有实现3d transform。涉及到animation的部分,你可以通过webkit内核的浏览器(Chrome or Safari)或者Firefox来浏览,但是涉及到3d transform的东西请用webkit的浏览器器浏览。

 

Css3 @KeyFrames Rule

什么是KeyFrames?直译过来就是关键帧。你可以把他理解为flash中的关键帧。他以关键字from开始,to结束。中间的部分都为百分比。意为动画到了某个百分比时,他的状态。而帧与帧之间的补间动画浏览器会帮你自动生成。

 

例如:

Html代码   收藏代码
  1. <style type="text/css">    
  2.         
  3.     @keyframes myfirst {    
  4.         from {background: red;}    
  5.         to {background: yellow;}    
  6.     }    
  7.     
  8.     @-moz-keyframes myfirst /* Firefox */ {    
  9.         from {background: red;}    
  10.         to {background: yellow;}    
  11.     }    
  12.     
  13.     @-webkit-keyframes myfirst /* Safari and Chrome */ {    
  14.         from {background: red;}    
  15.         to {background: yellow;}    
  16.     }    
  17.         
  18. </style>    
  为了支持将来可能有的标准,你不得不写3遍。

试着运行下面的代码,里面可能会有你没见过的属性,我们下面会讲到

 

 大家等待了两秒后,突然从画面左上角出现一个方块,花费5秒从红色变到黄色。可能大家已经猜到了

Html代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
  2. <html>    
  3. <head>    
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
  5. <title>Insert title here</title>    
  6.     
  7.     
  8. <style type="text/css">    
  9.         
  10.     @keyframes myfirst {    
  11.         from {background: red;}    
  12.         to {background: yellow;}    
  13.     }    
  14.     
  15.     @-moz-keyframes myfirst /* Firefox */ {    
  16.         from {background: red;}    
  17.         to {background: yellow;}    
  18.     }    
  19.     
  20.     @-webkit-keyframes myfirst /* Safari and Chrome */ {    
  21.         from {background: red;}    
  22.         to {background: yellow;}    
  23.     }    
  24.         
  25.     #sample {    
  26.         animation-name: myfirst;    
  27.         animation-duration: 5s;    
  28.         animation-timing-function: linear;    
  29.         animation-delay: 2s;    
  30.         animation-iteration-count: infinite;    
  31.         animation-direction: alternate;    
  32.         animation-play-state: running;    
  33.         /* Firefox: */    
  34.         -moz-animation-name: myfirst;    
  35.         -moz-animation-duration: 5s;    
  36.         -moz-animation-timing-function: linear;    
  37.         -moz-animation-delay: 2s;    
  38.         -moz-animation-iteration-count: infinite;    
  39.         -moz-animation-direction: alternate;    
  40.         -moz-animation-play-state: running;    
  41.         /* Safari and Chrome: */    
  42.         -webkit-animation-name: myfirst;    
  43.         -webkit-animation-duration: 5s;    
  44.         -webkit-animation-timing-function: linear;    
  45.         -webkit-animation-delay: 2s;    
  46.         -webkit-animation-iteration-count: infinite;    
  47.         -webkit-animation-direction: alternate;    
  48.         -webkit-animation-play-state: running;    
  49.     }    
  50.         
  51. </style>    
  52.     
  53. </head>    
  54. <body>    
  55.     <div id="sample" style="width:120px; height:120px;">    
  56.             
  57.     </div>    
  58. </body>    
  59. </html>    
 

 

animation-name: 和上面的@keyframe的名字对应起来,代表当前用的是何种变换

animation-duration: 代表动画开始到结束总共耗时多少时间

animation-timing-function: 代表动画的进行是均匀的还是先快后慢还是?他有这几个枚举值:linear:线性,ease:默认值,先慢后快,到了快一半的时候再变慢,ease-in:先,ease-out:先快后慢。ease-in-out:两头慢,中间快。cubic-bezier(n,n,n,n):自定义,n只能输入0-1之间的值。输入的值越大,移动的越慢,4个n代表了动画的4个阶段25%,50%,75%,100%。

 

也就是说,linear,ease等值,只是cubic-bezier在浏览器中的预设值,可以简单表示如下:

 

animation-delay:代表动画延迟多少时间开始,默认为0

Html代码   收藏代码
  1. #linear {animation-timing-function: cubic-bezier(0,0,1,1;}    
  2. #ease {animation-timing-function: cubic-bezier(0.25,0.1,0.25,1);}    
  3. #ease-in {animation-timing-function: cubic-bezier(0.42,0,1,1);}    
  4. #ease-out {animation-timing-function: cubic-bezier(0,0,0.58,1);}    
  5. #ease-in-out {animation-timing-function: cubic-bezier(0.42,0,0.58,1);}   
 

animation-iteration-count:代表动画执行多少次,默认为1.如果输入infinite,则代表无限次

animation-direction:默认值为normal,代表动画执行完后从头开始。如果设置为alternate,那么动画在结束后,第二遍会反着执行一遍

animation-play-state:默认值running,代表现在是执行状态。如果要让动画停止,只需要把这个值赋值为paused

 

 

好,接下来把上面的代码串联起来。

Html代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
  2. <html>    
  3. <head>    
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
  5. <title>Insert title here</title>    
  6.     
  7.     
  8. <style type="text/css">    
  9.         
  10.     @keyframes myfirst {    
  11.         from {left:0px; top:0px; background: red; }    
  12.         50% {left:100px; top:80px;}    
  13.         70%, 80% {transform: rotate(-360deg);left:240px; top:240px;}    
  14.         to {left:279px; top:279px; background: yellow;}    
  15.     }    
  16.     
  17.     @-moz-keyframes myfirst /* Firefox */ {    
  18.         from {left:0px; top:0px; background: red; }    
  19.         50% {left:100px; top:80px;}    
  20.         70%, 80% {-moz-transform: rotate(-360deg);left:240px; top:240px;}    
  21.         to {left:279px; top:279px; background: yellow;}    
  22.     }    
  23.     
  24.     @-webkit-keyframes myfirst /* Safari and Chrome */ {    
  25.         from {left:0px; top:0px; background: red; }    
  26.         50% {left:100px; top:80px;}    
  27.         70%, 80% {-webkit-transform: rotate(-360deg) scale(0.5, 0.5);left:240px; top:240px;}    
  28.         to {left:279px; top:279px; background: yellow;}    
  29.     }    
  30.         
  31.     #sample {    
  32.         animation-name: myfirst;    
  33.         animation-duration: 5s;    
  34.         animation-timing-function: linear;    
  35.         animation-iteration-count: infinite;    
  36.         animation-direction: alternate;    
  37.         /* Firefox: */    
  38.         -moz-animation-name: myfirst;    
  39.         -moz-animation-duration: 5s;    
  40.         -moz-animation-timing-function: linear;    
  41.         -moz-animation-iteration-count: infinite;    
  42.         -moz-animation-direction: alternate;    
  43.         /* Safari and Chrome: */    
  44.         -webkit-animation-name: myfirst;    
  45.         -webkit-animation-duration: 5s;    
  46.         -webkit-animation-timing-function: linear;    
  47.         -webkit-animation-iteration-count: infinite;    
  48.         -webkit-animation-direction: alternate;    
  49.     }    
  50.         
  51. </style>    
  52.     
  53. </head>    
  54. <body>    
  55.     <div style="width:400px; height:400px; border-style:dashed; border-width:1px; position:relative;">    
  56.         <div id="sample" style="width:120px; height:120px; position:absolute;"></div>    
  57.     </div>    
  58.         
  59. </body>    
  60. </html>    

你可能感兴趣的:(Css3 3d animation Step1---介绍如何实现css动画)