JS实现日历

HTML:

javascript 复制代码
<table>
        <thead>
            <tr>
                <th colspan="2">
                    <span class="left"><<</span>
                </th>
                <th colspan="3">
                    <span class="time"></span>
                </th>
                <th colspan="2"><span class="right">>></span>
                </th>
            </tr>
            <tr>
                <th>日</th>
                <th>一</th>
                <th>二</th>
                <th>三</th>
                <th>四</th>
                <th>五</th>
                <th>六</th>
            </tr>
        </thead>
        <tbody class="main">
        </tbody>
    </table>

CSS:

javascript 复制代码
  table {
        width: 320px;
        background: #ffffff;
        color: #000000;
    }

    td,
    th {
        text-align: center;
        cursor: pointer;
        height: 30px;
    }

    td {
        width: 40px;
        height: 40px;
        border-radius: 50px;
        background-color: rgb(244, 241, 241);
    }

JS:

javascript 复制代码
 var oTime = document.querySelector(".time") //头部年月标题显示
        var oMain = document.querySelector(".main") //主体日历数字显示
        var leftBtn = document.querySelector(".left"); //点击左边的按钮,月份减少
        var rightBtn = document.querySelector(".right"); //点击右边的按钮,月份增加
        var time = new Date(); //获取当前系统时间
        leftBtn.onclick = function () {
            time.setMonth(time.getMonth() - 1);
            getPrevDays(time);
            getCurrentDays(time);
            minHead(time);
            mainList(time, getPrevDays(time), getCurrentDays(time));
        }
        rightBtn.onclick = function () {
            time.setMonth(time.getMonth() + 1);
            getPrevDays(time);
            getCurrentDays(time);
            minHead(time);
            mainList(time, getPrevDays(time), getCurrentDays(time));
        }

        function minHead(time) {
            oTime.innerText = time.getFullYear() + "年" + (time.getMonth() + 1) + "月"
        }

        function mainList(time, prevDays, currentDays) {
            var beforeCount = prevDays.length + currentDays.length;
            var cellList = document.querySelectorAll("td");
            for (var i = 0; i < prevDays.length; i++) { //上一个月所占格子
                cellList[i].innerHTML = "<font color='#D3D5DA'>" + prevDays[i] + "</font>"
            }
            for (var i = 0; i < currentDays.length; i++) { //当前月所占格子
                // 显示当前日期背景颜色高亮
                
                if (currentDays[i] == time.getDate()) { //当天日期高亮显示
                    cellList[i + prevDays.length].innerHTML = "<font color='white'>" + currentDays[i] + "</font>";
                    // 背景颜色同样高亮
                    cellList[i + prevDays.length].style.backgroundColor = "#4e6ef2";
                    
                } else { //当月日期白色突出
                    cellList[i + prevDays.length].innerHTML = "<font color='#000000'>" + currentDays[i] + "</font>";
                }
            }
            for (var i = 1; i <= (42 - beforeCount); i++) { //下个月所占格子
                cellList[i + beforeCount - 1].innerHTML = "<font color='#D3D5DA'>" + i + "</font>"
            }
        }

        function createCells() {
            for (var i = 0; i < 6; i++) { //6行
                var oTr = document.createElement("tr");
                for (var j = 0; j < 7; j++) { //7列
                    var oTd = document.createElement("td");
                    oTr.appendChild(oTd);
                }
                oMain.appendChild(oTr);
            }
        }

        function getPrevDays(time) { //获得上一个在本月所占的天数
            var time = new Date(time); //拷贝一份时间,避免时间修改发生冲突
            var list = []; //上一个月所占的天数
            time.setDate(1); //设置成当月1号,便于查看是星期几;
            var day = time.getDay() == 0? 7 : time.getDay(); //如果是0,说明数字第一行需要空出来放上一个月的时间
            //获取上一个月的天数
            time.setDate(0); //系统自动计算到上一个月的最后一天
            var maxDay = time.getDate(); //得到上一个月的最后一天时间
            for (var i = maxDay; i > (maxDay - day); i--) {
                //根据maxDay和day之间的关系,把上一个月的时间放到list中
                list.push(i);
            }
            list.reverse(); //日期升序排列
            return list;
        }

        function getCurrentDays(time) { //获取当前月的天数
            var time = new Date(time); //拷贝时间,原因同上
            time.setDate(1); //确保不会出现跨越现象  如果当前时间是1月31号就会出现跨越到三月份的事情
            var list = [];
            //下面代码是为了获取上一个月的天数
            time.setMonth(time.getMonth() + 1) //修改时间到下一个月
            time.setDate(0); //修改时间日期是0
            var maxDay = time.getDate();
            //获取到上一个月的日期并放到数组中
            for (var i = 1; i <= maxDay; i++) {
                list.push(i);
            }
            return list;
        }
        createCells(); //创建6行7列表格
        getPrevDays(time); //获取上一个月在当前月所占的格子数
        getCurrentDays(time); //获取当前月所占的格子数
        minHead(time); //显示头部标题
        mainList(time, getPrevDays(time), getCurrentDays(time)); //显示主题日历

        // 点击别的日期时改变相对应的字体颜色和背景颜色
        var cellList = document.querySelectorAll("td");
        for (var i = 0; i < cellList.length; i++) {
            cellList[i].onclick = function () {
                // 恢复到默认的样式
                for (var i = 0; i < cellList.length; i++) {
                    cellList[i].classList.remove('selected');
                    cellList[i].style.backgroundColor = "rgb(244, 241, 241)";
                }
                // 更改当前点击的单元格的样式
                this.classList.add('selected');
                this.style.backgroundColor = "#4e6ef2";
            }
        }
相关推荐
xiaoxue..2 分钟前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
摘星编程6 分钟前
在OpenHarmony上用React Native:自定义useHighlight关键词高亮
javascript·react native·react.js
hhy_smile8 分钟前
Class in Python
java·前端·python
小邓吖30 分钟前
自己做了一个工具网站
前端·分布式·后端·中间件·架构·golang
南风知我意95737 分钟前
【前端面试2】基础面试(杂项)
前端·面试·职场和发展
qq_12498707531 小时前
基于Srpingboot心晴疗愈社平台的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·spring·microsoft·毕业设计·计算机毕业设计
大爱编程♡1 小时前
SpringBoot统一功能处理
java·spring boot·后端
LJianK11 小时前
BUG: Uncaught Error: [DecimalError] Invalid argument: .0
前端
2601_949613021 小时前
flutter_for_openharmony家庭药箱管理app实战+用药知识详情实现
android·javascript·flutter
No Silver Bullet1 小时前
Nginx 内存不足对Web 应用的影响分析
运维·前端·nginx