|
| 1 | +/** |
| 2 | + * 创建出每个装时间的盒子 |
| 3 | + * 包括: |
| 4 | + * 秒/分钟/小时/日/月/年 |
| 5 | + */ |
| 6 | +const box = document.getElementById('box') |
| 7 | + |
| 8 | +/** |
| 9 | + * 创建出每个装时间的盒子 |
| 10 | + * @param {String} labelName 创建的标签的 class 名称 |
| 11 | + */ |
| 12 | +function addBigBox(labelName) { |
| 13 | + const newLabel = document.createElement('div') |
| 14 | + newLabel.setAttribute('class', labelName) |
| 15 | + box.appendChild(newLabel) |
| 16 | +} |
| 17 | + |
| 18 | +addBigBox('secondBox') |
| 19 | +addBigBox('branchBox') |
| 20 | +addBigBox('hourBox') |
| 21 | +addBigBox('NumberBox') |
| 22 | +addBigBox('monthBox') |
| 23 | +addBigBox('year') |
| 24 | + |
| 25 | +/** |
| 26 | + * 获取到所有装时间的盒子 |
| 27 | + */ |
| 28 | +const secondBox = document.querySelector('.secondBox') |
| 29 | +const branchBox = document.querySelector('.branchBox') |
| 30 | +const hourBox = document.querySelector('.hourBox') |
| 31 | +const NumberBox = document.querySelector('.NumberBox') |
| 32 | +const monthBox = document.querySelector('.monthBox') |
| 33 | +const year = document.querySelector('.year') |
| 34 | +// 默认设置 2021 年 |
| 35 | +year.innerHTML = '2021' |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * 渲染时间的标签内容 |
| 40 | + * @param {String} PointerName 指针的名称 |
| 41 | + * @param {Object} bigTabName 需要放到的盒子 DOM 元素 |
| 42 | + * @param {String} explain 说明文字 |
| 43 | + * @param {Number} number 循环次数 |
| 44 | + * @param {Number} deg 旋转度数 |
| 45 | + */ |
| 46 | +function Rendering(PointerName, bigTabName, explain, number, deg) { |
| 47 | + for (let i = 1; i < number; i++) { |
| 48 | + // 新建一个 div 标签 设置 class 名称和旋转角度 |
| 49 | + const Pointer = document.createElement('div') |
| 50 | + Pointer.setAttribute('class', PointerName) |
| 51 | + Pointer.style.transform = `rotate(${i * deg}deg)` |
| 52 | + |
| 53 | + |
| 54 | + // 把新建的 div 都放在大盒子中 |
| 55 | + bigTabName.appendChild(Pointer) |
| 56 | + // 里面分别新建两个 span 标签用来显示时间内容描述 |
| 57 | + const newSpan1 = document.createElement('span') |
| 58 | + const newSpan2 = document.createElement('span') |
| 59 | + newSpan1.innerHTML = i + (number - 1) + explain |
| 60 | + newSpan2.innerHTML = i + explain |
| 61 | + |
| 62 | + // 给新建的 span 标签设置 class 值 并将 span 标签放到新建的 div 中 |
| 63 | + newSpan1.setAttribute('class', 'tra') |
| 64 | + Pointer.appendChild(newSpan1) |
| 65 | + Pointer.appendChild(newSpan2) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +Rendering('second', secondBox, '秒', 31, 6) |
| 70 | +Rendering('branch', branchBox, '分', 31, 6) |
| 71 | +Rendering('hour', hourBox, '时', 7, 30) |
| 72 | +Rendering('Number', NumberBox, '号', 17, 11.25) |
| 73 | +Rendering('month', monthBox, '月', 7, 30) |
| 74 | + |
| 75 | + |
| 76 | +/** |
| 77 | + * 清空指针的 60 分钟 |
| 78 | + * 清空指针的 60 秒 |
| 79 | + * 清空指针的 32 号 |
| 80 | + * @param {String} className Class 值 |
| 81 | + */ |
| 82 | +function subItem(className) { |
| 83 | + const item = document.querySelectorAll(className) |
| 84 | + item[item.length - 1].children[0].innerHTML = '' |
| 85 | +} |
| 86 | + |
| 87 | +subItem('.second') |
| 88 | +subItem('.branch') |
| 89 | +subItem('.Number') |
| 90 | + |
| 91 | + |
| 92 | +/** |
| 93 | + * 等待1.4秒后css动画结束后再获取时间 |
| 94 | + */ |
| 95 | +setTimeout(() => { |
| 96 | + setInterval(() => { |
| 97 | + const now = new Date() |
| 98 | + const s = now.getSeconds() // 秒 |
| 99 | + const m = now.getMinutes() // 分 |
| 100 | + const h = now.getHours() % 12 // 小时 |
| 101 | + const d = now.getDate() // 日 |
| 102 | + const y = now.getMonth() + 1 // 月 |
| 103 | + const n = now.getFullYear() // 年 |
| 104 | + |
| 105 | + secondBox.style.transform = `rotate(-${s * 6}deg)` // 秒 |
| 106 | + branchBox.style.transform = `rotate(-${m * 6}deg)` // 分 |
| 107 | + hourBox.style.transform = `rotate(-${h * 30}deg)` // 时 |
| 108 | + NumberBox.style.transform = `rotate(-${d * 11.25}deg)` // 日 |
| 109 | + monthBox.style.transform = `rotate(-${y * 30}deg)` // 月 |
| 110 | + year.innerHTML = n // 年 |
| 111 | + |
| 112 | + }, 1000) |
| 113 | +}, 1400) |
0 commit comments