-
Notifications
You must be signed in to change notification settings - Fork 1
/
calender.html
154 lines (140 loc) · 4.32 KB
/
calender.html
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
<html>
<head>
<meta charset="utf-8"/>
<title>Pull to Refresh</title>
<meta name="viewport"
content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;"/>
<style>
.calender {
margin-top: 20px;
}
.calender span {
display: inline-block;
border: 0.5px solid #ddd;
width: 50px;
height: 30px;
line-height: 30px;
text-align: center;
vertical-align: top;
font-size: 14px;
letter-spacing: 0;
}
.calender, .trends{
letter-spacing: -3px;
font-size: 0;
}
</style>
</head>
<body>
<div id="indexDB">
<div onclick="test(this)">测试1</div>
</div>
<span onclick="add()">添加</span>
<!--日历布局-->
<div><span onclick="arrowLeft()"><<</span><span class="calenderShow">2017年2月</span><span onclick="arrowRight()">>></span></div>
<div class="calender">
<div>
<span>周日</span><span>周一</span><span>周二</span><span>周三</span><span>周四</span><span>周五</span><span>周六</span>
<div class="trends"></div>
</div>
</div>
<script>
// 获取当前年月
var month = new Date().getMonth() + 1
var year = new Date().getFullYear()
function arrowLeft() {
month --
if (month == 0) {
month = 12
year --
}
getCalender (year, month)
}
function arrowRight() {
month ++
if (month == 13) {
month = 1
year ++
}
getCalender (year, month)
}
// 获取当月日历
getCalender (year, month) // 月份从1开始
// 日历布局
function getCalender(year, mounth) {
// 存储日历数据的数组
var calenderArr = []
var html = ""
// 获取本月的第一天是星期几 周日 0 周一 1...
var firstDay = new Date(year, mounth - 1, 1).getDay() // 参数的月份是从0开始
// 获取本月一共多少天
var curMonthdays = new Date(year, mounth, 0).getDate() // 参数的月份从1开始
var n = 0
// 行
for (var i = 0; i < 6; i++) {
html += "<div>"
// 列
for (j = 0; j < 7; j++) {
if (j === firstDay || n > 0) {
n ++
}
if (!n && j < firstDay) {
calenderArr.push("")
html += "<span> </span>"
} else if (n && n <= curMonthdays) {
calenderArr.push(n)
html += "<span>" + n + "</span>"
} else if (n && n >= curMonthdays) {
calenderArr.push("")
html += "<span> </span>"
}
}
html += "</div>"
if (n >= curMonthdays) {
break
}
}
// 获取容器
var calenderWrap = document.querySelector('.trends')
calenderWrap.innerHTML = ""
calenderWrap.innerHTML += html
document.querySelector('.calenderShow').innerText = year + "年" + month + "月"
}
// 获取当前月的天数
function getCurMonthDays () {
var date = new Date()
var year = date.getFullYear()
var mouth = date.getMonth() + 1
var days = new Date(year, mouth, 0)
return days.getDate()
}
// 获取近12个月
function getLast12Months(){
var last12Months = [];
var today = new Date();
var month = today.getMonth() + 1;
var year = today.getFullYear()
for(var i = 0; i < 12; i ++){
last12Months.push(year + '年' + month + '月')
if (month === 1) {
year --
month = 13
}
month --
}
}
getLast12Months()
function test (span) {
var allSpan = document.querySelector('#indexDB').querySelectorAll('div')
allSpan.forEach((item) => {
item.classList.remove('cur')
})
span.classList.add('cur')
}
function add () {
var string = "<div onclick='test(this)'>测试1</div>"
document.getElementById('indexDB').innerHTML += string
}
</script>
</body>
</html>