css模拟时钟

css模拟时钟
css模拟时钟

思路:

画时钟数字(x,y)坐标

x = x0 + r*cos(deg)
y = y0 + r*sin(deg)

知识点:

  1. 创建元素: createElement
  2. 添加元素: appendChild
  3. css3旋转: transform:rotate(30deg);
  4. css3改变旋转中心点: transform-origin:0 0;
  5. 获取当前时间, 把时间数字转换为度数
  1. <!DOCTYPE html> 

  2. <html lang="en"> 

  3. <head> 

  4. <meta charset="UTF-8"> 

  5. <title>Title</title> 

  6. <style> 

  7. * { margin: 0; padding: 0; } 

  8. #bg { 

  9. width: 400px; 

  10. height: 400px; 

  11. background: #ddd; 

  12. border-radius: 10px; 

  13. /* div上下左右垂直居中 */ 

  14. position: absolute; 

  15. left: 50%; 

  16. top: 50%; 

  17. margin-top: -200px; 

  18. margin-left: -200px; 




  19. #bg #clock { 

  20. width: 360px; 

  21. height: 360px; 

  22. background: #fff; 

  23. border-radius: 50%; 

  24. display: inline-block; 

  25. margin: 20px; 

  26. position: relative; 




  27. #hour { 

  28. width: 70px; 

  29. height: 16px; 

  30. background: #000; 

  31. position: absolute; 

  32. left: 180px; 

  33. top: 172px; 

  34. border-radius: 16px; 

  35. transform-origin:0 8px; 



  36. #minute { 

  37. width: 120px; 

  38. height: 12px; 

  39. background: #000; 

  40. position: absolute; 

  41. left: 180px; 

  42. top: 174px; 

  43. border-radius: 12px; 

  44. transform-origin:0 6px; 



  45. #second { 

  46. width: 140px; 

  47. height: 6px; 

  48. background: #f00; 

  49. position: absolute; 

  50. left: 180px; 

  51. top: 177px; 

  52. border-radius: 6px; 

  53. transform-origin:0 1px; 

  54. /*transform:rotate(30deg);*/ 

  55. /*animation: clockRotate 3s linear infinite;*/ 




  56. @keyframes clockRotate { 

  57. from {transform:rotate(0deg);} 

  58. to {transform:rotate(360deg);} 




  59. #point{ 

  60. width: 30px; 

  61. height: 30px; 

  62. border-radius: 50%; 

  63. background: #000; 

  64. position: absolute; 

  65. left: 50%; 

  66. top: 50%; 

  67. margin: -15px 0 0 -15px; 




  68. #clock .number{ 

  69. position: absolute; 

  70. font-size: 28px; 

  71. width: 50px; 

  72. height: 50px; 

  73. line-height: 50px; 

  74. text-align: center; 



  75. </style> 

  76. </head> 

  77. <body> 

  78. <div id="bg"> 

  79. <div id="clock"> 

  80. <div id="hour"></div> 

  81. <div id="minute"></div> 

  82. <div id="second"></div> 

  83. <div id="point"></div> 

  84. </div> 

  85. </div> 


  86. <script> 

  87. var clock = document.getElementById('clock'); 

  88. var r = 150; 

  89. var angle = Math.PI * 2; // 2PI = 360° 

  90. for (var i = 1; i <=12; i++) { 

  91. var deg = angle/12 * i - Math.PI/2; 

  92. var x = r + r*Math.cos(deg); 

  93. var y = r + r*Math.sin(deg); 

  94. //console.log(x,y); 

  95. var num = document.createElement('div');//创建div 

  96. num.className = 'number';//设置class样式 

  97. num.innerText = i; 

  98. //设置坐标 

  99. num.style.top = y + 'px'; 

  100. num.style.left = x + 'px'; 

  101. //添加 

  102. clock.appendChild(num); 




  103. //TODO 1. 数字转换度数 

  104. function run() { 

  105. var date = new Date(); 

  106. //时(0-23) 分(0-59)秒(0-59) 

  107. var hour = date.getHours(); 

  108. var minute = date.getMinutes(); 

  109. var second = date.getSeconds(); 


  110. var hourDeg = 360/12 * hour - 90; 

  111. var minuteDeg = 360/60 * minute - 90; 

  112. var secondDeg = 360/60 * second - 90; 


  113. var hourDiv = document.getElementById('hour'); 

  114. var minuteDiv = document.getElementById('minute'); 

  115. var secondDiv = document.getElementById('second'); 


  116. hourDiv.style.transform = 'rotate('+hourDeg+'deg)'; 

  117. minuteDiv.style.transform = 'rotate('+minuteDeg+'deg)'; 

  118. secondDiv.style.transform = 'rotate('+secondDeg+'deg)'; 




  119. //TODO 2. 设置定时器 

  120. run(); 

  121. setInterval(function () { 

  122. run(); 

  123. },1000); 

  124. </script> 

  125. </body> 

  126. </html> 

上一篇:通用窗口类 Inventory Pro 2.1.2 Demo1(下)


下一篇:Python 收集Twitter时间序列数据