js实现日历功能

 DatePicker.js

class DatePicker{
    constructor(id, callback){
        this.id = id;
        this.callback = callback;
    }

    render(date){
        this.date = date;
        this.update();
    }

    create(parent, type, content){
        const child = document.createElement(type);
        child.textContent = content;
        parent.appendChild(child);
        if (parent.nodeName === 'CAPTION' && content !== '>>'){
            parent.appendChild(document.createTextNode('\u00A0\u00A0\u00A0'));
        }
        return child;
    }

    //计算天数(上个月{-1}, 当前月
    getDays(year, month, i) {
        let days = 0;
        if (i === -1){
            if (month === 0) {
                year -= 1;
                month = 11;
            } else {
                month -= 1;
            }
        }
        days = new Date(year, month, 0).getDate();
        return days;
    }

    update(){
        const root = document.getElementById(this.id);
        const table = this.create(root, 'table')
        table.style.border = '1px solid black';

        const caption = this.create(table, 'caption');
        const year_prev = this.create(caption, 'span', '<<');
        year_prev.addEventListener('click', ()=> {
            this.date.setFullYear(this.date.getFullYear() - 1);
            this.update();
            table.remove();
        })
        const month_prev = this.create(caption, 'span', '<');
        month_prev.addEventListener('click', ()=> {
            this.date.setMonth(this.date.getMonth() - 1);
            this.update();
            table.remove();
        })
        let year = this.date.getFullYear();
        let month = this.date.getMonth() + 1;
        const year_span = this.create(caption, 'span', year);        
        const month_span = this.create(caption, 'span', month);
        const month_next = this.create(caption, 'span', '>');
        month_next.addEventListener('click', ()=> {
            this.date.setMonth(this.date.getMonth() + 1);
            this.update();
            table.remove();
        })
        const year_next = this.create(caption, 'span', '>>');    
        year_next.addEventListener('click', ()=> {
            this.date.setFullYear(this.date.getFullYear() + 1);
            this.update();
            table.remove();
        })    

        const tr = this.create(table, 'tr');
        
        const headers = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
        for(let day of headers){
            this.create(tr, 'th', day);
        }

        let days_now = this.getDays(year, month);    

        let offset = 6;
        let first_day = new Date(year, month - 1, 1).getDay();
        if (first_day){
            offset = first_day - 1;
        } 

        let tr_now = undefined;
        let cur = this.getDays(year, month, -1);

        for (let i = 0; i < Math.ceil((days_now + offset)/ 7) * 7; i++){
            if (!(i % 7)){
                tr_now = this.create(table, 'tr');
            }  
            if (i - offset + 1 <= 0){
                let td = this.create(tr_now, 'td', cur + i - offset + 1);
                td.style.color = '#717171';
            }
            else if(i - offset + 1 > days_now){
                let td = this.create(tr_now, 'td', i - offset + 1 - days_now);
                td.style.color = '#777777';
            }
            else{
                let td = this.create(tr_now, 'td', i - offset + 1);  
                td.addEventListener('click', (event)=>{
                    this.callback(this.id, {
                    year: this.date.getFullYear(),
                    month: this.date.getMonth() + 1,
                    day: event.target.textContent
                    })
                });
            }
        }
    }
}

datepicker.html

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
  <title>CS 142 Project 3, Problem 1</title>
  <link rel="stylesheet" type="text/css" href="datepicker.css" />
</head>

<body>
  <h1>First DatePicker</h1>
  <div id="datepicker1"></div>
  <h1>Second DatePicker</h1>
  <div id="datepicker2"></div>
  <script type="text/javascript" src="DatePicker.js"></script>
  <script type="text/javascript">
    // <![CDATA[
    'use strict';

    /* global DatePicker */

    var datePicker1 = new DatePicker('datepicker1', function (id, fixedDate) {
      console.log(
        'DatePicker with id',
        id,
        'selected date:',
        fixedDate.month + '/' + fixedDate.day + '/' + fixedDate.year,
      );
    });
    datePicker1.render(new Date());
    var datePicker2 = new DatePicker('datepicker2', function (id, fixedDate) {
      console.log(
        'DatePicker with id',
        id,
        'selected date:',
        fixedDate.month + '/' + fixedDate.day + '/' + fixedDate.year,
      );
    });
    datePicker2.render(new Date('1/1/2009'));
    // ]]>
  </script>
</body>

</html>

实现了日历的查看,可以对年月的加减

效果图(想好看可以自己添加css)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值