基于Vue實(shí)現(xiàn)支持按周切換的日歷
基于Vue的日歷小功能,可根據(jù)實(shí)際開(kāi)發(fā)情況按每年、每月、每周、進(jìn)行切換,具體內(nèi)容如下
<template> <div class="date"> <!-- 年份 月份 --> <div class="month"> <p>{{ currentYear }}年{{ currentMonth }}月</p> </div> <!-- 星期 --> <ul class="weekdays"> <li>一</li> <li>二</li> <li>三</li> <li>四</li> <li>五</li> <li>六</li> <li>日</li> </ul> <!-- 日期 --> <ul class="days"> <li @click="pick(day)" v-for="(day, index) in days" :key="index"> <!--本月--> <span v-if="day.getMonth()+1 != currentMonth" class="other-month">{{ day.getDate() }}</span> <span v-else> <!--今天--> <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="active">{{ day.getDate() }}</span> <span v-else>{{ day.getDate() }}</span> </span> </li> </ul> </div> </template>
js部分:目前默認(rèn)顯示一周,可根據(jù)實(shí)際情況更改
<script> export default { name: 'date', data () { return { currentYear: 1970, // 年份 currentMonth: 1, // 月份 currentDay: 1, // 日期 currentWeek: 1, // 星期 days: [], } }, mounted () { }, created () { this.initData(null) }, methods: { formatDate (year, month, day) { const y = year let m = month if (m < 10) m = `0${m}` let d = day if (d < 10) d = `0$ublnpf9mb` return `${y}-${m}-$ublnpf9mb` }, initData (cur) { let date = '' if (cur) { date = new Date(cur) } else { date = new Date() } this.currentDay = date.getDate() // 今日日期 幾號(hào) this.currentYear = date.getFullYear() // 當(dāng)前年份 this.currentMonth = date.getMonth() + 1 // 當(dāng)前月份 this.currentWeek = date.getDay() // 1...6,0 // 星期幾 if (this.currentWeek === 0) { this.currentWeek = 7 } const str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay)// 今日日期 年-月-日 this.days.length = 0 // 今天是周日,放在第一行第7個(gè)位置,前面6個(gè) 這里默認(rèn)顯示一周,如果需要顯示一個(gè)月,則第二個(gè)循環(huán)為 i<= 35- this.currentWeek /* eslint-disabled */ for (let i = this.currentWeek - 1; i >= 0; i -= 1) { const d = new Date(str) d.setDate(d.getDate() - i) // console.log(y:" + d.getDate()) this.days.push(d) } for (let i = 1; i <= 7 - this.currentWeek; i += 1) { const d = new Date(str) d.setDate(d.getDate() + i) this.days.push(d) } }, // 上個(gè)星期 weekPre () { const d = this.days[0] // 如果當(dāng)期日期是7號(hào)或者小于7號(hào) d.setDate(d.getDate() - 7) this.initData(d) }, // 下個(gè)星期 weekNext () { const d = this.days[6] // 如果當(dāng)期日期是7號(hào)或者小于7號(hào) d.setDate(d.getDate() + 7) this.initData(d) }, // 上一個(gè)月 傳入當(dāng)前年份和月份 pickPre (year, month) { const d = new Date(this.formatDate(year, month, 1)) d.setDate(0) this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1)) }, // 下一個(gè)月 傳入當(dāng)前年份和月份 pickNext (year, month) { const d = new Date(this.formatDate(year, month, 1)) d.setDate(35) this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1)) }, // 當(dāng)前選擇日期 pick (date) { alert(this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate())) }, }, } </script>
<style lang="scss"> @import "~base"; .date { height: px2rem(180); color: #333; .month { font-size: px2rem(24); text-align: center; margin-top: px2rem(20); } .weekdays { display: flex; font-size: px2rem(28); margin-top: px2rem(20); li { flex: 1; text-align: center; } } .days { display: flex; li { flex: 1; font-size: px2rem(30); text-align: center; margin-top: px2rem(10); line-height: px2rem(60); .active { display: inline-block; width: px2rem(60); height: px2rem(60); color: #fff; border-radius: 50%; background-color: #fa6854; } .other-month { color: #e4393c; } } } } </style>
相關(guān)參考鏈接:Vue.js創(chuàng)建Calendar日歷效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- VUE實(shí)現(xiàn)日歷組件功能
- vue實(shí)現(xiàn)一個(gè)炫酷的日歷組件
- Vue.js創(chuàng)建Calendar日歷效果
- 基于Vue2-Calendar改進(jìn)的日歷組件(含中文使用說(shuō)明)
- vue實(shí)現(xiàn)簡(jiǎn)單的日歷效果
- Vue實(shí)現(xiàn)日歷小插件
- Vue編寫(xiě)可顯示周和月模式的日歷 Vue自定義日歷內(nèi)容的顯示
- vue+elementUI實(shí)現(xiàn)簡(jiǎn)單日歷功能
- vue實(shí)現(xiàn)日歷備忘錄功能
- 使用Vue實(shí)現(xiàn)簡(jiǎn)單日歷效果
相關(guān)文章
Vue中 v-if 和v-else-if頁(yè)面加載出現(xiàn)閃現(xiàn)的問(wèn)題及解決方法
vue中v-if 和v-else-if在頁(yè)面加載的時(shí)候,不滿足條件的標(biāo)簽會(huì)加載然后再消失掉,如果要解決這個(gè)問(wèn)題,下面小編給大家?guī)?lái)了實(shí)例代碼,需要的朋友參考下吧2018-10-10Vue3.0利用vue-grid-layout插件實(shí)現(xiàn)拖拽布局
這篇文章主要介紹了Vue3.0利用vue-grid-layout插件實(shí)現(xiàn)拖拽布局,工作中難免遇到需要對(duì)頁(yè)面布局進(jìn)行拖拽然后改變布局,保存布局,下面文章就圍繞Vue3.0利用vue-grid-layout插件實(shí)現(xiàn)拖拽布局的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-11-11vue中keep-alive內(nèi)置組件緩存的實(shí)例代碼
這篇文章主要介紹了vue中的keep-alive內(nèi)置組件緩存,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04vue如何將字符串的一部分處理為html文檔并渲染到頁(yè)面
這篇文章主要介紹了vue如何將字符串的一部分處理為html文檔并渲染到頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue項(xiàng)目element UI input框掃碼槍掃描過(guò)快出現(xiàn)數(shù)據(jù)丟失問(wèn)題及解決方案
這篇文章主要介紹了vue項(xiàng)目element UI input框掃碼槍掃描過(guò)快出現(xiàn)數(shù)據(jù)丟失問(wèn)題,輸入框要掉兩個(gè)接口,根據(jù)第一個(gè)驗(yàn)證接口返回的code,彈不同的框,點(diǎn)擊彈框確認(rèn)再掉第二個(gè)接口,需要的朋友可以參考下2022-12-12vue項(xiàng)目中銷(xiāo)毀window.addEventListener事件監(jiān)聽(tīng)解析
這篇文章主要介紹了vue項(xiàng)目中銷(xiāo)毀window.addEventListener事件監(jiān)聽(tīng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Vue.js Ajax動(dòng)態(tài)參數(shù)與列表顯示實(shí)現(xiàn)方法
Vue.js是一個(gè)輕巧、高性能、可組件化的MVVM庫(kù),同時(shí)擁有非常容易上手的API。下面通過(guò)本文給大家介紹vue.js ajax動(dòng)態(tài)參數(shù)與列表顯示實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2016-10-10