Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79172
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 740
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-04 16:50
文章分类
文章存档

2014年(73)

我的朋友

分类: Html/Css

2014-07-10 16:49:03

  • 纯CSS3制作秒表,实现要领: 
      1、动画的逻辑就是给数字使用了绝对定位,改变“top”的属性值; 
      2、分 和 秒 应该是 '60' 而不是 '100',因此需要创建两个动画; 
      3、十步十个数字 
      4、六步六个数字

点击(此处)折叠或打开

  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>纯CSS3制作秒表</title>
  6. <style type="text/css">
  7. body{
  8.     background: url('/jscss/demoimg/201211/wood_pattern.png') repeat;
  9. }
  10. .container {
  11.     padding: 50px;
  12.     text-align: center;
  13. }

  14. .timer {
  15.     padding: 10px;
  16.     background: -webkit-linear-gradient(top, #222, #444);
  17.     background: -moz-linear-gradient(top, #222, #444);
  18.     background: -ms-linear-gradient(top, #222, #444);
  19.     background: -o-linear-gradient(top, #222, #444);
  20.     background: linear-gradient(top, #222, #444);
  21.     overflow: hidden;
  22.     display: inline-block;
  23.     border: 7px solid #efefef;
  24.     border-radius: 5px;
  25.     position: relative;    
  26.     box-shadow: inset 0 -2px 10px 1px rgba(0, 0, 0, 0.75), 0 5px 20px -10px rgba(0, 0, 0, 1);
  27. }

  28. .cell {
  29.     /*只显示一位数字. 高度 = 数字的行高,宽度 = 数字的宽度*/
  30.     width: 0.60em;
  31.     height: 40px;
  32.     font-size: 50px;
  33.     overflow: hidden;
  34.     position: relative;
  35.     float: left;
  36. }

  37. .numbers {
  38.     width: 0.6em;
  39.     line-height: 40px;
  40.     font-family: digital, arial, verdana;
  41.     text-align: center;
  42.     color: #fff;
  43.     position: absolute;
  44.     top: 0;
  45.     left: 0;
  46.     /*数字发光效果*/
  47.     text-shadow: 0 0 5px rgba(255, 255, 255, 1);
  48. }

  49. /*秒表控制器样式*/
  50. #timer_controls {
  51.     margin-top: -5px;
  52. }
  53. #timer_controls label {
  54.     cursor: pointer;
  55.     padding: 5px 10px;
  56.     background: #efefef;
  57.     font-family: arial, verdana, tahoma;
  58.     font-size: 11px;
  59.     border-radius: 0 0 3px 3px;
  60. }
  61. input[name="controls"] {display: none;}

  62. /*Control code*/
  63. /*秒表暂停运转*/
  64. #stop:checked~.timer .numbers {
  65.     -webkit-animation-play-state: paused;
  66.     -moz-animation-play-state: paused;
  67.     -o-animation-play-state: paused;
  68.     -ms-animation-play-state: paused;
  69.     animation-play-state: paused;
  70. }
  71. /*秒表开始运转*/
  72. #start:checked~.timer .numbers {
  73.   -webkit-animation-play-state: running;
  74.   -moz-animation-play-state: running;
  75.   -o-animation-play-state: running;
  76.   -ms-animation-play-state: running;
  77.   animation-play-state: running;
  78. }
  79. /*重置秒表*/
  80. #reset:checked~.timer .numbers {
  81.   -webkit-animation: none;
  82.   -moz-animation: none;
  83.   -o-animation: none;
  84.   -ms-animation: none;
  85.   animation: none;
  86. }

  87. .moveten {
  88.     /*使用分步骤来移动数字,10个数字等于10步*/
  89.     -webkit-animation: moveten 1s steps(10, end) infinite;
  90.     -moz-animation: moveten 1s steps(10, end) infinite;
  91.     -o-animation: moveten 1s steps(10, end) infinite;
  92.     -ms-animation: moveten 1s steps(10, end) infinite;
  93.     animation: moveten 1s steps(10, end) infinite;
  94.     /*默认情况下动画是停止不动的*/
  95.     -webkit-animation-play-state: paused;
  96.     -moz-animation-play-state: paused;
  97.     -o-animation-play-state: paused;
  98.     -ms-animation-play-state: paused;
  99.     animation-play-state: paused;
  100. }
  101. .movesix {
  102.     -webkit-animation: movesix 1s steps(6, end) infinite;
  103.     -moz-animation: movesix 1s steps(6, end) infinite;
  104.     -o-animation: movesix 1s steps(6, end) infinite;
  105.     -ms-animation: movesix 1s steps(6, end) infinite;
  106.     animation: movesix 1s steps(6, end) infinite;
  107. }

  108. /*同步时间的速率*/
  109. /*每秒十个数字,因此也需要十步*/
  110. .second {
  111.  -webkit-animation-duration: 10s;
  112.  -moz-animation-duration: 10s;
  113.  -o-animation-duration: 10s;
  114.  -ms-animation-duration: 10s;
  115.  animation-duration: 10s;
  116. }
  117. .tensecond {
  118.   /*60 times .second*/
  119.   -webkit-animation-duration: 60s;
  120.   -moz-animation-duration: 60s;
  121.   -o-animation-duration: 60s;
  122.   -ms-animation-duration: 60s;
  123.   animation-duration: 60s;
  124. }

  125. .milisecond {
  126.   /*1/10th of .second*/
  127.   -webkit-animation-duration: 1s;
  128.     -moz-animation-duration: 1s;
  129.     -o-animation-duration: 1s;
  130.     -ms-animation-duration: 1s;
  131.     animation-duration: 1s;
  132. }
  133. .tenmilisecond {
  134.   -webkit-animation-duration: 0.1s;
  135.   -moz-animation-duration: 0.1s;
  136.   -ms-animation-duration: 0.1s;
  137.   -o-animation-duration: 0.1s;
  138.   animation-duration: 0.1s;
  139. }
  140. .hundredmilisecond {
  141.   -webkit-animation-duration: 0.01s;
  142.   -moz-animation-duration: 0.01s;
  143.   -o-animation-duration: 0.01s;
  144.   -ms-animation-duration: 0.01s;
  145.   animation-duration: 0.01s;
  146. }

  147. .minute {
  148.   /*60 times .second*/
  149.   -webkit-animation-duration: 600s;
  150.     -moz-animation-duration: 600s;
  151.     -o-animation-duration: 600s;
  152.     -ms-animation-duration: 600s;
  153.     animation-duration: 600s;
  154. }
  155. .tenminute {
  156.   /*60 times .minute*/
  157.   -webkit-animation-duration: 3600s;
  158.     -moz-animation-duration: 3600s;
  159.     -ms-animation-duration: 3600s;
  160.     -o-animation-duration: 3600s;
  161.     animation-duration: 3600s;
  162. }

  163. .hour {
  164.   /*60 times .minute*/
  165.   -webkit-animation-duration: 36000s;
  166.     -moz-animation-duration: 36000s;
  167.     -ms-animation-duration: 36000s;
  168.     -o-animation-duration: 36000s;
  169.     animation-duration: 36000s;
  170. }
  171. .tenhour {
  172.   /*10 times .hour*/
  173.   -webkit-animation-duration: 360000s;
  174.     -moz-animation-duration: 360000s;
  175.     -o-animation-duration: 360000s;
  176.     -ms-animation-duration: 360000s;
  177.     animation-duration: 360000s;
  178. }
  179. @-webkit-keyframes moveten {
  180.     0% {top: 0;}
  181.     100% {top: -400px;}
  182.     /*height = 40. digits = 10. hence -400 to move it completely to the top*/
  183. }

  184. @-webkit-keyframes movesix {
  185.     0% {top: 0;}
  186.     100% {top: -240px;}
  187.     /*height = 40. digits = 6. hence -240 to move it completely to the top*/
  188. }

  189. @-moz-keyframes moveten {
  190.     0% {top: 0;}
  191.     100% {top: -400px;}
  192.     /*height = 40. digits = 10. hence -400 to move it completely to the top*/
  193. }

  194. @-moz-keyframes movesix {
  195.     0% {top: 0;}
  196.     100% {top: -240px;}
  197.     /*height = 40. digits = 6. hence -240 to move it completely to the top*/
  198. }
  199. @-o-keyframes moveten {
  200.     0% {top: 0;}
  201.     100% {top: -400px;}
  202.     /*height = 40. digits = 10. hence -400 to move it completely to the top*/
  203. }

  204. @-o-keyframes movesix {
  205.     0% {top: 0;}
  206.     100% {top: -240px;}
  207.     /*height = 40. digits = 6. hence -240 to move it completely to the top*/
  208. }
  209. @-ms-keyframes moveten {
  210.     0% {top: 0;}
  211.     100% {top: -400px;}
  212.     /*height = 40. digits = 10. hence -400 to move it completely to the top*/
  213. }

  214. @-ms-keyframes movesix {
  215.     0% {top: 0;}
  216.     100% {top: -240px;}
  217.     /*height = 40. digits = 6. hence -240 to move it completely to the top*/
  218. }
  219. @keyframes moveten {
  220.     0% {top: 0;}
  221.     100% {top: -400px;}
  222.     /*height = 40. digits = 10. hence -400 to move it completely to the top*/
  223. }

  224. @keyframes movesix {
  225.     0% {top: 0;}
  226.     100% {top: -240px;}
  227.     /*height = 40. digits = 6. hence -240 to move it completely to the top*/
  228. }
  229. /*Lets use a better font for the numbers*/
  230. @font-face {
  231.     font-family: 'digital';
  232.     src: url('');
  233.     
  234. }    
  235.     </style>
  236. </head>
  237. <body>
  238. <div class="page">
  239.     <section class="demo">
  240.         <div class="container">
  241.             
  242.             <input id="start" name="controls" type="radio" />
  243.             <input id="stop" name="controls" type="radio" />
  244.             <input id="reset" name="controls" type="radio" />
  245.             <div class="timer">
  246.                 <div class="cell">
  247.                     <div class="numbers tenhour moveten">0 1 2 3 4 5 6 7 8 9</div>
  248.                 </div>
  249.                 <div class="cell">
  250.                     <div class="numbers hour moveten">0 1 2 3 4 5 6 7 8 9</div>
  251.                 </div>
  252.                 <div class="cell divider"><div class="numbers">:</div></div>
  253.                 <div class="cell">
  254.                     <div class="numbers tenminute movesix">0 1 2 3 4 5 6</div>
  255.                 </div>
  256.                 <div class="cell">
  257.                     <div class="numbers minute moveten">0 1 2 3 4 5 6 7 8 9</div>
  258.                 </div>
  259.                 <div class="cell divider"><div class="numbers">:</div></div>
  260.                 <div class="cell">
  261.                     <div class="numbers tensecond movesix">0 1 2 3 4 5 6</div>
  262.                 </div>
  263.                 <div class="cell">
  264.                     <div class="numbers second moveten">0 1 2 3 4 5 6 7 8 9</div>
  265.                 </div>
  266.                 <div class="cell divider"><div class="numbers">:</div></div>
  267.                 <div class="cell">
  268.                     <div class="numbers milisecond moveten">0 1 2 3 4 5 6 7 8 9</div>
  269.                 </div>
  270.                 <div class="cell">
  271.                     <div class="numbers tenmilisecond moveten">0 1 2 3 4 5 6 7 8 9</div>
  272.                 </div>
  273.                 <div class="cell">
  274.                     <div class="numbers hundredmilisecond moveten">0 1 2 3 4 5 6 7 8 9</div>
  275.                 </div>
  276.             </div>
  277.             
  278.             <div id="timer_controls">
  279.                 <label for="start">Start</label>
  280.                 <label for="stop">Stop</label>
  281.                 <label for="reset">Reset</label>
  282.             </div>
  283.         </div>
  284.         <p style="text-align:center">此案例代码来自于<a href="http://thecodeplayer.com/walkthrough/make-a-stopwatch-using-css3-without-images-or-javascript" target="_blank">thecodeplayer.com</a>,并做过相应的代码处理。</p>
  285. </section>
  286. </div>
  287. </body>
  288. </html>


阅读(341) | 评论(0) | 转发(0) |
0

上一篇:CSS控制表格打印

下一篇:纯CSS实现三角形

给主人留下些什么吧!~~