vue简单的日历

<ul class="date">
<li v-for="(item, index) in list" :key="index" :class="{'bg-color': item.y === year && item.m === month && item.d === day}">
<div class="day" :class="{'text-color': item.cur}">{{item.d}}</div>
<div>哈哈</div>
</li>
</ul>

data () {
return {
year: new Date().getFullYear(), // 今日年份
month: new Date().getMonth() + 1, // 今日月份
day: new Date().getDate(), // 今日日份
currentYear: '', // 当前显示年份
currentMonth: '', // 当前显示月份 0-11,显示时加一
currentDay: '', // 当前显示日份
monthDays: [], // 1-12月的天数
list: []
}
},
mounted () {
this.showCalender()
},
methods: {
isLeap (year) { // 判断是不是闰年
return (year % 100 === 0 ? (year % 400 === 0 ? 1 : 0) : (year % 4 === 0 ? 1 : 0))
},
showCalender (type) {
this.list = []
this.newDate = new Date()
if (!type) this.currentYear = this.newDate.getFullYear()
if (!type) this.currentMonth = this.newDate.getMonth()
this.monthDays = [31, 28 + this.isLeap(this.currentYear), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
this.currentDay = this.newDate.getDate()
this.firstDay = new Date(`${this.currentYear}-${this.currentMonth + 1}-1`)
this.firstnow = this.firstDay.getDay() // 当月第一日是星期几 1-7
if (this.firstnow === 0) this.firstnow = 7
if (this.firstnow > 1) {
// 前一个月份
let monIndex = this.currentMonth
let year
if (monIndex === 0) {
year--
monIndex = 11
} else {
monIndex--
}
for (let i = 0; i < this.firstnow - 1; i++) {
this.list.unshift({
y: year,
m: monIndex + 1,
d: this.monthDays[monIndex] - i
})
}
}
for (let i = 0; i < this.monthDays[this.currentMonth]; i++) {
// 当前月份
this.list.push({
y: this.currentYear,
m: this.currentMonth + 1,
d: i + 1,
cur: true
})
}
const num = (this.monthDays[this.currentMonth] + this.firstnow - 1) % 7
if (num > 0) {
// 下个月份
let monIndex2 = this.currentMonth
let year2
if (monIndex2 === 11) {
year2++
monIndex2 = 0
} else {
monIndex2++
}
for (let i = 0; i < 7 - num; i++) {
this.list.push({
y: year2,
m: monIndex2 + 1,
d: i + 1
})
}
}
},
preMon () {
if (this.currentMonth === 0) {
this.currentYear--
this.currentMonth = 11
} else {
this.currentMonth--
}
this.showCalender('pre')
},
nextMon () {
if (this.currentMonth === 11) {
this.currentYear++
this.currentMonth = 0
} else {
this.currentMonth++
}
this.showCalender('next')
}


// scss 只放了表格样式
ul{
display: flex;
justify-content: space-around;
flex-wrap: wrap;
align-items: center;
border-top: 1rpx solid #E9E9E9;
border-left: 1rpx solid #E9E9E9;
li{
flex-grow: 1;
min-width: 44px;
height: 50px;
border-bottom: 1rpx solid #E9E9E9;
border-right: 1rpx solid #E9E9E9;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: #999;
font-size: 12px;
.day{
margin-bottom: 6px;
}
.text-color{
color: #222;
}
&.bg-color{
background-color: #E9E9E9;
}
}
}


转载于:https://www.cnblogs.com/xue-wejie/p/10553291.html

以下是一个简单Vue2日历组件的实现,包括期选择和月份切换功能: ```html <template> <div class="calendar"> <div class="header"> <span class="prev" @click="prevMonth"><</span> <span class="title">{{ year }}年{{ month }}月</span> <span class="next" @click="nextMonth">></span> </div> <div class="weekdays"> <span v-for="day in weekdays" :key="day">{{ day }}</span> </div> <div class="days"> <span v-for="day in days" :key="day.date" :class="{ today: day.today, selected: day.selected, disabled: day.disabled }" @click="selectDay(day)" >{{ day.day }}</span> </div> </div> </template> <script> export default { props: { value: { type: String, default: '' } }, data() { return { weekdays: ['', '一', '二', '三', '四', '五', '六'], year: 0, month: 0, days: [] } }, computed: { date() { return this.value ? new Date(this.value) : new Date() }, firstDay() { return new Date(this.year, this.month - 1, 1).getDay() }, lastDate() { return new Date(this.year, this.month, 0).getDate() } }, methods: { init() { this.year = this.date.getFullYear() this.month = this.date.getMonth() + 1 this.days = [] for (let i = 1; i <= this.lastDate; i++) { const date = new Date(this.year, this.month - 1, i) this.days.push({ date, day: i, today: this.isToday(date), selected: this.isSelected(date), disabled: this.isDisabled(date) }) } }, isToday(date) { const today = new Date() return date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth() && date.getDate() === today.getDate() }, isSelected(date) { return this.value && date.getTime() === new Date(this.value).getTime() }, isDisabled(date) { return false // 可以根据需要自定义禁用期的规则 }, selectDay(day) { if (!day.disabled) { this.$emit('input', day.date.toISOString()) } }, prevMonth() { if (this.month === 1) { this.year-- this.month = 12 } else { this.month-- } this.init() }, nextMonth() { if (this.month === 12) { this.year++ this.month = 1 } else { this.month++ } this.init() } }, mounted() { this.init() } } </script> <style> .calendar { width: 200px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; color: #333; } .header { display: flex; justify-content: space-between; align-items: center; padding: 8px; background-color: #f5f5f5; } .weekdays { display: flex; justify-content: space-around; align-items: center; padding: 8px; background-color: #f5f5f5; } .days { display: flex; flex-wrap: wrap; justify-content: space-around; align-items: center; padding: 8px; } .days span { display: inline-flex; justify-content: center; align-items: center; width: 24px; height: 24px; margin: 4px; cursor: pointer; border-radius: 50%; } .days span.today { background-color: #409eff; color: #fff; } .days span.selected { background-color: #f5f5f5; color: #333; } .days span.disabled { color: #ccc; cursor: not-allowed; } </style> ``` 使用方法: ```html <template> <div> <input type="text" v-model="date" readonly> <calendar v-model="date"></calendar> </div> </template> <script> import Calendar from './Calendar.vue' export default { components: { Calendar }, data() { return { date: '' } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值