vue實(shí)現(xiàn)自定義日期組件功能的實(shí)例代碼
實(shí)現(xiàn)一個(gè)日期組件,如圖:
components.js代碼如下:
Vue.component('sc-calendar',{ template:'<div class="scCalendar">' + '<div class="calendar_header">' + '<div class="prev" @click="prevMonth"> < </div>' + '<span class="text_year">{{currentYear}}年</span>' + '<span class="text_month">{{currentMonth}}月</span>' + '<div class="next" @click="nextMonth"> > </div>' + '</div>' + '<div class="calendar_content">' + '<ul class="week">' + '<li v-for="item in weeks">{{item}}</li>' + '</ul>' + '<ul class="day">' + '<li v-for="item in dayList" :class="{marginRight0:item.marginRight0}">{{item.text}}</li>' + '</ul>' + '</div>' + '</div>', data:function(){ return { weeks: ['日', '一', '二', '三', '四', '五', '六'], dayList:[], currentYear:'', currentMonth: '' } }, created:function(){ var date=new Date; this.currentYear = date.getFullYear(); this.currentMonth = date.getMonth()+1; this.calDay(this.currentYear, this.currentMonth); }, methods:{ //計(jì)算指定月份的天數(shù) calDay:function(year,month){ var oDate = new Date(); //setFullYear(year,month,day) 方法用于設(shè)置年份,返回調(diào)整過(guò)的日期的毫秒表示。 oDate.setFullYear(year, month-1,1); oDate.setDate(1);//設(shè)置一個(gè)月中的第一天 var oNow = oDate.getDay();//當(dāng)前月的第一天是星期幾 var dayNum = 0; //指定月份的天數(shù) if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){ dayNum = 31; }else if(month==4 || month==6 || month==9 || month==11){ dayNum = 30; }else if(month==2&&this.isLeaYear(year)){ dayNum = 29; }else{ dayNum = 28; } var SumDayLiNum = 0;//總共的格子數(shù) var lastNum = (dayNum-(7-oNow))%7; //最后剩余的數(shù) lastNum = lastNum == 0?0:7; SumDayLiNum = 7 + parseInt((dayNum-(7-oNow))/7)*7+lastNum; this.showDayList(dayNum,SumDayLiNum,oNow); }, //判斷是否是閏年 isLeaYear:function(year){ if(year%4==0&&year%100!=0){ return true; }else{ if(year%400==0){ return true; }else{ return false; } } }, //顯示當(dāng)前日歷內(nèi)容 showDayList:function(dayNum,SumDayLiNum,oNow){ this.dayList = []; var rows = parseInt(SumDayLiNum/7); var cols = 7; for(var i=0;i<rows;i++){ for(var j=0;j<cols ;j++){ if(j == cols-1){ this.dayList.push({ text:'', marginRight0:true }) }else{ this.dayList.push({ text:'', marginRight0:false }) } } } for(var z=1;z<=dayNum;z++){ this.dayList[oNow].text = z; oNow++; } }, prevMonth:function(){ if( this.currentMonth == 1){ this.currentYear = this.currentYear - 1; this.currentMonth = 12; }else{ this.currentMonth = this.currentMonth - 1; } this.calDay(this.currentYear, this.currentMonth); }, nextMonth:function(){ if( this.currentMonth == 12){ this.currentYear = this.currentYear + 1; this.currentMonth = 1; }else{ this.currentMonth = this.currentMonth + 1; } this.calDay(this.currentYear, this.currentMonth); } } });
ccal.css代碼:
html { font-family: '微軟雅黑'; } body, div, span, img, ul, li, p { margin: 0; padding: 0; } ul, li { list-style: none; } .commonprev { width: 0.46666667rem; height: 0.93333333rem; color: #ffffff; position: absolute; display: inline-block; } .commonyear { width: 5.46666667rem; height: 1.6rem; font-size: 1rem; color: #ffffff; position: absolute; } .scCalendar { width: 25rem; height: 21.66666667rem; background: #005498; background-size: 100%; } .scCalendar .calendar_header { height: 2.93333333rem; width: 100%; position: relative; line-height: 2.93333333rem; } .scCalendar .calendar_header .prev { width: 0.46666667rem; height: 0.93333333rem; color: #ffffff; position: absolute; display: inline-block; left: 2.76666667rem; } .scCalendar .calendar_header .next { width: 0.46666667rem; height: 0.93333333rem; color: #ffffff; position: absolute; display: inline-block; right: 2.76666667rem; } .scCalendar .calendar_header .text_year { width: 5.46666667rem; height: 1.6rem; font-size: 1rem; color: #ffffff; position: absolute; left: 9.76666667rem; } .scCalendar .calendar_header .text_month { width: 5.46666667rem; height: 1.6rem; font-size: 1rem; color: #ffffff; position: absolute; left: 13.1rem; } .scCalendar .calendar_content { padding: 0 1rem; } .scCalendar .calendar_content li { width: 2rem; height: 2rem; line-height: 2rem; margin-right: 1.5rem; text-align: center; margin-bottom: 0.66666667rem; float: left; color: white; font-size: 0.93333333rem; } .scCalendar .calendar_content .week li:nth-of-type(7) { margin-right: 0; } .scCalendar .calendar_content .day .marginRight0 { margin-right: 0; }
index.html代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="./ccal.css" rel="external nofollow" > <script> var pixRatio = 1/window.devicePixelRatio; //像素比 var html = document.documentElement; document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale='+ pixRatio +',minimum-scale='+ pixRatio +',maximum-scale='+ pixRatio +'">'); html.style.fontSize = html.clientWidth/25 + 'px'; </script> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <script src="./components.js"></script> </head> <body> <div id="app"> <div class="calendar"> <sc-calendar></sc-calendar> </div> </div> </body> <script> var vm = new Vue({ el:'#app' }) </script> </html>
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)自定義日期組件功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Vue日期時(shí)間選擇器組件使用方法詳解
- vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼
- vue使用自定義事件的表單輸入組件用法詳解【日期組件與貨幣組件】
- vue實(shí)現(xiàn)移動(dòng)端輕量日期組件不依賴第三方庫(kù)的方法
- 基于Vue組件化的日期聯(lián)動(dòng)選擇器功能的實(shí)現(xiàn)代碼
- Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼
- 詳解vue移動(dòng)端日期選擇組件
- 基于vue2.0+vuex的日期選擇組件功能實(shí)現(xiàn)
- vue日期組件 支持vue1.0和2.0
- vue左右滑動(dòng)選擇日期組件封裝的方法
相關(guān)文章
在Vue中使用防抖與節(jié)流,及this指向的問(wèn)題
這篇文章主要介紹了在Vue中使用防抖與節(jié)流,及this指向的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vue動(dòng)態(tài)循環(huán)出的多個(gè)select出現(xiàn)過(guò)的變?yōu)閐isabled(實(shí)例代碼)
本文通過(guò)實(shí)例代碼給大家分享了vue動(dòng)態(tài)循環(huán)出的多個(gè)select出現(xiàn)過(guò)的變?yōu)閐isabled效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-11-11Vue3使用v-html實(shí)現(xiàn)文本關(guān)鍵詞變色處理
遍歷文本內(nèi)容,找到關(guān)鍵詞,并使用某種方法更改其字體樣式,經(jīng)過(guò)搜尋資料決定采用v-html實(shí)現(xiàn),但是v-html本身并不安全,有安全性風(fēng)險(xiǎn)且影響性能,所以謹(jǐn)慎使用,本文給大家介紹了Vue3使用v-html實(shí)現(xiàn)文本關(guān)鍵詞變色處理,需要的朋友可以參考下2024-06-06VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯(cuò)位問(wèn)題及解決方法
這篇文章主要介紹了VUE2.0自定義指令與v-if沖突導(dǎo)致元素屬性修改錯(cuò)位問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07探秘Vue異步更新機(jī)制中nextTick的原理與實(shí)現(xiàn)
nextTick?是?Vue?提供的一個(gè)重要工具,它的作用主要體現(xiàn)在幫助我們更好地處理異步操作,下面就跟隨小編一起來(lái)探索一下nextTick的原理與實(shí)現(xiàn)吧2024-02-02Vue中以HTML形式顯示內(nèi)容并動(dòng)態(tài)生成HTML代碼的方法
有的時(shí)候我們想在vue中直接顯示一個(gè)html的網(wǎng)頁(yè),如果用富文本方式,那么內(nèi)容就會(huì)太多,那么怎么處理呢?這篇文章主要給大家介紹了關(guān)于Vue中如何以HTML形式顯示內(nèi)容并動(dòng)態(tài)生成HTML代碼的相關(guān)資料,需要的朋友可以參考下2024-03-03