|
| 1 | +var hoursContainer = document.querySelector('.hours') |
| 2 | +var minutesContainer = document.querySelector('.minutes') |
| 3 | +var secondsContainer = document.querySelector('.seconds') |
| 4 | +var dateElement = document.querySelector('.date') |
| 5 | +var monthElement = document.querySelector('.month') |
| 6 | +var yearElement = document.querySelector('.year') |
| 7 | +var tickElements = Array.from(document.querySelectorAll('.tick')) |
| 8 | + |
| 9 | +var last = new Date(0) |
| 10 | +last.setUTCHours(-1) |
| 11 | + |
| 12 | +var tickState = true |
| 13 | + |
| 14 | +function updateTime() { |
| 15 | + var now = new Date(); |
| 16 | + |
| 17 | + var lastHours = last.getUTCHours().toString(); |
| 18 | + var nowHours = now.getUTCHours().toString(); |
| 19 | + if (lastHours !== nowHours) { |
| 20 | + updateContainer(hoursContainer, nowHours); |
| 21 | + } |
| 22 | + |
| 23 | + var lastMinutes = last.getMinutes().toString(); |
| 24 | + var nowMinutes = now.getMinutes().toString(); |
| 25 | + if (lastMinutes !== nowMinutes) { |
| 26 | + updateContainer(minutesContainer, nowMinutes); |
| 27 | + } |
| 28 | + |
| 29 | + var lastSeconds = last.getSeconds().toString(); |
| 30 | + var nowSeconds = now.getSeconds().toString(); |
| 31 | + if (lastSeconds !== nowSeconds) { |
| 32 | + updateContainer(secondsContainer, nowSeconds); |
| 33 | + } |
| 34 | + |
| 35 | + var day = now.getUTCDate().toString(); |
| 36 | + var month = now.toLocaleDateString('en-US', { month: 'long' }); |
| 37 | + var year = now.getUTCFullYear().toString(); |
| 38 | + |
| 39 | + dateElement.textContent = day; |
| 40 | + monthElement.textContent = month; |
| 41 | + yearElement.textContent = year; |
| 42 | + |
| 43 | + last = now; |
| 44 | +} |
| 45 | + |
| 46 | +function tick() { |
| 47 | + tickElements.forEach(t => t.classList.toggle('tick-hidden')); |
| 48 | +} |
| 49 | + |
| 50 | +function updateContainer(container, newTime) { |
| 51 | + var time = newTime.split(''); |
| 52 | + |
| 53 | + if (time.length === 1) { |
| 54 | + time.unshift('0'); |
| 55 | + } |
| 56 | + |
| 57 | + var first = container.firstElementChild; |
| 58 | + if (first.lastElementChild.textContent !== time[0]) { |
| 59 | + updateNumber(first, time[0]); |
| 60 | + } |
| 61 | + |
| 62 | + var last = container.lastElementChild; |
| 63 | + if (last.lastElementChild.textContent !== time[1]) { |
| 64 | + updateNumber(last, time[1]); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function updateNumber(element, number) { |
| 69 | + var second = element.lastElementChild.cloneNode(true); |
| 70 | + second.textContent = number; |
| 71 | + |
| 72 | + element.appendChild(second); |
| 73 | + element.classList.add('move'); |
| 74 | + |
| 75 | + setTimeout(function () { |
| 76 | + element.classList.remove('move'); |
| 77 | + }, 990); |
| 78 | + setTimeout(function () { |
| 79 | + element.removeChild(element.firstElementChild); |
| 80 | + }, 990); |
| 81 | +} |
| 82 | + |
| 83 | +setInterval(updateTime, 100); |
0 commit comments