-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcanlendar.vue
206 lines (194 loc) · 4.75 KB
/
canlendar.vue
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
<template>
<div class="canlendarPanel">
<div class="canlendar-header">
<p class="pre">
<span @click="changeDate('preYear')">
<icon class="icon" name="preYear" />
</span>
<span @click="changeDate('preMonth')">
<icon class="icon" name="preMonth" />
</span>
</p>
<p class="currenDate">{{ `${year}年${month}月` }}</p>
<p class="next">
<span @click="changeDate('nextMonth')">
<icon class="icon" name="nextMonth" />
</span>
<span @click="changeDate('nextYear')">
<icon class="icon" name="nextYear" />
</span>
</p>
</div>
<div class="canlendar-main">
<ul class="main-header">
<li v-for="(item, index) in week">
{{ item }}
</li>
</ul>
<ul class="main">
<li v-for="inx in weekDay" class="disabled">
{{ preMonthSize - weekDay + inx }}
</li>
<li
v-for="index in monthList[month - 1]"
>
<span
:class="{ currentDay: index === day }"
>
{{ index }}</span
>
</li>
<li v-for="index in lastWeekDay" class="disabled">{{ index }}</li>
</ul>
</div>
</div>
</template>
<script lang="ts">
import { Component, Vue } from 'com/vue-ts'
@Component
export default class CanlendarPanel extends Vue {
year: number = 0
month: number = 0
day: number = 0
week: string[] = ['日', '一', '二', '三', '四', '五', '六']
monthList: number[] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
weekDay: number = 1
lastWeekDay: number = 1
// 上一个月有多少天
get preMonthSize() {
return this.month - 1 === 0 ? 31 : this.monthList[this.month - 2]
}
created() {
this.getCurrentDate()
this.initDate()
}
// 获得今天的日期
getCurrentDate() {
const date = new Date()
this.year = date.getFullYear()
this.month = date.getMonth() + 1
this.day = date.getDate()
}
// 根据年月日获得为星期几
getWeekDay(year: number, month: number, day: number) {
return new Date(`${year}/${month}/${day}`).getDay()
}
initDate() {
if (
(this.year % 4 === 0 && this.year % 100 !== 0) ||
this.year % 400 === 0
) {
this.monthList[1] = 29
} else {
this.monthList[1] = 28
}
// 获得指定年月的1号是星期几
const firstDay = this.getWeekDay(this.year, this.month, 1)
// 在1号前面填充多少个上个月的日期
this.weekDay = firstDay === 7 ? 0 : firstDay
// 获得最后一天是星期几,往后填充多少个
const remineDay = this.getWeekDay(
this.year,
this.month,
this.monthList[this.month - 1]
)
this.lastWeekDay = remineDay === 7 ? 6 : 6 - remineDay
}
changeDate(type: string) {
switch (type) {
case 'preYear':
this.year -= 1
break
case 'preMonth':
// 当前月份为1月, 上一个月分为12月,年份减1
if (this.month === 1) {
this.month = 12
this.year -= 1
} else {
this.month -= 1
}
break
case 'nextYear':
this.year += 1
break
case 'nextMonth':
// 当前月份为12月, 下个月分为1月,年份加1
if (this.month === 12) {
this.month = 1
this.year += 1
} else {
this.month += 1
}
break
default:
break
}
this.initDate()
}
}
</script>
<style lang="scss" scoped>
.canlendarPanel {
height: 260px;
border-bottom: 3px solid var(--background);
display: flex;
flex-direction: column;
.canlendar-header {
line-height: 30px;
background: var(--special-bg);
display: flex;
flex-direction: row;
justify-content: center;
.currenDate {
margin: 0 20px;
font-size: 16px;
font-family: PingFangSC-Medium;
}
.icon {
width: 12px;
height: 12px;
}
}
.canlendar-main {
flex: 1;
display: flex;
flex-direction: column;
.main-header {
display: flex;
line-height: 30px;
color: var(--text-color);
li {
flex: 1;
}
}
ul.main {
flex: 1;
display: flex;
flex-wrap: wrap;
align-items: center;
padding-bottom: 10px;
font-size: 14px;
li {
position: relative;
width: 42px;
line-height: 25px;
cursor: pointer;
}
.disabled {
color: var(--text-color);
cursor: default;
}
.currentDay:before {
content: '';
position: absolute;
display: inline-block;
left: 8px;
width: 24px;
height: 24px;
border-radius: 50%;
border: 1px solid #FFE100;
}
}
}
}
</style>