非常震撼的纯CSS3人物行走动画
author:一佰互联 2019-04-21   click:168

本文分享给大家的是一个用纯CSS3实现的人物行走动画,在没有使用JavaScript的情况下,用CSS3技术将人物行走的姿态描绘得非常逼真。其实动画实现的原理也是比较简单的,将人物行走时的状态分割成多张图片,然后利用CSS3的动画属性将这些图片串联起来形成人物行走动画效果。

HTML代码

XML/HTML Code复制内容到剪贴板
  1. <div id="canvas">  
  2.         <div class="sky">  
  3.             <div class="cloud-1"></div>  
  4.             <div class="cloud-2"></div>  
  5.             <div class="cloud-3"></div>  
  6.             <div class="cloud-4"></div>  
  7.             <div class="cloud-5"></div>  
  8.             <div class="cloud-6"></div>  
  9.             <div class="cloud-7"></div>  
  10.             <div class="cloud-8"></div>  
  11.         </div>  
  12.         <div class="horizon"></div>  
  13.         <div class="ground">  
  14.             <div class="sign-best"></div>  
  15.             <div class="sign-no-js"></div>  
  16.             <div class="sign-lots-of-divs"></div>  
  17.             <div class="sign-all-css"></div>  
  18.         </div>  
  19.         <!-- skeleton and shadow -->  
  20.         <div class="shadow"></div>  
  21.         <div class="me">  
  22.   
  23.             <div class="torso">  
  24.                 <div class="left leg">  
  25.                     <div class="left thigh">  
  26.                         <div class="left shin">  
  27.                             <div class="left foot">  
  28.                                 <div class="left toes"></div>  
  29.                             </div>  
  30.                         </div>  
  31.                     </div>  
  32.                 </div>  
  33.  <!-- left leg -->  
  34.   
  35.                 <div class="right leg">  
  36.                     <div class="right thigh">  
  37.                         <div class="right shin">  
  38.                             <div class="right foot">  
  39.                                 <div class="right toes"></div>  
  40.                             </div>  
  41.                         </div>  
  42.                     </div>  
  43.                 </div>  
  44.  <!-- right leg -->  
  45.   
  46.                 <div class="left arm">  
  47.                     <div class="left bicep">  
  48.                         <div class="left forearm"></div>  
  49.                     </div>  
  50.                 </div>  
  51.  <!-- left arm -->  
  52.   
  53.                 <div class="right arm">  
  54.                     <div class="right bicep">  
  55.                         <div class="right forearm"></div>  
  56.                     </div>  
  57.                 </div>  
  58.  <!-- right arm -->  
  59.   
  60.             </div>  
  61.  <!-- torso -->  
  62.         </div>  
  63.  <!-- me -->  
  64.   
  65.         <div class="overlay"></div>  
  66.     </div>  

基本CSS代码

