练习2_时钟.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. </style>
  50. </head>
  51. <body>
  52. <div class="box">
  53. <div class="clock">
  54. <ul id="clock-item">
  55. </ul>
  56. </div>
  57. </div>
  58. <script>
  59. // 获取刻度容器
  60. var clickItem = document.getElementById("clock-item");
  61. // 生成刻度
  62. for(var i=0;i<60;i++){
  63. // 创建li
  64. var oLi = document.createElement("li");
  65. // 为每一个li添加角度
  66. oLi.style.transform = "rotate("+(i*6)+"deg)";
  67. // 将创建好的li插入容器中
  68. clickItem.appendChild(oLi);
  69. }
  70. </script>
  71. </body>
  72. </html>