js实现动态时钟

以你之姓@ 2021-10-26 13:22 528阅读 0赞

发现一个超级好用的gif图工具”抠抠视频秀”,很好用也不占内存,长这样
在这里插入图片描述

  1. 效果图
    在这里插入图片描述
  2. 设计思路

1.调整各个指针的位置关系(transform-origin:)

  1. 计算好各个针之间的度数关系 (分针和时针的度数都不仅仅由自身时间决定)
  2. 由于指针初始摆放位置为三点钟方向,所以相对于摆放位置,要正常显示时间需要在计算好的角度基础上-90

关于转动角度的关系具体如下:1 hour=60min =3600s
先分别转动对应针的角度,此时应考虑联动。秒针每秒走6度;
分针每分钟走6度(在考虑秒针对 分针的影响,将秒数换算成分钟)
时针每小时走30度,考虑分针和秒针的影响

  1. 代码如下
    样式

html

  1. <div id="block">
  2. <div class="hours"></div>
  3. <div class="minute"></div>
  4. <div class="second"></div>
  5. </div>
  6. <script>
  7. setInterval(function () {
  8. var hours = document.getElementsByClassName("hours")[0];
  9. var minute = document.getElementsByClassName("minute")[0];
  10. var second = document.getElementsByClassName("second")[0];
  11. var date = new Date();
  12. var hour = date.getHours(); //调取当前时间的小时
  13. // console.log(hour);
  14. var min = date.getMinutes(); //调取当前时间的分钟
  15. var sec = date.getSeconds(); //调取当前时间的秒
  16. second.style.transform = "rotate(" + ((sec * 6) - 90) + "deg)";
  17. minute.style.transform = "rotate(" + (((min + (sec / 60)) * 6) - 90) + "deg)";
  18. /* */
  19. hours.style.transform = "rotate(" + (((hour + (sec / 3600) + (min / 60)) * 30) - 90) + "deg)";
  20. /**/
  21. }, 1000)
  22. </script>

