最近一个小游戏项目用到了CSS3的动画属性,例如transition、transform、animation。经过三个星期,终于做完了,利用周末好好梳理总结一下。
Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 规则和 animation 属性。
Chrome 和 Safari 需要前缀 -webkit-。
animation: name duration timing-function delay iteration-count direction;
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>delay</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 7 8 <style type="text/css"> 9 @-webkit-keyframes delay{ 10 11 99%{ 12 -webkit-transform:translate(100px,0); 13 } 14 15 } 16 .delay{ 17 18 width:100px;height:100px; 19 background-color: #000; 20 -webkit-animation:delay 1s linear 2s infinite; 21 animation:delay 1s linear 2s infinite; 22 } 23 </style> 24 </head> 25 <body> 26 <div class="delay"></div> 27 </body> 28 </html>
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>delay</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 7 8 <style type="text/css"> 9 @-webkit-keyframes delay{ 10 11 100%{ 12 -webkit-transform:translate(100px,0); 13 } 14 15 } 16 .delay{ 17 18 width:100px;height:100px; 19 background-color: #000; 20 -webkit-animation:delay 1s linear 2 forwards alternate; 21 } 22 </style> 23 </head> 24 <body> 25 <div class="delay"></div> 26 </body> 27 </html>
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>delay</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 7 8 <style type="text/css"> 9 @-webkit-keyframes delay{ 10 11 100%{ 12 -webkit-transform:translate(100px,0); 13 } 14 15 } 16 .delay{ 17 18 width:100px;height:100px; 19 background-color: #000; 20 -webkit-animation:delay 1s linear 2 both alternate; 21 } 22 </style> 23 </head> 24 <body> 25 <div class="delay"></div> 26 </body> 27 </html>
两段代码主要说明在forwards和both的情况下动画在设置了反向运行偶次数时,效果仍然一样。因为之前看到网上说的是forwards在偶次数反向是会停在关键帧的100%处,而不是0%处。
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>delay</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 7 8 <style type="text/css"> 9 @-webkit-keyframes delay{ 10 11 100%{ 12 -webkit-transform:translate(100px,0); 13 } 14 15 } 16 .delay{ 17 18 width:100px;height:100px; 19 background-color: #000; 20 -webkit-animation:delay 1s linear 2 both alternate; 21 } 22 </style> 23 </head> 24 <body> 25 <div class="delay"></div> 26 <script type="text/javascript"> 27 window.onload = function(){ 28 var delay = document.getElementsByClassName(‘delay‘)[0], 29 time1, 30 time2; 31 delay.addEventListener(‘webkitAnimationStart‘,function(){ 32 time1 = new Date().getTime(); 33 time2 = time1; 34 console.log(time2-time1+‘ms‘) 35 36 }) 37 delay.addEventListener(‘webkitAnimationIteration‘,function(){ 38 time2 = new Date().getTime(); 39 console.log(time2-time1+‘ms‘) 40 }) 41 delay.addEventListener(‘webkitAnimationEnd‘,function(){ 42 time2 = new Date().getTime(); 43 console.log(time2-time1+‘ms‘) 44 }) 45 } 46 </script> 47 </body> 48 </html>
从代码可以看出,iteration是在动画重复执行的时候触发的。
原文:http://www.cnblogs.com/dq-Leung/p/4280087.html