练习题1_时钟.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. .box{
  13. width: 402px;
  14. height: 402px;
  15. border:1px dashed #000;
  16. margin:100px auto;
  17. }
  18. .clock{
  19. width: 400px;
  20. height: 400px;
  21. border:1px solid #000;
  22. border-radius: 50%;
  23. position: relative;
  24. }
  25. .clock li{
  26. list-style: none;
  27. width: 4px;
  28. height: 8px;
  29. background-color: #000;
  30. position: absolute;
  31. left: 50%;
  32. top: 0;
  33. margin-left: -2px;
  34. transform-origin: 2px 200px;
  35. }
  36. /* .clock li:nth-child(1){
  37. transform: rotate(0deg);
  38. }
  39. .clock li:nth-child(2){
  40. transform: rotate(6deg);
  41. }
  42. .clock li:nth-child(3){
  43. transform: rotate(12deg);
  44. } */
  45. .clock li:nth-child(5n+1){
  46. height: 16px;
  47. }
  48. .hours,.minute,.seconds{
  49. position: absolute;
  50. left: 50%;
  51. top:0;
  52. transform-origin:center bottom;
  53. }
  54. .clock .hours{
  55. margin-left: -6px;
  56. width: 12px;
  57. height: 100px;
  58. background-color: red;
  59. transform: translateY(100px);
  60. }
  61. .clock .minute{
  62. margin-left: -4px;
  63. width: 8px;
  64. height: 150px;
  65. background-color: blue;
  66. transform: translateY(50px);
  67. }
  68. .clock .seconds{
  69. margin-left: -2px;
  70. width: 4px;
  71. height: 180px;
  72. transform: translateY(20px);
  73. background-color: green;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <div class="box">
  79. <div class="clock">
  80. <!-- 刻度 -->
  81. <ul></ul>
  82. <div class="zz">
  83. <div class="hours"></div>
  84. <div class="minute"></div>
  85. <div class="seconds"></div>
  86. </div>
  87. </div>
  88. </div>
  89. <script>
  90. let oUl = document.getElementsByTagName("ul")[0];
  91. for(let i = 0;i<60;i++){
  92. let oLi = document.createElement("li");
  93. oLi.style.transform = `rotate(${i*6}deg)`;
  94. oUl.appendChild(oLi);
  95. }
  96. //控制时分秒针的旋转
  97. let oSeconds = document.getElementsByClassName("seconds")[0];
  98. let oMinutes = document.getElementsByClassName("minute")[0];
  99. let oHours = document.getElementsByClassName("hours")[0];
  100. setInterval(function(){
  101. let timer = new Date();
  102. let seconds = timer.getSeconds();
  103. let minutes = timer.getMinutes();
  104. let hours = timer.getHours();
  105. oSeconds.style.transform = `translateY(20px) rotate(${seconds*6}deg)`;
  106. oMinutes.style.transform = `translateY(50px) rotate(${minutes*6}deg)`;
  107. oHours.style.transform = `translateY(100px) rotate(${hours*30+minutes/2}deg)`;
  108. },1000);
  109. </script>
  110. </body>
  111. </html>