最后分享一个见到的实现很棒的动态时钟,但是没有使用图片,而是代码绘制!效果如下
在这里插入图片描述
代码如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. /* 全局 */
  8. *{
  9. margin:0;
  10. padding:0;
  11. }
  12. .clock{
  13. width:400px;
  14. height:400px;
  15. border:10px solid #333;
  16. box-shadow: 0 0 20px 3px #444 inset;
  17. border-radius:210px;
  18. position:relative;
  19. margin:5px auto;
  20. z-index:10;
  21. background-color:#f6f6f6;
  22. }
  23. /* 时钟数字 */
  24. .clock-num{
  25. width:40px;
  26. height:40px;
  27. font-size:22px;
  28. text-align:center;
  29. line-height:40px;
  30. position:absolute;
  31. z-index:8;
  32. color:#555;
  33. font-family:fantasy, 'Trebuchet MS';
  34. }
  35. .em_num{
  36. font-size:28px;
  37. }
  38. /* 指针 */
  39. .clock-line{
  40. position:absolute;
  41. z-index:20;
  42. }
  43. .hour-line{ width:100px;
  44. height:4px;
  45. top:198px;
  46. left:200px;
  47. background-color:#000;
  48. border-radius:2px;
  49. transform-origin:0 50%;
  50. box-shadow:1px -3px 8px 3px #aaa;
  51. }
  52. .minute-line{
  53. width:130px;
  54. height:2px;
  55. top:199px;
  56. left:190px;
  57. background-color:#000;
  58. transform-origin:7.692% 50%;
  59. box-shadow:1px -3px 8px 1px #aaa;
  60. }
  61. .second-line{
  62. width:170px;
  63. height:1px;
  64. top:199px;
  65. left:180px;
  66. background-color:#f60;
  67. transform-origin:11.765% 50%;
  68. box-shadow:1px -3px 7px 1px #bbb;
  69. }
  70. /* 原点 */
  71. .origin{
  72. width:20px;
  73. height:20px;
  74. border-radius:10px;
  75. background-color:#000;
  76. position:absolute;
  77. top:190px;
  78. left:190px;
  79. z-index:14;
  80. }
  81. /* 日期 时间 */
  82. .date-info{
  83. width:160px;
  84. height:28px;
  85. line-height:28px;
  86. text-align:center;
  87. position:absolute;
  88. top:217px;
  89. left:120px;
  90. z-index:11;
  91. color:#555;
  92. font-weight:bold;
  93. }
  94. .time-info{
  95. width:92px;
  96. height:30px;
  97. line-height:30px;
  98. text-align:center;
  99. position:absolute;
  100. top:270px;
  101. left:154px;
  102. z-index:11;
  103. background-color:#555;
  104. padding:0;
  105. box-shadow:0 0 9px 2px #222 inset;
  106. }
  107. .time{
  108. width:30px;
  109. height:30px;
  110. text-align:center;
  111. float:left;
  112. color:#fff;
  113. font-weight:bold;
  114. }
  115. #minute-time{
  116. border-left:1px solid #fff;
  117. border-right:1px solid #fff;
  118. }
  119. /* 刻度 */
  120. .clock-scale{
  121. width:195px;
  122. height:2px;
  123. transform-origin:0% 50%;
  124. z-index:7;
  125. position:absolute;
  126. top:199px;
  127. left:200px;
  128. }
  129. .scale-show{
  130. width:12px;
  131. height:2px;
  132. background-color:#555;
  133. float:left;
  134. }
  135. .scale-hidden{
  136. width:183px;
  137. height:2px;
  138. float:left;
  139. }
  140. </style>
  141. </head>
  142. <body>
  143. <div class="clock" id="clock">
  144. <!-- 原点 -->
  145. <div class="origin"></div>
  146. <!-- 时分秒针 -->
  147. <div class="clock-line hour-line" id="hour-line"></div>
  148. <div class="clock-line minute-line" id="minute-line"></div>
  149. <div class="clock-line second-line" id="second-line"></div>
  150. <!-- 日期 -->
  151. <div class="date-info" id="date-info"></div>
  152. <!-- 时间 -->
  153. <div class="time-info" >
  154. <div class="time" id="hour-time"></div>
  155. <div class="time" id="minute-time"></div>
  156. <div class="time" id="second-time"></div>
  157. </div>
  158. </div>
  159. <script>
  160. (function(){
  161. window.onload=initNumXY(200, 160, 40,40);
  162. var hour_line = document.getElementById("hour-line");
  163. var minute_line = document.getElementById("minute-line");
  164. var second_line = document.getElementById("second-line");
  165. var date_info = document.getElementById("date-info");
  166. var week_day = [
  167. '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'
  168. ];
  169. var hour_time = document.getElementById("hour-time");
  170. var minute_time = document.getElementById("minute-time");
  171. var second_time = document.getElementById("second-time");
  172. function setTime(){
  173. var this_day = new Date();
  174. var hour = (this_day.getHours() >= 12) ?
  175. (this_day.getHours() - 12) : this_day.getHours();
  176. var minute = this_day.getMinutes();
  177. var second = this_day.getSeconds();
  178. var hour_rotate = (hour*30-90) + (Math.floor(minute / 12) * 6);
  179. var year = this_day.getFullYear();
  180. var month = ((this_day.getMonth() + 1) < 10 ) ?
  181. "0"+(this_day.getMonth() + 1) : (this_day.getMonth() + 1);
  182. var date = (this_day.getDate() < 10 ) ?
  183. "0"+this_day.getDate() : this_day.getDate();
  184. var day = this_day.getDay();
  185. hour_line.style.transform = 'rotate(' + hour_rotate + 'deg)';
  186. minute_line.style.transform = 'rotate(' + (minute*6 - 90) + 'deg)';
  187. second_line.style.transform = 'rotate(' + (second*6 - 90)+'deg)';
  188. date_info.innerHTML =
  189. year + "-" + month + "-" + date + " " + week_day[day];
  190. hour_time.innerHTML = (this_day.getHours() < 10) ?
  191. "0" + this_day.getHours() : this_day.getHours();
  192. minute_time.innerHTML = (this_day.getMinutes() < 10) ?
  193. "0" + this_day.getMinutes() : this_day.getMinutes();
  194. second_time.innerHTML = (this_day.getSeconds() < 10) ?
  195. "0" + this_day.getSeconds():this_day.getSeconds();
  196. }
  197. setInterval(setTime, 1000);
  198. function initNumXY(R, r, w, h){
  199. var numXY = [
  200. {
  201. "left" : R + 0.5 * r - 0.5 * w,
  202. "top" : R - 0.5 * r * 1.73205 - 0.5 * h
  203. },
  204. {
  205. "left" : R + 0.5 * r * 1.73205 - 0.5 * w,
  206. "top" : R - 0.5 * r - 0.5 * h
  207. },
  208. {
  209. "left" : R + r - 0.5 * w,
  210. "top" : R - 0.5 * h
  211. },
  212. {
  213. "left" : R + 0.5 * r * 1.73205 - 0.5 * w,
  214. "top" : R + 0.5 * r - 0.5 * h
  215. },
  216. {
  217. "left" : R + 0.5 * r - 0.5 * w,
  218. "top" : R + 0.5 * r * 1.732 - 0.5 * h
  219. },
  220. {
  221. "left" : R - 0.5 * w,
  222. "top" : R + r - 0.5 * h
  223. },
  224. {
  225. "left" : R - 0.5 * r - 0.5 * w,
  226. "top" : R + 0.5 * r * 1.732 - 0.5 * h
  227. },
  228. {
  229. "left" : R - 0.5 * r * 1.73205 - 0.5 * w,
  230. "top" : R + 0.5 * r - 0.5 * h
  231. },
  232. {
  233. "left" : R - r - 0.5 * w,
  234. "top" : R - 0.5 * h
  235. },
  236. {
  237. "left" : R - 0.5 * r * 1.73205 - 0.5 * w,
  238. "top" : R - 0.5 * r - 0.5 * h
  239. },
  240. {
  241. "left" : R - 0.5 * r - 0.5 * w,
  242. "top": R - 0.5 * r * 1.73205 - 0.5 * h
  243. },
  244. {
  245. "left" : R - 0.5 * w,
  246. "top" : R - r - 0.5 * h
  247. }
  248. ];
  249. var clock = document.getElementById("clock");
  250. for(var i = 1; i <= 12; i++){
  251. if(i%3 == 0) {
  252. clock.innerHTML += "<div class='clock-num em_num'>"+i+"</div>";
  253. } else {
  254. clock.innerHTML += "<div class='clock-num'>" + i + "</div>";
  255. }
  256. }
  257. var clock_num = document.getElementsByClassName("clock-num");
  258. for(var i = 0; i < clock_num.length; i++) {
  259. clock_num[i].style.left = numXY[i].left + 'px';
  260. clock_num[i].style.top = numXY[i].top + 'px';
  261. }
  262. for(var i = 0; i < 60; i++) {
  263. clock.innerHTML += "<div class='clock-scale'> " +
  264. "<div class='scale-hidden'></div>" +
  265. "<div class='scale-show'></div>" +
  266. "</div>";
  267. }
  268. var scale = document.getElementsByClassName("clock-scale");
  269. for(var i = 0; i < scale.length; i++) {
  270. scale[i].style.transform="rotate(" + (i * 6 - 90) + "deg)";
  271. }
  272. }
  273. })();
  274. </script>
  275. </body>
  276. </html>

发表评论

表情:
评论列表 (有 0 条评论,528人围观)

还没有评论,来说两句吧...

相关阅读

    相关 html+css+js实现时钟

    html+css+js实现比较特别的时钟 简介: 为止,在中国历史上有留下记载的四代计时器分别为:日晷、沙漏、机械钟、石英钟。在中国市场上石英钟最热销。 时钟一直

    相关 Python实现动态时钟小程序

    前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 PS:如有需要Python学习资料的小伙伴可以加点击下方链接

    相关 JS实现简单时钟效果

    老师上课需要我们做一个时钟的小作业 ,我把它放在上面记录一下啦 表盘和时针我都是用的背景图的形式,然后绝对定位,通过调整left和top确定时针、分针、秒针的位置,tran