练习2_时钟.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. /* css reset */
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. }
  13. li{
  14. list-style: none;
  15. }
  16. .box{
  17. width: 400px;
  18. height: 400px;
  19. border:2px dashed black;
  20. margin:100px auto;
  21. }
  22. .clock{
  23. width: 400px;
  24. height: 400px;
  25. border-radius: 50%;
  26. border:2px solid black;
  27. box-sizing: border-box;
  28. position: relative;
  29. }
  30. .clock ul li{
  31. width: 4px;
  32. height: 6px;
  33. background-color: black;
  34. position: absolute;
  35. top:0;
  36. left: 50%;
  37. margin-left: -2px;
  38. transform-origin: center 198px;
  39. }
  40. .clock ul li:nth-child(5n+1){
  41. height: 12px;
  42. }
  43. /* .clock ul li:nth-child(2){
  44. transform: rotate(6deg);
  45. }
  46. .clock ul li:nth-child(3){
  47. transform: rotate(12deg);
  48. } */
  49. .hour{
  50. margin-left: -5px;
  51. margin-top: 78px;
  52. width: 10px;
  53. height: 120px;
  54. background-color: red;
  55. }
  56. .minute{
  57. margin-left: -4px;
  58. width: 8px;
  59. height: 150px;
  60. margin-top: 48px;
  61. background-color: blue;
  62. }
  63. .second{
  64. margin-left: -3px;
  65. width: 6px;
  66. height: 180px;
  67. margin-top: 18px;
  68. background-color: green;
  69. /* transform: rotate(30deg); */
  70. }
  71. .second,.hour,.minute{
  72. position: absolute;
  73. left: 50%;
  74. top:0;
  75. transform-origin: center bottom;
  76. /* transform: translateX(-50%); */
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <div class="box">
  82. <div class="clock">
  83. <ul id="clock-item">
  84. </ul>
  85. <div class="hour"></div>
  86. <div class="minute"></div>
  87. <div class="second"></div>
  88. </div>
  89. </div>
  90. <script>
  91. // 获取刻度容器
  92. var clickItem = document.getElementById("clock-item");
  93. // 获取时针
  94. var oHour = document.getElementsByClassName("hour");
  95. oHour = oHour[0];
  96. // 获取分针
  97. var oMinute = document.getElementsByClassName("minute");
  98. oMinute = oMinute[0];
  99. // 获取秒针
  100. var oSecond = document.getElementsByClassName("second");
  101. oSecond = oSecond[0];
  102. // 生成刻度
  103. for(var i=0;i<60;i++){
  104. // 创建li
  105. var oLi = document.createElement("li");
  106. // 为每一个li添加角度
  107. oLi.style.transform = "rotate("+(i*6)+"deg)";
  108. // 将创建好的li插入容器中
  109. clickItem.appendChild(oLi);
  110. }
  111. // 控制时分秒旋转
  112. function move(){
  113. // 获取时间对象
  114. var now = new Date();
  115. // 获取时分秒
  116. var hour = now.getHours();
  117. var minute = now.getMinutes();
  118. var second = now.getSeconds();
  119. // 设置旋转角度
  120. oSecond.style.transform = "rotate("+ (second*6) +"deg)";
  121. oMinute.style.transform = "rotate("+ (minute*6) +"deg)";
  122. oHour.style.transform = "rotate("+ (hour*30) +"deg)";
  123. }
  124. move();
  125. // 定时器每隔一秒运行一次
  126. setInterval(move,1000);
  127. </script>
  128. </body>
  129. </html>