vue實(shí)現(xiàn)日歷組件
基于VUE實(shí)現(xiàn)日歷組件,供大家參考,具體內(nèi)容如下

年和月份是使用輸入框來(lái)切換的,沒(méi)有做成選擇框,??和??切換月份,紅色選取是選取的日期
實(shí)現(xiàn)思路和網(wǎng)上的大多數(shù)一樣,首先是把月份的天數(shù)存進(jìn)一個(gè)數(shù)組,
monthDay:[31,'',31,30,31,30,31,31,30,31,30,31],
由于二月的天數(shù)是不確定的,所以就先設(shè)置為空
然后去求選擇的月份的第一天是星期幾,通過(guò) Date.getDay()函數(shù),這個(gè)函數(shù)有一個(gè)注意事項(xiàng),就是如果是星期天,他會(huì)返回0,這需要我們自己去處理一下
圖中,2019年1月1號(hào)是星期二,所以前面就要給它一個(gè)空格塊
下面代碼中第一層循環(huán)就是在循環(huán)空格塊,spaceDay表示需要幾個(gè)空格
<p v-for="item in spaceDay" :key="item.id"></p>
<p v-for="(item,idx) in (monthDay[this.month-1] || 30)" @click="setDay(idx)" :class="idx==activeDay?'active':''" :key="item.id">{{item}}</p>下面貼出完整代碼
<template>
? ? <div id="calender">
? ? ? ? <div class="txt-c p-10">
? ? ? ? ? ? <span @click="prev"> ?? </span>
? ? ? ? ? ? <input type="text" v-model="year">年
? ? ? ? ? ? <input type="text" v-model="month" class="month">月
? ? ? ? ? ? <span @click="next"> ????? </span>
? ? ? ? </div>
? ? ? ? <div class="weekDay flex jc-sb p-5 day" >
? ? ? ? ? ? <p v-for="item in weekList" :key="item.id">{{item}}</p>
? ? ? ? </div>
? ? ? ? <div class="weekDay flex p-5 day">
? ? ? ? ? ? <p v-for="item in spaceDay" :key="item.id"></p>
? ? ? ? ? ? <p v-for="(item,idx) in (monthDay[this.month-1] || 30)" @click="setDay(idx)" :class="idx==activeDay?'active':''" :key="item.id">{{item}}</p>
? ? ? ? </div>
? ? </div>
</template>
<script>
? ? export default {
? ? ? ? name: "calender",
? ? ? ? data(){
? ? ? ? ? ? return{
? ? ? ? ? ? ? ? year: '', // 年份
? ? ? ? ? ? ? ? month: '', // 月份
? ? ? ? ? ? ? ? day: '', // 天數(shù)
? ? ? ? ? ? ? ? current: '', // 當(dāng)前時(shí)間
? ? ? ? ? ? ? ? weekList:['周一','周二','周三','周四','周五','周六','周日'],
? ? ? ? ? ? ? ? monthDay:[31,'',31,30,31,30,31,31,30,31,30,31],
? ? ? ? ? ? ? ? activeDay: '', // 選中的日期
? ? ? ? ? ? ? ? spaceDay: '', // 每個(gè)月第一天是星期幾
? ? ? ? ? ? ? ? February:'' // 判斷2月份的天數(shù)
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? created(){
? ? ? ? ? ? this.current = new Date()
? ? ? ? ? ? this.getTheCurrentDate()
? ? ? ? ? ? this.getMonthFisrtDay()
? ? ? ? ? ? this.February = this.isLeapYear(this.year) ? 28 : 29
? ? ? ? ? ? this.monthDay.splice(1,1,this.February)
? ? ? ? },
? ? ? ? watch:{
? ? ? ? ? month(){
? ? ? ? ? ? ? if(this.month>12||this.month<1){
? ? ? ? ? ? ? ? ? console.log('請(qǐng)輸入正確月份')
? ? ? ? ? ? ? ? ? return
? ? ? ? ? ? ? }
? ? ? ? ? ? ? this.getMonthFisrtDay()
? ? ? ? ? },
? ? ? ? ? year(){
? ? ? ? ? ? ? this.getMonthFisrtDay()
? ? ? ? ? }
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? // 判斷是否是閏年
? ? ? ? ? ? isLeapYear(year){
? ? ? ? ? ??
? ? ? ? ? ? ? ? return year % 4 == 0 && year % 100 != 0 ||year % 400 == 0;
? ? ? ? ? ? },
? ? ? ? ? ? // 選取特定天數(shù)
? ? ? ? ? ? setDay(idx){
? ? ? ? ? ? ? ? this.activeDay = idx
? ? ? ? ? ? ? ? this.day = idx + 1
? ? ? ? ? ? ? ? console.log('選擇的日期是'+this.year+' '+this.month+' '+this.day)
? ? ? ? ? ? },
? ? ? ? ? ? // 判斷月份的第一天是星期幾
? ? ? ? ? ? getMonthFisrtDay(){
? ? ? ? ? ? ? ? var firstDayOfCurrentMonth = new Date(this.year,this.month-1,1) // 某年某月的第一天
? ? ? ? ? ? ? ? if(firstDayOfCurrentMonth.getDay() == 0){
? ? ? ? ? ? ? ? ? ? this.spaceDay = 6
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? this.spaceDay = firstDayOfCurrentMonth.getDay() - 1
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? // 獲取當(dāng)前的日期
? ? ? ? ? ? getTheCurrentDate(){
? ? ? ? ? ? ? ? this.year = this.current.getFullYear()
? ? ? ? ? ? ? ? this.month = this.current.getMonth() + 1
? ? ? ? ? ? ? ? this.day = this.current.getDate()
? ? ? ? ? ? },
? ? ? ? ? ? prev(){
? ? ? ? ? ? ? ? if(this.month==1){
? ? ? ? ? ? ? ? ? ? this.year--
? ? ? ? ? ? ? ? ? ? this.month=12
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? this.month--
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.activeDay = 0
? ? ? ? ? ? ? ? this.getMonthFisrtDay()
? ? ? ? ? ? },
? ? ? ? ? ? next(){
? ? ? ? ? ? ? ? if(this.month==12){
? ? ? ? ? ? ? ? ? ? this.year++
? ? ? ? ? ? ? ? ? ? this.month=1
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? this.month++
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.activeDay = 0
? ? ? ? ? ? ? ? this.getMonthFisrtDay()
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
<style lang="scss" scoped>
#calender{
? ? .txt-c{
? ? ? ? text-align: center;
? ? }
? ? .p-10{
? ? ? ? padding: 2rem;
? ? }
? ? .p-5{
? ? ? ? padding: 1rem;
? ? }
? ? .flex {
? ? ? ? display: flex;
? ? }
? ? .jc-sb{
? ? ? ? justify-content: space-between;
? ? }
? ? input{
? ? ? ? width: 50px;
? ? ? ? &.month{
? ? ? ? ? ? width: 30px;
? ? ? ? }
? ? }
? ? .day{
? ? ? ? flex-wrap: wrap;
? ? ? ? p{
? ? ? ? ? ? width: 14.28%;
? ? ? ? ? ? /*flex: 0 0 0 ;*/
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? line-height: 2.4rem;
? ? ? ? ? ? height: 2.4rem;
? ? ? ? ? ? position: relative;
? ? ? ? ? ? z-index: 100;
? ? ? ? ? ? &.active{
? ? ? ? ? ? ? ? color: #fff;
? ? ? ? ? ? }
? ? ? ? ? ? &.active:before{
? ? ? ? ? ? ? ? content: '';
? ? ? ? ? ? ? ? height: 2.5rem;
? ? ? ? ? ? ? ? width: 2.5rem;
? ? ? ? ? ? ? ? position: absolute;
? ? ? ? ? ? ? ? z-index: -1;
? ? ? ? ? ? ? ? left: 0;
? ? ? ? ? ? ? ? top: 0;
? ? ? ? ? ? ? ? transform: translateX(50%);
? ? ? ? ? ? ? ? border-radius: 50%;
? ? ? ? ? ? ? ? background: #e97163;
? ? ? ? ? ? ? ? color: #fff;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
</style>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui中select下拉框加載大數(shù)據(jù)渲染優(yōu)化方式
這篇文章主要介紹了element-ui中select下拉框加載大數(shù)據(jù)渲染優(yōu)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue iview組件表格 render函數(shù)的使用方法詳解
下面小編就為大家分享一篇vue iview組件表格 render函數(shù)的使用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue如何給組件添加點(diǎn)擊事件?@click.native
這篇文章主要介紹了Vue如何給組件添加點(diǎn)擊事件?@click.native,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
幾個(gè)你不知道的技巧助你寫(xiě)出更優(yōu)雅的vue.js代碼
本文參考自油管上某個(gè)國(guó)外大神的公開(kāi)演講視頻,學(xué)習(xí)了一下覺(jué)得很不錯(cuò),所以在項(xiàng)目中也使用了這些不錯(cuò)的技巧。趁周末有空,寫(xiě)個(gè)博客記錄一下2018-06-06
Vue中import與@import的區(qū)別及使用場(chǎng)景說(shuō)明
這篇文章主要介紹了Vue中import與@import的區(qū)別及使用場(chǎng)景說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的
這篇文章主要介紹了Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式(2)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue3的父?jìng)髯訂?wèn)題(單項(xiàng)數(shù)據(jù)流)
這篇文章主要介紹了vue3的父?jìng)髯訂?wèn)題(單項(xiàng)數(shù)據(jù)流),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
keep-alive include和exclude無(wú)效問(wèn)題及解決
這篇文章主要介紹了keep-alive include和exclude無(wú)效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue中使用swiper,左右箭頭點(diǎn)擊沒(méi)有效果問(wèn)題及解決
這篇文章主要介紹了vue中使用swiper,左右箭頭點(diǎn)擊沒(méi)有效果問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

