|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>JS + CSS Clock</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + |
| 9 | + |
| 10 | + <div class="clock"> |
| 11 | + <div class="clock-face"> |
| 12 | + <div class="decorated-circle"></div> |
| 13 | + <div class="hand hour-hand"></div> |
| 14 | + <div class="hand min-hand"></div> |
| 15 | + <div class="hand second-hand"></div> |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + |
| 19 | + |
| 20 | + <style> |
| 21 | + html { |
| 22 | + background:#018DED url(http://unsplash.it/1500/1000?image=881&blur=50); |
| 23 | + background-size:cover; |
| 24 | + font-family:'helvetica neue'; |
| 25 | + text-align: center; |
| 26 | + font-size: 10px; |
| 27 | + } |
| 28 | + |
| 29 | + body { |
| 30 | + margin: 0; |
| 31 | + font-size: 2rem; |
| 32 | + display:flex; |
| 33 | + flex:1; |
| 34 | + min-height: 100vh; |
| 35 | + align-items: center; |
| 36 | + } |
| 37 | + |
| 38 | + .clock { |
| 39 | + width: 30rem; |
| 40 | + height: 30rem; |
| 41 | + border:20px solid white; |
| 42 | + border-radius:50%; |
| 43 | + margin:50px auto; |
| 44 | + position: relative; |
| 45 | + padding:2rem; |
| 46 | + box-shadow: |
| 47 | + 0 0 0 4px rgba(0,0,0,0.1), |
| 48 | + inset 0 0 0 3px #EFEFEF, |
| 49 | + inset 0 0 10px black, |
| 50 | + 0 0 10px rgba(0,0,0,0.2); |
| 51 | + } |
| 52 | + |
| 53 | + .clock-face { |
| 54 | + position: relative; |
| 55 | + width: 100%; |
| 56 | + height: 100%; |
| 57 | + transform: translateY(-3px); /* account for the height of the clock hands */ |
| 58 | + } |
| 59 | + |
| 60 | + .hand { |
| 61 | + width:50%; |
| 62 | + height:6px; |
| 63 | + background: black; |
| 64 | + position: absolute; |
| 65 | + top:50%; |
| 66 | + transform: rotate(90deg); |
| 67 | + transform-origin: 100%; |
| 68 | + } |
| 69 | + |
| 70 | + .hand.hour-hand { |
| 71 | + z-index: 5; |
| 72 | + background: #6e5050; |
| 73 | + transform-origin: 0%; |
| 74 | + width: 25%; |
| 75 | + left: 50%; |
| 76 | + } |
| 77 | + |
| 78 | + .hand.second-hand { |
| 79 | + background: red; |
| 80 | + height: 3px; |
| 81 | + opacity: .8; |
| 82 | + } |
| 83 | + |
| 84 | + .decorated-circle { |
| 85 | + z-index: 10; |
| 86 | + position: absolute; |
| 87 | + width: 10px; |
| 88 | + height: 10px; |
| 89 | + top: calc(50% - 2px); |
| 90 | + left: calc(50% - 5px); |
| 91 | + background: black; |
| 92 | + border-radius: 50%; |
| 93 | + } |
| 94 | + </style> |
| 95 | + |
| 96 | + <script> |
| 97 | + (function main() { |
| 98 | + const |
| 99 | + SECONDS = 'seconds', |
| 100 | + MINUTES = 'minutes', |
| 101 | + HOURS = 'hours'; |
| 102 | + |
| 103 | + function getAngle(unit, date) { |
| 104 | + switch (unit) { |
| 105 | + case SECONDS: |
| 106 | + return (360 / 60) * date.getSeconds(); |
| 107 | + case MINUTES: |
| 108 | + return (360 / 60) * date.getMinutes(); |
| 109 | + case HOURS: |
| 110 | + return (360 / 12) * date.getHours(); |
| 111 | + default: |
| 112 | + return NaN; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + function setTime() { |
| 117 | + const date = new Date(); |
| 118 | + |
| 119 | + document.querySelector('.hour-hand').style.transform = `rotate(${getAngle(HOURS, date) + 270}deg)`; |
| 120 | + document.querySelector('.min-hand').style.transform = `rotate(${getAngle(MINUTES, date) + 90}deg)`; |
| 121 | + document.querySelector('.second-hand').style.transform = `rotate(${getAngle(SECONDS, date) + 90}deg)`; |
| 122 | + } |
| 123 | + |
| 124 | + setInterval(setTime, 1000); |
| 125 | + |
| 126 | + })(); |
| 127 | + </script> |
| 128 | +</body> |
| 129 | +</html> |
0 commit comments