Vue+Vant實(shí)現(xiàn)7天日歷展示并在切換日期時(shí)實(shí)時(shí)變換功能
效果圖:


主要使用 moment.js 插件完成
HTML部分
<div class="day-content">
<div class="day-content-t">
<div>{{ monthVal }}</div>
<div @click="onCalendar()">更多>></div>
</div>
<div class="day-content-b">
<div class="day-week">
<div v-for="item in weekList">{{ item }}</div>
</div>
<div class="day-list">
<div v-for="item in dayList" :class="{ 'active' : todayVal === item }">{{ item }}</div>
</div>
</div>
</div>
<!-- 日歷 -->
<van-calendar v-model="calendarShow" :show-confirm="false" @confirm="onConfirm" color="#007AFF" />js部分
<script>
import moment from 'moment';
export default {
components: {},
data() {
return {
weekList: [],
dayList: [],
monthVal: '',
todayVal: '',
calendarShow: false,
}
},
created() {
this.monthVal = moment().format('YYYY年MM月');
let month = moment().format('YYYY-MM-DD');
this.todayVal = moment().format('DD');
this.dayInit(month,this.todayVal);
},
methods: {
dayInit(month,day){
this.weekList = [];
this.dayList = [];
// 初始化周
const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
const todayWeek = weekDays[moment(month).day()];
const frontWeek1 = weekDays[moment(month).subtract(3, 'days').day()];
const frontWeek2 = weekDays[moment(month).subtract(2, 'days').day()];
const frontWeek3 = weekDays[moment(month).subtract(1, 'days').day()];
const afterWeek1 = weekDays[moment(month).add(1, 'days').day()];
const afterWeek2 = weekDays[moment(month).add(2, 'days').day()];
const afterWeek3 = weekDays[moment(month).add(3, 'days').day()];
this.weekList.push(frontWeek1,frontWeek2,frontWeek3,todayWeek,afterWeek1,afterWeek2,afterWeek3);
// 初始化日期
const today = day;
const frontDay1 = moment(month).subtract(3, 'days').format('DD');
const frontDay2 = moment(month).subtract(2, 'days').format('DD');
const frontDay3 = moment(month).subtract(1, 'days').format('DD');
const afterDay1 = moment(month).add(1, 'days').format('DD');
const afterDay2 = moment(month).add(2, 'days').format('DD');
const afterDay3 = moment(month).add(3, 'days').format('DD');
this.dayList.push(frontDay1,frontDay2,frontDay3,today,afterDay1,afterDay2,afterDay3);
},
onCalendar(){
this.calendarShow = true;
},
formatDate(date) {
this.monthVal = `${date.getFullYear()}年${date.getMonth() + 1}月`;
let month = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
this.todayVal = date.getDate();
this.dayInit(month,this.todayVal);
},
onConfirm(date){
this.calendarShow = false;
this.formatDate(date)
},
}
}
</script>css部分
<style lang="scss" scoped>
.day-content{
background-color: #007AFF;
padding: 0.3rem;
color: #fff;
.day-content-t{
display: flex;
flex-direction: row;
justify-content: space-between;
div:nth-child(1){
font-size: 16px;
}
div:nth-child(2){
font-size: 12px;
}
}
.day-content-b{
.day-week,.day-list{
display: flex;
flex-direction: row;
justify-content: space-between;
margin: 0.4rem 0;
div{
text-align: center;
width: 0.6rem;
height: 0.6rem;
line-height: 0.6rem;
}
}
.active{
background-color: #0054B0;
border-radius: 50%;
}
}
}
</style>至此完成!??!
測試有效!?。「兄x支持?。?!
到此這篇關(guān)于Vue+Vant實(shí)現(xiàn)7天日歷展示并在切換日期時(shí)實(shí)時(shí)變換的文章就介紹到這了,更多相關(guān)vue vant切換日期時(shí)實(shí)時(shí)變換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0 watch里面的 deep和immediate用法說明
這篇文章主要介紹了vue2.0 watch里面的 deep和immediate用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前元素以外的地方隱藏當(dāng)前元素(實(shí)現(xiàn)思路)
這篇文章主要介紹了Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前元素以外的地方隱藏當(dāng)前元素,文中給大家擴(kuò)展了vue實(shí)現(xiàn)點(diǎn)擊其他地方隱藏div的三種方法,需要的朋友可以參考下2019-12-12
基于element-ui對話框el-dialog初始化的校驗(yàn)問題解決
這篇文章主要介紹了基于element-ui對話框el-dialog初始化的校驗(yàn)問題解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue內(nèi)嵌iframe跨域通信的實(shí)例代碼
這篇文章主要介紹了vue內(nèi)嵌iframe跨域通信,主要介紹了Vue組件中如何引入iframe,vue如何獲取iframe對象以及iframe內(nèi)的window對象,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì)需要的朋友可以參考下2022-11-11
vue3中調(diào)用api接口實(shí)現(xiàn)數(shù)據(jù)的渲染以及詳情方式
這篇文章主要介紹了vue3中調(diào)用api接口實(shí)現(xiàn)數(shù)據(jù)的渲染以及詳情方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
基于Vue框架vux組件庫實(shí)現(xiàn)上拉刷新功能
這篇文章主要為大家詳細(xì)介紹了基于Vue框架vux組件庫實(shí)現(xiàn)上拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

