|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Clock demo</title> |
| 6 | + |
| 7 | + <style> |
| 8 | + body { |
| 9 | + display: flex; |
| 10 | + justify-content: center; |
| 11 | + align-items: center; |
| 12 | + height: 90vh; |
| 13 | + background: white; |
| 14 | + } |
| 15 | + |
| 16 | + .clock { |
| 17 | + position: relative; |
| 18 | + width: 600px; |
| 19 | + height: 600px; |
| 20 | + } |
| 21 | + |
| 22 | + .hand { |
| 23 | + position: absolute; |
| 24 | + top: 50%; |
| 25 | + left: 50%; |
| 26 | + transform-origin: 50% 50%; /* center */ |
| 27 | + transform: translate(-50%, -50%) rotate(0deg); |
| 28 | + } |
| 29 | + |
| 30 | + /* Optional sizing */ |
| 31 | + .background { height: 600px; } |
| 32 | + #hours { height: 600px; } |
| 33 | + #minutes { height: 600px; } |
| 34 | + #seconds { height: 600px; } |
| 35 | + </style> |
| 36 | +</head> |
| 37 | + |
| 38 | +<body> |
| 39 | + <div class="clock"> |
| 40 | + <img src="background.png" class="background" alt="Clock background"> |
| 41 | + <img src="hours.png" id="hours" class="hand"> |
| 42 | + <img src="minutes.png" id="minutes" class="hand"> |
| 43 | + <img src="seconds.png" id="seconds" class="hand"> |
| 44 | + </div> |
| 45 | + |
| 46 | + <script> |
| 47 | + const h = document.getElementById("hours"); |
| 48 | + const m = document.getElementById("minutes"); |
| 49 | + const s = document.getElementById("seconds"); |
| 50 | + |
| 51 | + function updateClock() { |
| 52 | + const now = new Date(); |
| 53 | + |
| 54 | + const seconds = now.getSeconds(); |
| 55 | + const minutes = now.getMinutes() + seconds / 60; |
| 56 | + const hours = (now.getHours() % 12) + minutes / 60; |
| 57 | + |
| 58 | + h.style.transform = `translate(-50%, -50%) rotate(${hours * 30}deg)`; |
| 59 | + m.style.transform = `translate(-50%, -50%) rotate(${minutes * 6}deg)`; |
| 60 | + s.style.transform = `translate(-50%, -50%) rotate(${seconds * 6}deg)`; |
| 61 | + } |
| 62 | + |
| 63 | + updateClock(); |
| 64 | + setInterval(updateClock, 10); |
| 65 | + </script> |
| 66 | +</body> |
| 67 | +</html> |
0 commit comments