Vue實(shí)現(xiàn)萬(wàn)年日歷的示例詳解
前言
又是一個(gè)老生常談的功能,接下來(lái)會(huì)從零實(shí)現(xiàn)一個(gè)萬(wàn)年日歷,從布局到邏輯,再到隨處可見(jiàn)的打卡功能。涉及的內(nèi)容有:
- 日歷的布局
- 日期數(shù)據(jù)的產(chǎn)生
- 年月的變化
- 連續(xù)最長(zhǎng)打卡日期
- 補(bǔ)卡邏輯

1.日歷的布局
<template>
<div class="calendar">
<!-- 年份和日期變化 -->
<div class="select">
<span class="select-icon select-sub" @click="changeYear(-1)">《</span>
<span class="select-icon select-sub select-month" @click="changeMonth(-1)"><</span>
<span>{{ currentMonth }}</span>
<span class="select-icon select-add select-month" @click="changeMonth(+1)">></span>
<span class="select-icon select-add" @click="changeYear(+1)">》</span>
</div>
<!-- 日歷標(biāo)題 -->
<ul class="calendar-header">
<li v-for="(item, index) in headerData" :key="index" class="calendar-header-item">
<span>{{ item }}</span>
</li>
</ul>
<!-- 日期內(nèi)容 -->
<div class="calendar-box">
<div class="calendar-box-item" :class="{ delMark: !hasRemarkDays.includes(item) }" v-for="(item, index) in baseDateList" :key="index">
<!-- 最高層級(jí)顯示圓形日期 -->
<div class="item-wrap" :class="{ hasMarked: hasRemarkDays.includes(item) }" @click="toRemark(item, currentDate)">
<span class="item-date">{{ item ? item : "" }}</span>
<span class="item-patch" v-if="isShowPatch && !hasRemarkDays.includes(item) && item && item < currentDate">補(bǔ)卡</span>
<span class="item-current" v-if="currentDate == item"><i></i></span>
<span class="item-continue" v-if="continueMarkObj.maxDay == item">連續(xù){{ continueMarkObj.maxLen }}天</span>
</div>
<!-- 全遮罩背景淡橙色背景 -->
<span class="all-mark" v-if="hasRemarkDays.includes(item)"></span>
<!-- 左右遮罩,如果左邊無(wú)打卡,遮??;如果右邊無(wú)打卡,遮住 -->
<span class="left-mark" v-if="index % 7 === 0 || !hasRemarkDays.includes(item - 1)"></span>
<span class="right-mark" v-if="index % 7 === 6 || !hasRemarkDays.includes(item + 1)"></span>
</div>
</div>
</div>
</template>2.日期數(shù)據(jù)的產(chǎn)生
computed: {
// 返回當(dāng)前的實(shí)際天數(shù),從周一算起
baseDateList() {
let date = new Date(this.currentMonth);
let monthFlag = date.getMonth() + 1;
let yearFlag = date.getFullYear();
let currentData = date.getDay();
let dayLength = new Date(yearFlag, monthFlag, 0).getDate();
// 周一之前的補(bǔ)0
let dateBaseData = [];
for (let i = 0; i < currentData; i++) {
dateBaseData.push(0);
}
// 周一之后的實(shí)際填寫
for (let i = 0; i < dayLength; i++) {
dateBaseData.push(i + 1);
}
return dateBaseData;
},
}其中currentData表示今天是周幾,在此之前的數(shù)組都補(bǔ)充為0,dayLength表示這月共多少天,然后,全部push進(jìn)日期數(shù)據(jù)中。通過(guò)flex布局中的flex-wrap: wrap;讓其自動(dòng)換行即可。
3.年月的變化
// 修改年:當(dāng)type = +1時(shí),表示加一年,為-1時(shí)反之
changeYear(type) {
let time = new Date(this.currentMonth);
let year = time.getFullYear() + type;
let month = time.getMonth() + 1;
this.currentMonth = `${year}-${month}`;
},
// 修改月:當(dāng)type = -1時(shí),表示加一月,為-1時(shí)次之
changeMonth(type) {
let time = new Date(this.currentMonth);
let year = time.getFullYear();
let month = time.getMonth() + 1;
if (month === 12 && type > 0) { // 12月,并且是加的情況,年加1,月變?yōu)?
year++
month = 1
} else if(month === 1 && type < 0) { // 1月,并且是減的情況,年減1,月變?yōu)?2
year--
month = 12
} else { // 其他情況,直接變化變量type的大小
month += type
}
this.currentMonth = `${year}-${month}`;
},引入type,既是加減的標(biāo)志位,又是變量的大小。
4.連續(xù)最長(zhǎng)打卡日期
getMaxDay() {
let arr = this.hasRemarkDays;
let buffChild = [];
let max = [];
for (let i = 0; i < arr.length; i++) {
// 如果子集不連續(xù)了,重新進(jìn)行賦值和更新
if (buffChild.length && arr[i] - buffChild[buffChild.length - 1] > 1) {
max = max.length > buffChild.length ? [...max] : [...buffChild];
buffChild = [];
buffChild.push(arr[i]);
continue;
}
// 如果不是,直接進(jìn)行累計(jì)
buffChild.push(arr[i]);
}
return max.length > buffChild.length ? [...max] : [...buffChild];
},假設(shè)max是最大的數(shù)組,那么,每次找到連續(xù)的數(shù)組都max進(jìn)行對(duì)比并更新。在節(jié)點(diǎn)中,通過(guò):class="{ hasMarked: hasRemarkDays.includes(item) }"的方式,為其添加hasMarked的樣式。
5.補(bǔ)卡日期
補(bǔ)卡邏輯很簡(jiǎn)單,當(dāng)前日期之前的日期,均可進(jìn)行補(bǔ)卡,然后,在觸發(fā)的方法中,通過(guò)$emit的方式向父組件傳遞需要補(bǔ)卡的日期,并觸發(fā)補(bǔ)卡邏輯。
總結(jié):日歷的實(shí)現(xiàn)主要是應(yīng)用JavaScript中Date對(duì)象的api和經(jīng)典的flex布局,而最長(zhǎng)連續(xù)打卡是常規(guī)算法最長(zhǎng)連續(xù)子序列的實(shí)現(xiàn)方式之一。
到此這篇關(guān)于Vue實(shí)現(xiàn)萬(wàn)年日歷的示例詳解的文章就介紹到這了,更多相關(guān)Vue萬(wàn)年日歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue循環(huán)中多個(gè)input綁定指定v-model實(shí)例
這篇文章主要介紹了Vue循環(huán)中多個(gè)input綁定指定v-model實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
解決vue中虛擬dom,無(wú)法實(shí)時(shí)更新的問(wèn)題
今天小編就為大家分享一篇解決vue中虛擬dom,無(wú)法實(shí)時(shí)更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
element滾動(dòng)條組件el-scrollbar的使用詳解
本文主要介紹了element滾動(dòng)條組件el-scrollbar的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
vue項(xiàng)目打包之開(kāi)發(fā)環(huán)境和部署環(huán)境的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目打包之開(kāi)發(fā)環(huán)境和部署環(huán)境的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue如何實(shí)現(xiàn)u-form多個(gè)form表單同時(shí)校驗(yàn)
在 Vue 項(xiàng)目中使用 UView UI 的 u-form 組件時(shí),多個(gè)表單同時(shí)校驗(yàn)的需求非常常見(jiàn),本文主要介紹了如何使用 u-form 組件實(shí)現(xiàn)多個(gè)表單同時(shí)校驗(yàn),需要的可以參考一下2025-01-01

