这周写自己的项目发现又用到日历了,加之自己毕业之后的第一个工作中遇到的任务也是需要写个日历(组员写了,我就不用写了)
今天就来好好折腾一下日历是怎么写的。
首先,我们看看 windows 的日历。发现总共有这么几个元素。先实现试试。
- 1.年份的选择、月份的选择
- 2.周一 ~ 周日(周日 ~ 周六)
- 3.日历格子 6*7 = 42
从数据的角度来分析日历的实现是比较简单的
分析完之后,让我们跟着 新增/修改 一些代码。
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> border: 1px solid sandybrown; <button id="preMonth" class="year-prev">上一月</button> <button id="nowYear" class="year-now"></button> <button id="nowMonth"></button> <button id="nowDate"></button> <button id="nextMonth" class="year-next">下一月</button> <div id="weekLine" class="week-line"></div> <div id="dateWrap" class="date-wrap"></div> // 1.为了获得每个月的日期有多少,我们需要判断 平年闰年[四年一闰,百年不闰,四百年再闰] const isLeapYear = (year) => { return (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0); // 2.获得每个月的日期有多少,注意 month - [0-11] const getMonthCount = (year, month) => { let count = arr[month] || (isLeapYear(year) ? 29 : 28); return Array.from(new Array(count), (item, value) => value + 1); // 3.获得某年某月的 1号 是星期几,这里要注意的是 JS 的 API-getDay() 是从 [日-六](0-6),返回 number const getWeekday = (year, month) => { let date = new Date(year, month, 1); const getPreMonthCount = (year, month) => { return getMonthCount(year - 1, 11); return getMonthCount(year, month - 1); const getNextMonthCount = (year, month) => { return getMonthCount(year + 1, 0); return getMonthCount(year, month + 1); weekArr = weekStr.split('').map(item => '星期' + item); let oFragWeek = document.createDocumentFragment(); weekArr.forEach(item => { let oSpan = document.createElement('span'); let oText = document.createTextNode(item); oSpan.appendChild(oText); oSpan.classList.add('week-item'); oFragWeek.appendChild(oSpan); let weekWrap = document.getElementById('weekLine'); weekWrap.appendChild(oFragWeek); const updateCalendar = (year, month, day) => { if (typeof year === 'undefined' && typeof month === 'undefined' && typeof day === 'undefined') { let nowDate = new Date(); year = nowDate.getFullYear(); month = nowDate.getMonth(); document.getElementById('nowYear').innerHTML = year; document.getElementById('nowMonth').innerHTML = month + 1; document.getElementById('nowDate').innerHTML = day; // 生成日历数据,上个月的 x 天 + 当月的 [28,29,30,31]天 + 下个月的 y 天 = 42 let currentMonth = getMonthCount(year, month); let preMonth = getPreMonthCount(year, month); let nextMonth = getNextMonthCount(year, month); let whereMonday = getWeekday(year, month); let preArr = preMonth.slice(-1 * whereMonday); let nextArr = nextMonth.slice(0, 42 - currentMonth.length - whereMonday); res = [].concat(preArr, currentMonth, nextArr); // 上面经过我本人的测试是没有什么问题,接下来就是更新 dom 的信息的问题 let hadDom = document.getElementsByClassName('date-item'); if (hadDom && hadDom.length) { let domArr = document.getElementsByClassName('date-item'); for (let i = 0; i < domArr.length; i++) { domArr[i].innerHTML = res.shift(); for (let i = 0; i < 6; i++) { str += '<div class="date-line">'; for (let j = 0; j < 7; j++) { str += `<span class='date-item'>${res.shift()}</span>`; document.getElementById('dateWrap').innerHTML = str; let oPreButton = document.getElementById('preMonth'); let oNextButton = document.getElementById('nextMonth'); oPreButton.addEventListener('click', function () { let currentYear = +document.getElementById('nowYear').textContent; let currentMonth = +document.getElementById('nowMonth').textContent - 1; let currentDate = +document.getElementById('nowDate').textContent; if (currentMonth === 0) { updateCalendar(currentYear - 1, 11, currentDate); updateCalendar(currentYear, currentMonth - 1, currentDate); oNextButton.addEventListener('click', function () { let currentYear = +document.getElementById('nowYear').textContent; let currentMonth = +document.getElementById('nowMonth').textContent - 1; let currentDate = +document.getElementById('nowDate').textContent; if (currentMonth === 11) { updateCalendar(currentYear + 1, 0, currentDate); updateCalendar(currentYear, currentMonth + 1, currentDate);
发现用 dom 直接操作而不是通过 mvvm 框架实现确实还是比较蛋疼的,以下是这次实现的效果。
实现一个功能的时候,从数据的层面分析,有时候会比较容易理解
|