1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| const $$ = function (select) { return document.querySelectorAll(select); } const numToChinese = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '二十一', '二十二', '二十三', '二十四', '二十五', '二十六', '二十七', '二十八', '二十九', '三十', '三十一']; let timer = null; const monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; let currentYear = new Date().getFullYear() if (currentYear % 4 === 0 && currentYear % 400 !== 0) { monthLength[1] = 29; } const currentMonthLength = monthLength[new Date().getMonth()]
function start() { const tableList = ['month', 'day', 'week', 'hour', 'min', 'seconds'];
const [monthDom, dayDom, weekDom, hourDom, minDom, secondsDom] = tableList.map(className => $$(`#app .${className}`)[0]);
const setTransform = (dom, rate) => { dom.style.transform = `rotateZ(${-rate}deg)` }; const setActive = (dom, currentIndex) => { const domList = dom.querySelectorAll('.active'); if (domList.length > 0) { dom.querySelector('.active').classList.remove('active') } const allNode = dom.querySelectorAll('li'); const currentLi = currentIndex - 1 < 0 ? allNode[allNode.length] : allNode[currentIndex - 1]; currentLi.classList.add('active'); }
const taskObj = { setMonthRate(currentDate) { const currentIndex = currentDate.getMonth() + 1; const rate = currentIndex * 30 - 90;
setActive(monthDom, currentIndex) setTransform(monthDom, rate) }, setWeekRate(currentDate) { const currentIndex = currentDate.getDay(); const rate = (currentIndex - 1) * 360 / 7;
setActive(weekDom, currentIndex) setTransform(weekDom, rate) }, setDayRate(currentDate) { const currentIndex = currentDate.getDate() + 1; const rate = currentIndex * parseInt(360 / currentMonthLength) - 90;
setActive(dayDom, currentIndex - 1) setTransform(dayDom, rate) }, setHourRate(currentDate) { const currentIndex = currentDate.getHours(); const rate = currentIndex * 15 - 90;
setActive(hourDom, currentIndex) setTransform(hourDom, rate) }, setMinRate(currentDate) { const currentIndex = currentDate.getMinutes(); const rate = currentIndex * 6 - 90;
setActive(minDom, currentIndex) setTransform(minDom, rate)
}, setSecondsRate(currentDate) { const currentIndex = currentDate.getSeconds(); const rate = currentIndex * 6 - 90;
setActive(secondsDom, currentIndex) setTransform(secondsDom, rate); }
}
timer = setInterval(() => { const currentDate = new Date(); Object.values(taskObj).forEach(fun => fun(currentDate)) }, 500) }
function end() { clearInterval(timer) } function createTable(config = {}) { const data = { radius: 200, num: 12, unit: '', className: '' }; config = Object.assign(data, config) const offset = parseInt(- config.num / 4);
let i = 1; let ul = `<ul style="width:${config.radius * 2}px;height:${config.radius * 2}px" class="scale ${config.className}">`; while (i <= config.num) { let rate = 360 / config.num * (i + offset); ul += `<li style="transform:rotateZ(${rate}deg);left:${config.radius / 2}px;top:${config.radius - 10}px;width:${config.radius}px"><span>${config.showChinese ? numToChinese[i - 1] : i}${config.unit}</span></li>`; i++ } ul += '</ul>'; return ul; } function createHr(data = {}) { const { radius = 800, left = 170 } = data; const year = new Date().getFullYear() const html = `<div id="hr" style="width:${radius / 2}px;left:${radius / 4}px"> <span>${year}年</span> <span style="position:absolute;left:${left}px">星期</span> </div>`; return html; } function init() { const clientMin = Math.min(document.body.clientHeight, document.body.clientWidth); const clientMinRate = clientMin / 1297;
let list = [ { radius: 250, num: 12, unit: '月', className: 'month', showChinese: true }, { radius: 450, num: 31, unit: '日', className: 'day', showChinese: true }, { radius: 600, num: 7, className: 'week', showChinese: true }, { radius: 700, num: 24, unit: '时', className: 'hour' }, { radius: 800, num: 60, unit: '分', className: 'min', }, { radius: 900, num: 60, unit: '秒', className: 'seconds', } ].map(e => { e.radius = clientMinRate * e.radius return e; }); let tableHTML = list.reduce((total, ele) => { return total + createTable(ele) }, ''); const hrLine = createHr({ radius: 900 * clientMinRate, left: clientMinRate * 235 });
$$('#app')[0].innerHTML = tableHTML + hrLine; start() } init()
|