-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_nolunar.html
412 lines (409 loc) · 15.6 KB
/
index_nolunar.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="iconfont/iconfont.css">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<title>日历</title>
<style>
*{
padding: 0;
margin: 0;
}
body,html{
width: 100%;
height: 100%;
background: #efeff4;
}
ul{
list-style: none;
margin: 0;
padding: 0;
}
.calendarDiv{
width: 50%;
height: auto;
border: 1px solid #f5f5f5;
background: #fff;
margin: 0 auto;
padding-bottom: 18px;
}
.calendar_title{
width: 100%;
height: 40px;
line-height: 40px;
text-align: center;
}
.calendar_title i{
cursor: pointer;
padding: 5px;
}
.calendar_title span, .calendar_title i{
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}
.calendar_title i:active{
background: #eee;
}
.calendar_content ul li{
float: left;
width: calc(100% / 7);
height: 40px;
line-height: 40px;
text-align: center;
}
.calendar_content ul li span{
cursor: pointer;
}
.actived{
/*border-radius: 50%;
background: #2c9dfc;
color: #fff;
padding: 4px;*/
border-radius: 50%;
background: #2c9dfc;
color: #fff;
width: 30px;
height: 30px;
line-height: 30px;
display: inline-block;
vertical-align: middle;
}
.today{
border-radius: 50%;
border: 1px solid #2c9dfc;
color: #2c9dfc;
width: 30px;
height: 30px;
line-height: 30px;
display: inline-block;
vertical-align: middle;
}
.notMonth{
color: #d7d7d7;
cursor: no-drop !important;
}
.weekend{
color: red;
}
.shrinkageDiv{
width: 30px;
height: 30px;
line-height: 30px;
border-radius: 50%;
box-shadow: 0 2px 5px #cdcdcd;
background: #fff;
float: right;
margin-right: 25px;
text-align: center;
}
.shrinkage {
font-size: 16px;
color: #b5b5b5;
cursor: pointer;
}
.rotate{
display: block;
transform: rotate(180deg);
}
</style>
</head>
<body>
<div id="calendar">
<div class="calendarDiv">
<div class="calendar_title">
<i class="iconfont icon-zuojiantou" @click="lastMonth"></i>
<span>{{year}}年{{month}}月</span>
<i class="iconfont icon-youjiantou" @click="nextMonth"></i>
</div>
<div class="calendar_content">
<!--星期-->
<ul>
<li
v-for="(weeks,weIndex) in week"
:key="weIndex"
:class="weeks === '日' || weeks === '六' ? 'weekend' : '' ">
{{weeks}}
</li>
</ul>
<!--一周-->
<ul v-if="showWeek">
<li
v-for="(wkDates,wkDaIndex) in weekDateList"
:key="wkDaIndex"
@click="clickDates(wkDates,wkDaIndex)">
<span :class="wkDates.type ? wkDates.type : '' || activedDate === wkDates.nowDate ? 'actived' : ''">
{{wkDates.date}}
</span>
</li>
</ul>
<!--日期-->
<ul v-if="!showWeek">
<li
v-for="(dates,daIndex) in dateList"
:key="daIndex"
@click="clickDates(dates,daIndex)">
<span :class="dates.type ? dates.type : '' || activedDate === dates.nowDate ? 'actived' : ''">
{{dates.date}}
</span>
</li>
</ul>
<div style="clear:both;"></div>
</div>
<div class="shrinkageDiv">
<i class="iconfont icon-arrow-up shrinkage" :class="showWeek ? 'rotate' : ''" @click="shrinkageFun"></i>
</div>
</div>
<div style="text-align: center;margin-top: 10%;">
当前日期:{{activedDate}}<br/>{{clickItem}}
</div>
</div>
<script>
new Vue({
el: '#calendar',
data(){
return{
year: '',
month: '',
day: '',
times: '',//方便寻找一周日期,具体日是根据查询当天算的某年某月X日
week: ['日','一','二','三','四','五','六'],
nuberOne: '',//本月1号是星期几
nuberlast: '',//本月最后1天是星期几
dateList: [],//显示日期
weekDateList: [],//一周日期
showWeek: false,//展示一周状态
activedDate : '',//当前选中日期
clickItem: ''//当前选择对象
}
},
mounted(){
//获取当月数据
this.getThisMonth();
},
methods:{
/*
* @获取当前月数据
* @具体日期、总共多少天、1号和最后1号是星期几
* @组装上日期列表 组装本月日期列表 组装下月日期列表 合并数组
*/
getThisMonth(dates){
//如果没有指定日期就查询当月的
let date;
if(dates){
date = new Date(dates);
}else {
date = new Date();
}
//获取本月具体日期
this.year = date.getFullYear();
this.month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
this.day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
const time = this.year + '-' + this.month + '-' + this.day;
this.times = time;
//计算本月总共有多少天
const thisTotalDay = new Date(this.year, this.month, 0).getDate();
//获取本月1号是星期几
const numberOne = date.setDate(1);
const serialOneNumber = new Date(numberOne).getDay();
this.nuberOne = this.week[serialOneNumber];
//获取本月最后1天是星期几
const numberLast = date.setDate(thisTotalDay);
const serialLastNumber = new Date(numberLast).getDay();
this.nuberlast = this.week[serialLastNumber];
//组装上日期列表
const lastArr = this.getLastObj(serialOneNumber);
//组装本月日期列表
const arr = this.getNowObj(thisTotalDay);
//组装下月日期列表
const nextArr = this.getNextObj(serialLastNumber);
//合并数组
this.dateList = lastArr.concat(arr,nextArr);
//找到本周的数据
let selfIndex,selfWeek;
for(let i = 0; i < this.dateList.length; i++){
const nowDate = this.dateList[i].nowDate;
const week = this.dateList[i].week;
if(nowDate === this.times){
selfIndex = i;
selfWeek = week;
break;
}
}
//根据刚才取得的week序号位置,取出一个区间值 --> ['日','一','二','三','四','五','六']
const weekIndex = this.week.indexOf(selfWeek);
const minNum = weekIndex - 0;
const maxNum = this.week.length - weekIndex;
this.weekDateList = this.dateList.slice((selfIndex - minNum),(selfIndex + maxNum));
},
/*
* @组装上月数据
* @serialOneNumber 这个月第一天星期几
* @setMonth()方法存在的一个BUG
* @拼接为 {
type: 'notMonth',
week: '1',//星期几
date: '今',//当前日期
nowDate: '2019-03-1'//具体日期
}
*/
getLastObj(serialOneNumber){
//虽然知道当月月份,但是涉及跨年,所以不能直接减以获取上月、下月月份
const time = this.times;
const date = new Date(time);
//特别注意!!!:在31号的时候,使用setMonth()方法会出问题,因此在使用setMonth()方法前,先设置日期的方式或者setMonth中填写第二个日期参数
//setMonth():第一个参数是月份,第二个为天数,当我们不设置后面的天数的时候,前面设置的月份,就会自动顺延
//setMonth()其实是设置与当前时间天数相同的月份,如果设置的月份与当前天数不相同则往后顺延一个月
const lastDate = date.setMonth(date.getMonth() - 1, 1);
const lastYear = new Date(lastDate).getFullYear();
const lastMonth = new Date(lastDate).getMonth() + 1 < 10 ? '0' + (new Date(lastDate).getMonth() + 1) : new Date(lastDate).getMonth() + 1;
//计算上月总共多少天 new Date(year, month, 0).getDate()
const lastTotalDay = new Date(lastYear, lastMonth, 0).getDate();
let lastArr = [];
//总共缺几天 -0是因为week从0开始
const lastLack = serialOneNumber - 0;
for(let i = 0; i < lastLack; i++){
let obj = {};
//计算某一天星期几
const nowDate = lastYear + '-' + lastMonth + '-' + [(lastTotalDay - i) < 10 ? '0' + (lastTotalDay - i) : (lastTotalDay - i)];
const matchDate = new Date(Date.parse(nowDate.replace(/-/g, '/')));
const week = this.week[matchDate.getDay()];
obj['type'] = 'notMonth';
obj['date'] = lastTotalDay - i;
obj['week'] = week;
obj['nowDate'] = nowDate;
lastArr.push(obj);
}
//倒序
lastArr = lastArr.reverse();
return lastArr;
},
/*
* @组装下月数据
* @serialLastNumber 这个月最后一天星期几
* @拼接为 {
type: 'notMonth',
week: '1',//星期几
date: '今',//当前日期
nowDate: '2019-05-1'//具体日期
}
*/
getNextObj(serialLastNumber){
//虽然知道当月月份,但是涉及跨年,所以不能直接减以获取上月、下月月份
const time = this.times;
const date = new Date(time);
const nextDate = date.setMonth(date.getMonth() + 1);
const nextYear = new Date(nextDate).getFullYear();
const nextMonth = new Date(nextDate).getMonth() + 1 < 10 ? '0' + (new Date(nextDate).getMonth() + 1) : new Date(nextDate).getMonth() + 1;
let nextArr = [];
//总共缺几天
const nextLack = this.week.length - serialLastNumber;
for(let i = 1; i < nextLack; i++){
let obj = {};
//计算某一天星期几
const nowDate = nextYear + '-' + nextMonth + '-' + (i < 10 ? '0' + i : i);
const matchDate = new Date(Date.parse(nowDate.replace(/-/g, '/')));
const week = this.week[matchDate.getDay()];
obj['type'] = 'notMonth';
obj['date'] = i;
obj['week'] = week;
obj['nowDate'] = nowDate;
nextArr.push(obj);
}
return nextArr;
},
/*
* @计算当月日期列表
* @serialOneNumber 这个月第一天星期几
* @thisTotalDay 本月总共多少天
* 处理特殊情况: 今天
* @拼接为 {
type: '',//类型 today actived
week: '1',//星期几
date: '今',//当前日期
nowDate: '2019-04-1'//具体日期
}
*/
getNowObj(thisTotalDay){
let arr = [];
//X号星期几->起始是1号
const zsDate = new Date();
const zsYear = zsDate.getFullYear();
const zsMonth = zsDate.getMonth() + 1 < 10 ? '0' + (zsDate.getMonth() + 1) : zsDate.getMonth() + 1;
const zsDay = zsDate.getDate() < 10 ? '0' + (zsDate.getDate()) : zsDate.getDate();
const nowDate = zsYear + '-' + zsMonth + '-' + zsDay;//真实日期
for(let i = 1; i <= thisTotalDay; i++){
let obj = {};
const thisDate = this.year + '-' + this.month + '-' + (i < 10 ? '0' + i : i);//当前显示日期
const matchDate = new Date(Date.parse(thisDate.replace(/-/g, '/')));
const week = this.week[matchDate.getDay()];
obj['week'] = week;
obj['nowDate'] = thisDate;
//如果是今天特殊处理
if(nowDate === thisDate){
obj['type'] = 'today';
obj['date'] = '今';
}else{
obj['type'] = '';
obj['date'] = i;
}
arr.push(obj);
}
return arr;
},
/*
* @日期点击事件
* @点击灰色数字,跳转到相应月份并且选中
*/
clickDates(item,index){
this.activedDate = item.nowDate;
this.clickItem = item;
if(item.type === 'notMonth'){
const dates = new Date(item.nowDate);
this.getThisMonth(dates);
}
},
/*
* @上一个月
*/
lastMonth(){
const nowDate = this.times;
const date = new Date(nowDate);
const lastDate = date.setMonth(date.getMonth() - 1);
const lastYear = new Date(lastDate).getFullYear();
const lastMonth = new Date(lastDate).getMonth() + 1 < 10 ? '0' + (new Date(lastDate).getMonth() + 1) : new Date(lastDate).getMonth() + 1;
const lastDay = new Date(lastDate).getDate();
const lastDatas = lastYear + '-' + lastMonth + '-' + lastDay;
this.getThisMonth(lastDatas);
},
/*
* @下一个月
*/
nextMonth(){
const nowDate = this.times;
const date = new Date(nowDate);
const nextDate = date.setMonth(date.getMonth() + 1);
const nextYear = new Date(nextDate).getFullYear();
const nextMonth = new Date(nextDate).getMonth() + 1 < 10 ? '0' + (new Date(nextDate).getMonth() + 1) : new Date(nextDate).getMonth() + 1;
const nextDay = new Date(nextDate).getDate();
const nextDatas = nextYear + '-' + nextMonth + '-' + nextDay;
this.getThisMonth(nextDatas);
},
/*
* @ 展示一周还是展示全月
* */
shrinkageFun(){
if(this.showWeek){
this.showWeek = false;
}else {
this.showWeek = true;
}
}
}
})
</script>
</body>
</html>