• 使用JavaScript及HTML、CSS完成秒表计时器


    案例要求

    1.界面为一个显示计时面板和三个按钮分别为:开始,暂停,重置
    2.点击开始,面板开始计时,
    3.点击暂停,面板停止
    4.点击重置,计时面板重新为0

    案例源码

    1. 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>秒表计时器title>
    7. <style>
    8. body {
    9. font-family: Arial, sans-serif;
    10. background-color: #f4f4f4;
    11. margin: 0;
    12. padding: 0;
    13. display: flex;
    14. justify-content: center;
    15. align-items: center;
    16. height: 100vh;
    17. }
    18. .container {
    19. text-align: center;
    20. }
    21. #timerDisplay {
    22. font-size: 36px;
    23. margin-bottom: 20px;
    24. }
    25. .clock {
    26. width: 200px;
    27. height: 200px;
    28. border: 8px solid #007bff;
    29. border-radius: 50%;
    30. position: relative;
    31. margin-bottom: 20px;
    32. }
    33. .hand {
    34. width: 2px;
    35. background-color: #007bff;
    36. position: absolute;
    37. top: 50%;
    38. left: 50%;
    39. transform-origin: bottom center;
    40. }
    41. .hour-hand {
    42. height: 50px;
    43. }
    44. .minute-hand {
    45. height: 70px;
    46. }
    47. .second-hand {
    48. height: 80px;
    49. background-color: red;
    50. }
    51. .center-dot {
    52. width: 8px;
    53. height: 8px;
    54. background-color: #007bff;
    55. border-radius: 50%;
    56. position: absolute;
    57. top: 50%;
    58. left: 50%;
    59. transform: translate(-50%, -50%);
    60. }
    61. .buttons {
    62. display: flex;
    63. justify-content: center;
    64. }
    65. .button {
    66. background-color: #007bff;
    67. color: #fff;
    68. border: none;
    69. padding: 10px 20px;
    70. cursor: pointer;
    71. margin-right: 10px;
    72. }
    73. style>
    74. head>
    75. <body>
    76. <div class="container">
    77. <div id="timerDisplay">00:00:00div>
    78. <div class="clock">
    79. <div class="hand hour-hand">div>
    80. <div class="hand minute-hand">div>
    81. <div class="hand second-hand">div>
    82. <div class="center-dot">div>
    83. div>
    84. <div class="buttons">
    85. <button id="startButton" class="button">开始button>
    86. <button id="pauseButton" class="button">暂停button>
    87. <button id="resetButton" class="button">重置button>
    88. div>
    89. div>
    90. <script>
    91. var timer;
    92. var hours = 0;
    93. var minutes = 0;
    94. var seconds = 0;
    95. function startTimer() {
    96. timer = setInterval(updateTimer, 1000);
    97. }
    98. function pauseTimer() {
    99. clearInterval(timer);
    100. }
    101. function resetTimer() {
    102. clearInterval(timer);
    103. hours = 0;
    104. minutes = 0;
    105. seconds = 0;
    106. updateDisplay();
    107. }
    108. function updateTimer() {
    109. seconds++;
    110. if (seconds == 60) {
    111. seconds = 0;
    112. minutes++;
    113. }
    114. if (minutes == 60) {
    115. minutes = 0;
    116. hours++;
    117. }
    118. updateDisplay();
    119. updateClock();
    120. }
    121. function updateDisplay() {
    122. var displayHours = (hours < 10) ? "0" + hours : hours;
    123. var displayMinutes = (minutes < 10) ? "0" + minutes : minutes;
    124. var displaySeconds = (seconds < 10) ? "0" + seconds : seconds;
    125. document.getElementById("timerDisplay").innerText = displayHours + ":" + displayMinutes + ":" + displaySeconds;
    126. }
    127. function updateClock() {
    128. var hourHand = document.querySelector(".hour-hand");
    129. var minuteHand = document.querySelector(".minute-hand");
    130. var secondHand = document.querySelector(".second-hand");
    131. var hourRotation = (hours % 12) * 30 + minutes * 0.5;
    132. var minuteRotation = minutes * 6 + seconds * 0.1;
    133. var secondRotation = seconds * 6;
    134. hourHand.style.transform = `translate(-1px, -100%) rotate(${hourRotation}deg)`;
    135. minuteHand.style.transform = `translate(-1px, -100%) rotate(${minuteRotation}deg)`;
    136. secondHand.style.transform = `translate(-1px, -100%) rotate(${secondRotation}deg)`;
    137. }
    138. document.getElementById("startButton").addEventListener("click", startTimer);
    139. document.getElementById("pauseButton").addEventListener("click", pauseTimer);
    140. document.getElementById("resetButton").addEventListener("click", resetTimer);
    141. script>
    142. body>
    143. html>

    案例效果图

  • 相关阅读:
    vscode微博发布案例
    跨境电商测评方式有哪些?
    Android修行手册 - 模板匹配函数matchTemplate详解,从N张图片中找到是否包含五星
    《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 02 Clos拓扑
    ScanNet数据集和YFCC100m数据集下载
    二蛋赠书五期:《Python数据挖掘:入门、进阶与实用案例分析》
    (Java高级教程)第一章Java多线程基础-第一节5:wait和notify
    Es中索引的创建
    人均瑞数系列,瑞数 6 代 JS 逆向分析
    macOS下更改默认的Python版本
  • 原文地址:https://blog.csdn.net/m0_65992344/article/details/138096820