|
| 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="hand hour-hand"></div> |
| 13 | + <div class="hand min-hand"></div> |
| 14 | + <div class="hand second-hand"></div> |
| 15 | + </div> |
| 16 | + </div> |
| 17 | + |
| 18 | + <audio class="tick" src="sounds/tick.wav"></audio> |
| 19 | + <audio class="tock" src="sounds/tock.wav"></audio> |
| 20 | + |
| 21 | + |
| 22 | + <style> |
| 23 | + html { |
| 24 | + background:#abf1bc; |
| 25 | + background-size:cover; |
| 26 | + font-family:'helvetica neue'; |
| 27 | + text-align: center; |
| 28 | + font-size: 10px; |
| 29 | + } |
| 30 | + |
| 31 | + body { |
| 32 | + font-size: 2rem; |
| 33 | + display:flex; |
| 34 | + flex:1; |
| 35 | + min-height: 100vh; |
| 36 | + align-items: center; |
| 37 | + } |
| 38 | + |
| 39 | + .clock { |
| 40 | + background: url('images/face.png'); |
| 41 | + width: 400px; |
| 42 | + height: 400px; |
| 43 | + margin:50px auto; |
| 44 | + position: relative; |
| 45 | + } |
| 46 | + |
| 47 | + .clock-face { |
| 48 | + position: relative; |
| 49 | + width: 100%; |
| 50 | + height: 100%; |
| 51 | + transform: translateY(-8px); /* account for the height of the clock hands */ |
| 52 | + } |
| 53 | + |
| 54 | + .hand { |
| 55 | + width:50%; |
| 56 | + height:50px; |
| 57 | + background:url('images/clockhand1.png'); |
| 58 | + position: absolute; |
| 59 | + top:50%; |
| 60 | + transform-origin: 100%; |
| 61 | + transition: all 0.5s; |
| 62 | + transition-timing-function: cubic-bezier(0.1, 2.8, 0.5, 1); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + </style> |
| 67 | + |
| 68 | + <script> |
| 69 | + (function (){ |
| 70 | + const secondHand = document.querySelector('.second-hand'); |
| 71 | + const minHand = document.querySelector('.min-hand'); |
| 72 | + const hourHand = document.querySelector('.hour-hand'); |
| 73 | + |
| 74 | + function getTransitionTime(time){ |
| 75 | + const transitionTime = time === 0 ? 0 : 0.05; |
| 76 | + return transitionTime |
| 77 | + } |
| 78 | + |
| 79 | + function setDate(){ |
| 80 | + const now = new Date(); |
| 81 | + const seconds = now.getSeconds(); |
| 82 | + const minutes = now.getMinutes(); |
| 83 | + const hours = now.getHours(); |
| 84 | + const secondsDegrees = (seconds/ 60) * 360 + 90; |
| 85 | + const minutesDegrees = (minutes/ 60) * 360 + 90; |
| 86 | + const hoursDegrees = (hours/12) * 360 + 90; |
| 87 | + const tick = document.querySelector('.tick'); |
| 88 | + const tock = document.querySelector('.tock'); |
| 89 | + seconds % 2 === 0 ? tick.play() : tock.play(); |
| 90 | + secondHand.style.transition = `all ${getTransitionTime(seconds)}s`; |
| 91 | + minHand.style.transition = `all ${getTransitionTime(minutes)}s`; |
| 92 | + hourHand.style.transition = `all ${getTransitionTime(hours)}s`; |
| 93 | + secondHand.style.transform = `rotate(${secondsDegrees}deg) scaleX(1.5)`; |
| 94 | + minHand.style.transform = `rotate(${minutesDegrees}deg)`; |
| 95 | + hourHand.style.transform = `rotate(${hoursDegrees}deg) scale(0.7, 1.5)`; |
| 96 | + } |
| 97 | + |
| 98 | + setInterval(setDate, 1000); |
| 99 | + })(); |
| 100 | + </script> |
| 101 | +</body> |
| 102 | +</html> |
0 commit comments