ZCalendar.js

本想给公司写个专门的日历插件,结果写到中间,说不需要了,遂终!

用法及其简单,当然目前只具备展示的功能,其他功能尚待完善

完善日期未知……

我的设想是不依托于其他 jQ或 moment.js 等任何js库,力求精简

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./ZCalendar.css">
</head>
<body>
<div id='calendar' ></div>
<script src='./ZCalendar.js'></script>
<script>
var ZCalendar = new ZCalendar({
container:'#calendar'
})
</script>
</body>
</html>

不传 y[Number],m[Number] 的情况下默认为当前时间

源码如下

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    
/*
*********************************************************************************
**** config [object | {container[String] , y[Number], m[Number]}];
*********************************************************************************
*/

function ZCalendar(config) {
this.presentTime = new Date();


this.container = config.container;

this.currentMoment = new Date(config.y?config.y: this.presentTime.getFullYear() , config.m?config.m-1 : this.presentTime.getMonth() );

this.year = this.currentMoment.getFullYear();
this.month = this.currentMoment.getMonth() + 1;

this.date = new Date().getDate();

this.init();
}

ZCalendar.prototype = {
init: function () {
this.startDay = this.getStartDay();//该月1号为星期几;
this.endDay = this.getEnDay();//该月最后一天为星期几;

this.startDate = 1,//该月从 1 号开始;
this.endDate = this.getEndDate();//该月 最后一天为几号

this.totalDays = this.endDate;//总天数

this.renderCalendar();

document.getElementById('zc-prev').onclick = this.prevClick.bind(this);
document.getElementById('zc-next').onclick = this.nextClick.bind(this);

},

getStartDay: function () {//获取 该月 1 号为星期几;
return this.currentMoment.getDay();
},

getEndDate: function () {// 获取 该月 最后 一日 为 几号;
return new Date(this.year, this.month, 0).getDate();
},

getEnDay: function () {// 获取 该月 最后 一日 为 星期几;
return new Date(this.year, this.month - 1, this.getEndDate()).getDay();
},

daysAlias: [ //中文星期几的别名
'周日', '周一', '周二', '周三', '周四', '周五', '周六'
],

getChineseAliasDays: function (day) {//获取中文对应的星期几别名;
return daysAlias[day];
},

renderDateTd: function (date) {//渲染每个日期单元;

return '<td class="currentTime' + ((date == this.date) && (this.year == this.presentTime.getFullYear() && (this.month == this.presentTime.getMonth()+1) ) ? ' active' : '') + '" >' + date + '</td>';
},

renderStartEmptyTd: function () {//渲染 每月开始之前的 多余的 周几 的空格 占位符;
return '<td></td>';
},

renderEndEmptyTd: function () {

},

renderCalendarBody: function () {
var calendarTds = '';

for (var j = 0; j < 6; j++) {
calendarTds += '<tr>';

for (var i = 0; i < 7; i++) {
var index = j * 7 + i;

if (index < this.startDay || index >= (this.endDate + this.startDay)) {
calendarTds += this.renderStartEmptyTd();
}

if (index >= this.startDay && index < (this.endDate + this.startDay)) {
calendarTds += this.renderDateTd(index - this.startDay + 1);
}

}

calendarTds += '</tr>'
}

return '<tbody>' + calendarTds + '</tbody>';
},

renderCalendarHead: function () {
var head = '<thead><tr>'
for (var i = 0; i < this.daysAlias.length; i++) {
head += this.renderDateTd(this.daysAlias[i]);
}
head += '</tr></thead>'
return head;
},

renderTimeShowBox: function () {
var timeBox = '<div class="zc-timebox">' +
'<div class="zc-time">' +
'<span id="zc-prev"> < </span>' +
'<span class="zc-time-item">' + this.year + '</span>年' +
'<span class="zc-time-item">' + this.month + '</span>月' +
'<span id="zc-next"> > </span>' +
'</div>' +
'</div>';
return timeBox;
},

renderCalendarTable: function () {
var tableHead = this.renderCalendarHead();
var tableBody = this.renderCalendarBody();

var calendarTabel = '<table class="zc-table" width="100%" border=0 cellspacing=0 >' +
tableHead + tableBody + '</table>';
return calendarTabel;
},

renderCalendar: function () {
var calender = '';
calender += this.renderTimeShowBox() + this.renderCalendarTable();

calender = '<div>' + calender + '</div>';

document.querySelector(this.container).innerHTML = calender;
},


prevClick: function () {
this.currentMoment = new Date(this.year, this.month - 2);

this.year = this.currentMoment.getFullYear();
this.month = this.currentMoment.getMonth() + 1;

this.date = new Date().getDate();

this.init();
},

nextClick: function () {
this.currentMoment = new Date(this.year, this.month);

this.year = this.currentMoment.getFullYear();
this.month = this.currentMoment.getMonth() + 1;

this.date = new Date().getDate();

this.init();
}
}