CSS Code复制内容到剪贴板
  1. #canvas {   
  2.     height600px;   
  3.     width760px;   
  4.     margin: 0;   
  5.     padding: 0;   
  6.     positionrelative;   
  7.     overflowhidden;   
  8. }   
  9.   
  10. #canvas div {   
  11.     positionabsolute;   
  12.     -webkit-animation-iteration-count: infinite;   
  13.     -moz-animation-iteration-count: infinite;   
  14.     -ms-animation-iteration-count: infinite;   
  15.     -o-animation-iteration-count: infinite;   
  16.     animation-iteration-count: infinite;   
  17.   
  18.     -webkit-animation-timing-function: linear;   
  19.     -moz-animation-timing-function: linear;   
  20.     -ms-animation-timing-function: linear;   
  21.     -o-animation-timing-function: linear;   
  22.     animation-timing-function: linear;   
  23. }   
  24.   
  25. #canvas:target div:not(.overlay) {   
  26.     border1px solid black;   
  27. }   
  28.   
  29. #canvas:target div.me div{   
  30.     background: rgba(255, 255, 255, 0.25);   
  31. }   
  32.   
  33. .me {   
  34.     top50pxleft350px;   
  35.     -webkit-animation-name: me;   
  36.     -moz-animation-name: me;   
  37.     -ms-animation-name: me;   
  38.     -o-animation-name: me;   
  39.     animation-name: me;   
  40. }   
  41.   
  42. .me, .me div {   
  43.     background-repeatno-repeat;   
  44.     -webkit-animation-duration: 1750ms;   
  45.     -moz-animation-duration: 1750ms;   
  46.     -ms-animation-duration: 1750ms;   
  47.     -o-animation-duration: 1750ms;   
  48.     animation-duration: 1750ms;   
  49. }   
  50.   
  51. .torso {   
  52.     width86pxheight275px;   
  53.     background-imageurl(images/me/torso.png);   
  54. }   
  55.   
  56. .arm {   
  57.     left12px;   
  58.     -webkit-transform-origin: 20px 10px;   
  59.     -moz-transform-origin: 20px 10px;   
  60.     -ms-transform-origin: 20px 10px;   
  61.     -o-transform-origin: 20px 10px;   
  62.     transform-origin: 20px 10px;   
  63. }   
  64.   
  65. .rightright.arm {   
  66.     top93px;   
  67.     -webkit-animation-name: rightright-bicep;   
  68.     -moz-animation-name: rightright-bicep;   
  69.     -ms-animation-name: rightright-bicep;   
  70.     -o-animation-name: rightright-bicep;   
  71.     animation-name: rightright-bicep;   
  72. }   
  73. .left.arm {   
  74.     top87px;   
  75.     -webkit-animation-name: left-bicep;   
  76.     -moz-animation-name: left-bicep;   
  77.     -ms-animation-name: left-bicep;   
  78.     -o-animation-name: left-bicep;   
  79.     animation-name: left-bicep;   
  80. }   
  81.   
  82. .bicep {   
  83.     height124pxwidth51px;   
  84. }   
  85.   
  86. .rightright.bicep { background-imageurl(images/me/rightright-bicep.png); }   
  87. .left.bicep { background-imageurl(images/me/left-bicep.png); }   
  88.   
  89. .forearm {   
  90.     top108pxleft14px;   
  91.     width36pxheight121px;   
  92.     -webkit-transform-origin: 3px 7px;   
  93.     -moz-transform-origin: 3px 7px;   
  94.     -ms-transform-origin: 3px 7px;   
  95.     -o-transform-origin: 3px 7px;   
  96.     transform-origin: 3px 7px;   
  97. }   
  98.   
  99. .rightright.forearm {   
  100.     background-imageurl(images/me/rightright-forearm.png);   
  101.     -webkit-animation-name: rightright-forearm;   
  102.     -moz-animation-name: rightright-forearm;   
  103.     -ms-animation-name: rightright-forearm;   
  104.     -o-animation-name: rightright-forearm;   
  105.     animation-name: rightright-forearm;   
  106. }   
  107.   
  108. .left.forearm {   
  109.     background-imageurl(images/me/left-forearm.png);   
  110.     -webkit-animation-name: left-forearm;   
  111.     -moz-animation-name: left-forearm;   
  112.     -ms-animation-name: left-forearm;   
  113.     -o-animation-name: left-forearm;   
  114.     animation-name: left-forearm;   
  115. }   
  116.   
  117. .leg {   
  118.     left6px;   
  119.     -webkit-transform-origin: 30px 20px;   
  120.     -moz-transform-origin: 30px 20px;   
  121.     -ms-transform-origin: 30px 20px;   
  122.     -o-transform-origin: 30px 20px;   
  123.     transform-origin: 30px 20px;   
  124.     -webkit-animation-name: thigh;   
  125.     -moz-animation-name: thigh;   
  126.     -ms-animation-name: thigh;   
  127.     -o-animation-name: thigh;   
  128.     animation-name: thigh;   
  129. }   
  130.   
  131. .rightright.leg {   
  132.     top235px;   
  133.     -webkit-animation-name: rightright-thigh;   
  134.     -moz-animation-name: rightright-thigh;   
  135.     -ms-animation-name: rightright-thigh;   
  136.     -o-animation-name: rightright-thigh;   
  137.     animation-name: rightright-thigh;   
  138. }   
  139.   
  140. .left.leg {   
  141.     top225px;   
  142.     -webkit-animation-name: left-thigh;   
  143.     -moz-animation-name: left-thigh;   
  144.     -ms-animation-name: left-thigh;   
  145.     -o-animation-name: left-thigh;   
  146.     animation-name: left-thigh;   
  147. }   
  148.   
  149. .thigh {   
  150.     width70pxheight145px;   
  151. }   
  152.   
  153. .left.thigh { background-imageurl(images/me/left-thigh.png); }   
  154. .rightright.thigh { background-imageurl(images/me/rightright-thigh.png); }   
  155.   
  156. .shin {   
  157.     top115px;   
  158.     width50pxheight170px;   
  159.     background-imageurl(images/me/shin.png);   
  160.     -webkit-transform-origin: 30px 25px;   
  161.     -moz-transform-origin: 30px 25px;   
  162.     -ms-transform-origin: 30px 25px;   
  163.     -o-transform-origin: 30px 25px;   
  164.     transform-origin: 30px 25px;   
  165. }   
  166.   
  167. .rightright.shin {   
  168.     -webkit-animation-name: rightright-shin;   
  169.     -moz-animation-name: rightright-shin;   
  170.     -ms-animation-name: rightright-shin;   
  171.     -o-animation-name: rightright-shin;   
  172.     animation-name: rightright-shin;   
  173. }   
  174. .left.shin {   
  175.     -webkit-animation-name: left-shin;   
  176.     -moz-animation-name: left-shin;   
  177.     -ms-animation-name: left-shin;   
  178.     -o-animation-name: left-shin;   
  179.     animation-name: left-shin;   
  180. }   
  181.   
  182. .foot {   
  183.     top155pxleft2px;   
  184.     width67pxheight34px;   
  185.     background-imageurl(images/me/foot.png);   
  186.     -webkit-transform-origin: 0 50%;   
  187.     -moz-transform-origin: 0 50%;   
  188.     -ms-transform-origin: 0 50%;   
  189.     -o-transform-origin: 0 50%;   
  190.     transform-origin: 0 50%;   
  191. }   
  192.   
  193. .rightright.foot {   
  194.     -webkit-animation-name: rightright-foot;   
  195.     -moz-animation-name: rightright-foot;   
  196.     -ms-animation-name: rightright-foot;   
  197.     -o-animation-name: rightright-foot;   
  198.     animation-name: rightright-foot;   
  199. }   
  200. .left.foot {   
  201.     -webkit-animation-name: left-foot;   
  202.     -moz-animation-name: left-foot;   
  203.     -ms-animation-name: left-foot;   
  204.     -o-animation-name: left-foot;   
  205.     animation-name: left-foot;   
  206. }   
  207.   
  208. .toes {   
  209.     top9pxleft66px;   
  210.     width28pxheight25px;   
  211.     background-imageurl(images/me/toes.png);   
  212.     -webkit-transform-origin: 0% 100%;   
  213.     -moz-transform-origin: 0% 100%;   
  214.     -ms-transform-origin: 0% 100%;   
  215.     -o-transform-origin: 0% 100%;   
  216.     transform-origin: 0% 100%;   
  217. }   
  218.   
  219. .rightright.toes {   
  220.     -webkit-animation-name: rightright-toes;   
  221.     -moz-animation-name: rightright-toes;   
  222.     -ms-animation-name: rightright-toes;   
  223.     -o-animation-name: rightright-toes;   
  224.     animation-name: rightright-toes;   
  225. }   
  226. .left.toes {   
  227.     -webkit-animation-name: left-toes;   
  228.     -moz-animation-name: left-toes;   
  229.     -ms-animation-name: left-toes;   
  230.     -o-animation-name: left-toes;   
  231.     animation-name: left-toes;   
  232. }   
  233.   
  234. .shadow {   
  235.     width200pxheight70px;   
  236.     left270pxbottombottom5px;   
  237.     background-imageurl(images/misc/shadow.png);   
  238.     -webkit-animation-name: shadow;   
  239.     -moz-animation-name: shadow;   
  240.     -ms-animation-name: shadow;   
  241.     -o-animation-name: shadow;   
  242.     animation-name: shadow;   
  243. }   
  244.   
  245. /* setting proper z-indexes so that limbs show up correctly */  
  246.   
  247. div.rightright.arm { z-index: 1; }   
  248. div.left.arm { z-index: -3; }   
  249. div.arm > div.bicep > div.forearm { z-index: -1; }   
  250.   
  251. div.rightright.leg { z-index: -1; }   
  252. div.left.leg { z-index: -2; }   
  253. div.leg > div.thigh > div.shin { z-index: -1; }   
  254.   
  255. .overlay {   
  256.     width: 100%; height: 100%;   
  257.     backgroundurl(images/misc/gradient-left.png) repeat-y top left,   
  258.                 url(images/misc/gradient-rightright.png) repeat-y top rightright;   
  259. }   
  260.   
  261. .sky div {   
  262.     background-repeatno-repeat;   
  263.     -webkit-animation-name: prop-1200;   
  264.     -moz-animation-name: prop-1200;   
  265.     -ms-animation-name: prop-1200;   
  266.     -o-animation-name: prop-1200;   
  267.     animation-name: prop-1200;   
  268. }   
  269.   
  270. .cloud-1, .cloud-2 {   
  271.     width82pxheight90px;   
  272.     background-imageurl(images/clouds/1.png);   
  273.     -webkit-animation-duration: 120s;   
  274.     -moz-animation-duration: 120s;   
  275.     -ms-animation-duration: 120s;   
  276.     -o-animation-duration: 120s;   
  277.     animation-duration: 120s;   
  278. }   
  279.   
  280. .cloud-3, .cloud-4 {   
  281.     top70px;   
  282.     width159pxheight90px;   
  283.     background-imageurl(images/clouds/2.png);   
  284.     -webkit-animation-duration: 80s;   
  285.     -moz-animation-duration: 80s;   
  286.     -ms-animation-duration: 80s;   
  287.     -o-animation-duration: 80s;   
  288.     animation-duration: 80s;   
  289. }   
  290.   
  291. .cloud-5, .cloud-6 {   
  292.     top30px;   
  293.     width287pxheight62px;   
  294.     background-imageurl(images/clouds/3.png);   
  295.     -webkit-animation-duration: 140s;   
  296.     -moz-animation-duration: 140s;   
  297.     -ms-animation-duration: 140s;   
  298.     -o-animation-duration: 140s;   
  299.     animation-duration: 140s;   
  300. }   
  301.   
  302. .cloud-7, .cloud-8 {   
  303.     top100px;   
  304.     width94pxheight81px;   
  305.     background-imageurl(images/clouds/4.png);   
  306.     -webkit-animation-duration: 90s;   
  307.     -moz-animation-duration: 90s;   
  308.     -ms-animation-duration: 90s;   
  309.     -o-animation-duration: 90s;   
  310.     animation-duration: 90s;   
  311. }   
  312.   
  313. .cloud-1 { left0px; }   
  314. .cloud-2 { left1200px; }   
  315.   
  316. .cloud-3 { left250px; }   
  317. .cloud-4 { left1450px; }   
  318.   
  319. .cloud-5 { left500px; }   
  320. .cloud-6 { left1700px; }   
  321.   
  322. .cloud-7 { left950px; }   
  323. .cloud-8 { left2150px; }   
  324.   
  325. .horizon {   
  326.     top350px;   
  327.     width1800px