10
0

练习题1_时钟.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ul{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. li{
  13. list-style: none;
  14. }
  15. .box{
  16. width: 402px;
  17. height: 402px;
  18. border:1px dashed black;
  19. margin: 100px auto;
  20. }
  21. .clock{
  22. width: 400px;
  23. height: 400px;
  24. border: 1px solid black;
  25. border-radius: 50%;
  26. position: relative;
  27. transform: rotate(180deg);
  28. }
  29. .clock li{
  30. width: 4px;
  31. height: 8px;
  32. background-color: #000;
  33. position: absolute;
  34. top: 0;
  35. left: 50%;
  36. transform-origin: 0 200px;
  37. }
  38. .clock li:nth-child(5n + 1){
  39. height: 16px;
  40. }
  41. .clock div{
  42. position: absolute;
  43. top: 200px;
  44. left: 50%;
  45. margin-left: -2px;
  46. /* transform: translateY(-100%); */
  47. transform-origin: 0 0;
  48. }
  49. .hour{
  50. width: 4px;
  51. height: 80px;
  52. background-color: red;
  53. }
  54. .minute{
  55. width: 4px;
  56. height: 120px;
  57. background-color: blue;
  58. }
  59. .seconds{
  60. width: 4px;
  61. height: 160px;
  62. background-color: green;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <div class="box">
  68. <div class="clock">
  69. <ul>
  70. </ul>
  71. <div class="seconds"></div>
  72. <div class="minute"></div>
  73. <div class="hour"></div>
  74. </div>
  75. </div>
  76. <script>
  77. let oUl = document.getElementsByTagName("ul")[0];
  78. let oSecond = document.getElementsByClassName("seconds")[0];
  79. let oMinute = document.getElementsByClassName("minute")[0];
  80. let oHour = document.getElementsByClassName("hour")[0];
  81. for(let i=0;i<60;i++){
  82. let oLi = document.createElement("li");
  83. oLi.style.transform = `rotate(${i*6}deg)`;
  84. oUl.append(oLi);
  85. }
  86. setInterval(() => {
  87. let nowTimer = new Date();
  88. let second = nowTimer.getSeconds();
  89. let minute = nowTimer.getMinutes();
  90. let hour = nowTimer.getHours();
  91. console.log(second);
  92. oSecond.style.transform = `rotate(${second*6}deg)`;
  93. oMinute.style.transform = `rotate(${minute*6}deg)`;
  94. oHour.style.transform = `rotate(${hour*30}deg)`;
  95. }, 1000);
  96. </script>
  97. </body>
  98. </html>