-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
88 lines (76 loc) · 3.32 KB
/
calendar.js
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
(function ($, window) {
$.fn.calendar = function (config) {
var opt = $.extend({
date: '', //yyyy-MM || yyyy/MM
isWeekend: true, //周末是否变色
curDayClass: 'current',
onChanged: function () {}
}, config);
function getDays(year, month) {
// 年 月少一个月 上一天0 = 上一个月
return new Date(year, month, 0).getDate();
}
var self = this,
yearSelect = 0,
monthSelect = 0;
self.goToNextMonth = function () {
if (monthSelect++ >= 12) {
yearSelect += 1;
monthSelect = 1;
}
renderTbody(yearSelect, monthSelect);
console.log(yearSelect, monthSelect);
}
self.goToPrevMonth = function () {
if (monthSelect-- <= 1) {
yearSelect -= 1;
monthSelect = 12;
}
renderTbody(yearSelect, monthSelect);
console.log(yearSelect, monthSelect);
}
function renderTbody (year, month) {
var today = new Date(),
curYear = today.getFullYear(),
curMonth = today.getMonth() + 1,
curDate = today.getDate();
// 获取默认的选中时间
var _date = opt.date ? opt.date.split(/[-||\/]/) : [new Date().getFullYear(), new Date().getMonth() + 1];
// 当有选中的时间后,修改默认的时间
var _year = +year || +_date[0],
_month = +month || +_date[1],
// 当前星期几
_firstDay = new Date(_year, _month -1, 1).getDay(),
_weeks = ['日', '一', '二', '三', '四', '五', '六'];
var thead = '',
tbody = '',
result = '',
isThisMonth = _year === curYear && _month === curMonth ? true : false;
yearSelect = _year;
monthSelect = _month;
// 渲染星期
for (var i = 0; i < 7; i++) {
thead += '<th>' + _weeks[i] + '</th>';
}
thead = '<tr>' + thead + '</tr>';
//渲染天
for (var j = 0, len = _firstDay + getDays(_year, _month); j < len; j++) {
if (j % 7 === 0) {
tbody += '<tr>';
}
tbody += '<td' + (isThisMonth && j - _firstDay + 1 === curDate ? ' class="' + opt.curDayClass + '"' : '') + ' data-id="' + (j - _firstDay + 1) + '"><div><span>' + (j < _firstDay ? ' ' : j - _firstDay + 1) + '</span></div></td>';
if ((j !== 0 && (j + 1) % 7 === 0) || j === len - 1) {
tbody += '<tr>';
}
}
result = thead + tbody;
self.html('<table>' + result + '</table>');
if (opt.isWeekend) {
$('tr', self).find('td:first, td:eq(6), th:first, th:eq(6)').addClass('weekend');
}
opt.onChanged(yearSelect, monthSelect, getDays(yearSelect, monthSelect));
}
// each() 方法规定为每个匹配元素规定运行的函数
return self.each(renderTbody);
}
})(window.jQuery || window.Zepto, window)