aupcroft

Stopwatch: HTML, CSS, JavaScript

Jan 18th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Stopwatch</title>
  6. <link rel="stylesheet" href="stopwatch.css">
  7. </head>
  8. <body>
  9.  
  10. <div class="stopwatch">
  11.  
  12. <div class="display">
  13. <span class="hours">00</span><span class="minutes">00</span><span class="seconds">00</span><span class="centiseconds">00</span>
  14. </div>
  15.  
  16. <div class="controls">
  17. <button class="start">Start</button>
  18. <button class="stop">Stop</button>
  19. <button class="reset">Reset</button>
  20. </div>
  21.  
  22. </div>
  23.  
  24. <script src="stopwatch.js"></script>
  25. </body>
  26. </html>
  27.  
  28. //CSS
  29.  
  30. .stopwatch {
  31. width: 500px;
  32. height: 200px;
  33. background-color: #000;
  34. border-radius: 5px;
  35. box-shadow: 0 4px rgba(0, 0, 0, 0.75), 0 0 1px rgba(0, 0, 0, 0.15);
  36. padding: 100px;
  37. }
  38.  
  39. .stopwatch, .stopwatch * {
  40. transition: all 0.15s ease-out;
  41. }
  42.  
  43. .stopwatch .controls {
  44. display: flex;
  45. }
  46.  
  47. .stopwatch .controls button {
  48. flex-grow: 1;
  49. margin: 20px 5px 10px 10px;
  50. padding: 10px 0;
  51. border-radius: 5px;
  52. border: 0;
  53. outline: 0;
  54. font-size: 16px;
  55. color: white;
  56. cursor: pointer;
  57. font-weight: bold;
  58. }
  59.  
  60. .stopwatch .controls button:active {
  61. margin-bottom: 0;
  62. margin-top: 4px;
  63. box-shadow: none;
  64. }
  65.  
  66. .stopwatch .controls .start {
  67. background-color: #5d5;
  68. }
  69.  
  70. .stopwatch .controls .start:hover {
  71. background-color: #6e6;
  72. }
  73.  
  74. .stopwatch .controls .stop {
  75. background-color: #d55;
  76. }
  77.  
  78. .stopwatch .controls .stop:hover {
  79. background-color: #e66;
  80. }
  81.  
  82. .stopwatch .controls .reset {
  83. background-color: #55d;
  84. }
  85.  
  86. .stopwatch .controls .reset:hover {
  87. background-color: #66e;
  88. }
  89.  
  90. @font-face {
  91. font-family: 'digital-7';
  92. src: url('digital-7.ttf');
  93. }
  94.  
  95.  
  96. .stopwatch .display {
  97. margin-left: 150px;
  98. font-size: 50px;
  99. width: 100px;
  100. color: #fff;
  101. font-family: 'digital-7', sans-serif;
  102. margin-top: 10px;
  103.  
  104. }
  105.  
  106. .stopwatch .display :not(:last-child):after {
  107. content: ':';
  108. }
  109.  
  110. //JavaScript
  111.  
  112. var ss = document.getElementsByClassName('stopwatch');
  113.  
  114. [].forEach.call(ss, function (s) {
  115. var currentTimer = 0,
  116. interval = 0,
  117. lastUpdateTime = new Date().getTime(),
  118. start = s.querySelector('button.start'),
  119. stop = s.querySelector('button.stop'),
  120. reset = s.querySelector('button.reset'),
  121.  
  122. hrs = s.querySelector('span.hours'),
  123. mins = s.querySelector('span.minutes'),
  124. secs = s.querySelector('span.seconds'),
  125. cents = s.querySelector('span.centiseconds');
  126.  
  127. start.addEventListener('click', startTimer);
  128. stop.addEventListener('click', stopTimer);
  129. reset.addEventListener('click', resetTimer);
  130.  
  131. function pad (n) {
  132. return ('00' + n).substr(-2);
  133. }
  134.  
  135. function update () {
  136. var now = new Date().getTime(),
  137. dt = now - lastUpdateTime;
  138.  
  139. currentTimer += dt;
  140.  
  141. var time = new Date(currentTimer);
  142.  
  143. hrs.innerHTML = pad(time.getHours());
  144. mins.innerHTML = pad(time.getMinutes());
  145. secs.innerHTML = pad(time.getSeconds());
  146. cents.innerHTML = pad(Math.floor(time.getMilliseconds() / 10));
  147.  
  148.  
  149. lastUpdateTime = now;
  150. }
  151.  
  152. function startTimer () {
  153. if (!interval) {
  154. lastUpdateTime = new Date().getTime();
  155. interval = setInterval(update, 1);
  156. }
  157. }
  158.  
  159. function stopTimer () {
  160. clearInterval(interval);
  161. interval = 0;
  162. }
  163.  
  164. function resetTimer () {
  165. stopTimer();
  166.  
  167. currentTimer = 0;
  168.  
  169. hrs.innerHTML = mins.innerHTML = secs.innerHTML = cents.innerHTML = pad(0);
  170. }
  171. });
Add Comment
Please, Sign In to add comment