打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
优雅WEB:三步制作CSS3扁平化小时钟
userphoto

2016.05.19

关注
 本帖最后由 轻微生活网 于 2016-3-18 10:23 编辑

本码农长期浸淫于Dribbble等设计师网站,看到那些好看的设计作品就非常激动,但是无奈PS不精通,AI也早忘光了,只不过对前端略有研究,偶然间看到几个设计清爽的时钟,觉得用CSS实现起来应该也没什么难度,于是花了个把钟头琢磨了一个出来。
效果图


DEMO


技术点
  • box-shadow 表盘的质感什么的全靠它了
  • nth-child(x) 用于表盘刻度的定位什么的
  • transform-origin 这个用来定位三个表针旋转的中心点
  • keyframes 表针动画效果
流程
先设计好DOM结构,代码如下:
  1. <div class="clock-wrapper">
  2.     <div class="clock-base">
  3.         <div class="click-indicator">
  4.             <div></div>
  5.             <div></div>
  6.             <div></div>
  7.             <div></div>
  8.             <div></div>
  9.             <div></div>
  10.             <div></div>
  11.             <div></div>
  12.             <div></div>
  13.             <div></div>
  14.             <div></div>
  15.             <div></div>
  16.         </div>
  17.         <div class="clock-hour"></div>
  18.         <div class="clock-minute"></div>
  19.         <div class="clock-second"></div>
  20.         <div class="clock-center"></div>
  21.     </div>
  22. </div>
复制代码

结构很简单,从样式名可以看出来每个元素的用处,中间那段空div自然就是表盘刻度了。
接下来是CSS代码
  1. html,body{
  2.     height: 100%;
  3.     margin: 0;
  4.     padding: 0;
  5.     background-image: linear-gradient(#e7e7e7,#d7d7d7);
  6. }
  7. /*时钟容器*/
  8. .clock-wrapper{
  9.     position: absolute;
  10.     top: 0;
  11.     right: 0;
  12.     bottom: 100px;
  13.     left: 0;
  14.     width: 250px;
  15.     height: 250px;
  16.     margin: auto;
  17.     padding: 5px;
  18.     background-image: linear-gradient(#f7f7f7,#e0e0e0);
  19.     border-radius: 50%;
  20.     box-shadow: 0 10px 15px rgba(0,0,0,.15),0 2px 2px rgba(0,0,0,.2);

  21. }
  22. /*表盘*/
  23. .clock-base{
  24.     width: 250px;
  25.     height: 250px;
  26.     background-color: #eee;
  27.     border-radius: 50%;
  28.     box-shadow: 0 0 5px #eee;
  29. }
  30. /*表盘刻度容器*/
  31. .click-indicator{
  32.     position: absolute;
  33.     z-index: 1;
  34.     top: 15px;
  35.     left: 15px;
  36.     width: 230px;
  37.     height: 230px;
  38. }
  39. /*表盘刻度*/
  40. .click-indicator div{
  41.     position: absolute;
  42.     width: 2px;
  43.     height: 4px;
  44.     margin: 113px 114px;
  45.     background-color: #ddd;
  46. }
  47. /*分别设置各个刻度的位置和角度*/
  48. .click-indicator div:nth-child(1) {
  49.     transform: rotate(30deg) translateY(-113px);
  50. }
  51. .click-indicator div:nth-child(2) {
  52.     transform: rotate(60deg) translateY(-113px);
  53. }
  54. .click-indicator div:nth-child(3) {
  55.     transform: rotate(90deg) translateY(-113px);
  56.     background-color: #aaa;
  57. }
  58. .click-indicator div:nth-child(4) {
  59.     transform: rotate(120deg) translateY(-113px);
  60. }
  61. .click-indicator div:nth-child(5) {
  62.     transform: rotate(150deg) translateY(-113px);
  63. }
  64. .click-indicator div:nth-child(6) {
  65.     transform: rotate(180deg) translateY(-113px);
  66.     background-color: #aaa;
  67. }
  68. .click-indicator div:nth-child(7) {
  69.     transform: rotate(210deg) translateY(-113px);
  70. }
  71. .click-indicator div:nth-child(8) {
  72.     transform: rotate(240deg) translateY(-113px);
  73. }
  74. .click-indicator div:nth-child(9) {
  75.     transform: rotate(270deg) translateY(-113px);
  76.     background-color: #aaa;
  77. }
  78. .click-indicator div:nth-child(10) {
  79.     transform: rotate(300deg) translateY(-113px);
  80. }
  81. .click-indicator div:nth-child(11) {
  82.     transform: rotate(330deg) translateY(-113px);
  83. }
  84. .click-indicator div:nth-child(12) {
  85.     transform: rotate(360deg) translateY(-113px);
  86.     background-color: #c00;
  87. }
  88. /*时针*/
  89. .clock-hour{
  90.     position: absolute;
  91.     z-index: 2;
  92.     top: 80px;
  93.     left: 128px;
  94.     width: 4px;
  95.     height: 65px;
  96.     background-color: #555;
  97.     border-radius: 2px;
  98.     box-shadow: 0 0 2px rgba(0,0,0,.2);
  99.     transform-origin: 2px 50px;
  100.     transition: .5s;
  101.     -webkit-animation: rotate-hour 43200s linear infinite;
  102. }
  103. /*分针*/
  104. .clock-minute{
  105.     position: absolute;
  106.     z-index: 3;
  107.     top: 60px;
  108.     left: 128px;
  109.     width: 4px;
  110.     height: 85px;
  111.     background-color: #555;
  112.     border-radius: 2px;
  113.     box-shadow: 0 0 2px rgba(0,0,0,.2);
  114.     transform-origin: 2px 70px;
  115.     transition: .5s;
  116.     -webkit-animation: rotate-minute 3600s linear infinite;
  117. }
  118. /*秒针*/
  119. .clock-second{
  120.     position: absolute;
  121.     z-index: 4;
  122.     top: 20px;
  123.     left: 129px;
  124.     width: 2px;
  125.     height: 130px;
  126.     background-color: #a00;
  127.     box-shadow: 0 0 2px rgba(0,0,0,.2);
  128.     transform-origin: 1px 110px;
  129.     transition: .5s;
  130.     -webkit-animation: rotate-second 60s linear infinite;
  131. }
  132. .clock-second:after{
  133.     content: "";
  134.     display: block;
  135.     position: absolute;
  136.     left: -5px;
  137.     bottom: 14px;
  138.     width: 8px;
  139.     height: 8px;
  140.     background-color: #a00;
  141.     border: solid 2px #a00;
  142.     border-radius: 50%;
  143.     box-shadow: 0 0 3px rgba(0,0,0,.2);
  144. }
  145. /*表盘中央的原型区域*/
  146. .clock-center{
  147.     position: absolute;
  148.     z-index: 1;
  149.     width: 150px;
  150.     height: 150px;
  151.     top: 55px;
  152.     left: 55px;
  153.     background-image: linear-gradient(#e3e3e3,#f7f7f7);
  154.     border-radius: 50%;
  155.     box-shadow: inset 0 -1px 0 #fafafa, inset 0 1px 0 #e3e3e3;
  156. }
  157. .clock-center:after{
  158.     content: "";
  159.     display: block;
  160.     width: 20px;
  161.     height: 20px;
  162.     margin: 65px;
  163.     background-color: #ddd;
  164.     border-radius: 50%;
  165. }
复制代码

样式文件就这些,不过这样的话三个指针都是在12点的,接下来需要让指针动起来。
其实简单点的话直接在css里面定好动画规则就行了:时针43200秒旋转360度,分针秒针以此类推。
但是强迫症表示这样坚决不行,连正确的时间都不能指示的时钟肯定是山寨品,所以这里需要找CSS的好兄弟JavaScript借下力了:
  1. (function(){

  2.     //generate clock animations
  3.     var now       = new Date(),
  4.         hourDeg   = now.getHours() / 12 * 360 + now.getMinutes() / 60 * 30,
  5.         minuteDeg = now.getMinutes() / 60 * 360 + now.getSeconds() / 60 * 6,
  6.         secondDeg = now.getSeconds() / 60 * 360,
  7.         stylesDeg = [
  8.             "@-webkit-keyframes rotate-hour{from{transform:rotate(" + hourDeg + "deg);}to{transform:rotate(" + (hourDeg + 360) + "deg);}}",
  9.             "@-webkit-keyframes rotate-minute{from{transform:rotate(" + minuteDeg + "deg);}to{transform:rotate(" + (minuteDeg + 360) + "deg);}}",
  10.             "@-webkit-keyframes rotate-second{from{transform:rotate(" + secondDeg + "deg);}to{transform:rotate(" + (secondDeg + 360) + "deg);}}"
  11.         ].join("");

  12.     document.getElementById("clock-animations").innerHTML = stylesDeg;

  13. })();
复制代码

哦,千万别忘了在head标签里面放点东西:
  1. <style id="clock-animations"></style>
复制代码

不然JS生成的样式代码没地方安身啊。

教程来源:
HTML+CSS3再加一点点JS做的一个小时钟 - 优雅的Web - SegmentFault  https://segmentfault.com/a/1190000003055672
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
css3元素如何扭曲、移位或旋转
让等待变爽快!用CSS3创建预加载动画集
纯CSS3实现各种表情动画
html5和css3制作不规则的网页背景分割线
CSS动画实例:一颗躁动的心
CSS3——2D变形(CSS3) transform
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服