




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第vue的h5日歷組件實現詳解本文實例為大家分享了vue的h5日歷組件實現代碼,供大家參考,具體內容如下
日歷樣式自定義
日歷組件
template
section
div
div
li@click="PreMonth(myDate,false)"
div/div
/li
li{{dateTop}}/li
li@click="NextMonth(myDate,false)"
div/div
/li
/div
div
divv-for="(tag,index)intextTop":key="index"
div{{tag}}/div
/div
/div
div
div
v-for="(item,index)inlist"
@click="clickDay(item,index)"
:key="index"
!--div
:class="item.isToday'wh_isToday':item.isPreDay'wh_chose_day':item.isChosedDay'wh_chose_day':''"
{{item.id}}/div--
div
v-bind:class="[{wh_isMark:item.isMark},
{wh_other_dayhide:item.otherMonth!=='nowMonth'},
{wh_want_dayhide:item.dayHide},
{wh_isToday:item.isToday},
{wh_chose_day:item.chooseDay},setClass(item)]"
{{item.id}}/div
/div
/div
/div
/section
/template
script
importmomentfrom"moment";
importtimeUtilfrom"./calendar";
importVuefrom"vue";
exportdefault{
data(){
return{
myDate:[],
list:[],
historyChose:[],
dateTop:"",
};
props:{
rangeDate:{
type:Array,
default:()=[],
},
markDate:{
type:Array,
default:()=[],
},
markDateMore:{
type:Array,
default:()=[],
},
textTop:{
type:Array,
default:()=["一","二","三","四","五","六","日"],
},
sundayStart:{
type:Boolean,
default:()=false,
},
agoDayHide:{
type:String,
default:`0`,
},
futureDayHide:{
type:String,
default:`2554387200`,
},
created(){
Start();
//獲取今日的日期
varcurDate=newDate();
varpreDate=Date.parse(newDate(curDate.getTime()-24*60*60*1000));//前一天
this.myDate=newDate(preDate);
console.log(this.rangeDate);
methods:{
intStart(){
timeUtil.sundayStart=this.sundayStart;
},
setClass(data){
//console.log('data',data)
letobj={};
obj[data.markClassName]=data.markClassName;
returnobj;
},
//點擊選擇的日期
clickDay:function(item,index){
console.log("in","kkkkkk",item);
if(item.otherMonth==="nowMonth"!item.dayHide){
console.log("in","kkkkkk");
this.getList(this.myDate,item.date);
}
if(item.otherMonth!=="nowMonth"){
item.otherMonth==="preMonth"
this.PreMonth(item.date)
:this.NextMonth(item.date);
}
},
//選擇月份
ChoseMonth:function(date,isChosedDay=true){
date=timeUtil.dateFormat(date);
this.myDate=newDate(date);
this.$emit("changeMonth",timeUtil.dateFormat(this.myDate));
if(isChosedDay){
this.getList(this.myDate,date,isChosedDay);
}else{
this.getList(this.myDate);
}
},
//上一個月的切換
PreMonth:function(date,isChosedDay=true){
date=timeUtil.dateFormat(date);
this.myDate=timeUtil.getOtherMonth(this.myDate,"preMonth");
this.$emit("changeMonth",timeUtil.dateFormat(this.myDate));
if(isChosedDay){
this.getList(this.myDate,date,isChosedDay);
}else{
this.getList(this.myDate);
}
},
//下一個月的切換
NextMonth:function(date,isChosedDay=true){
date=timeUtil.dateFormat(date);
this.myDate=timeUtil.getOtherMonth(this.myDate,"nextMonth");
this.$emit("changeMonth",timeUtil.dateFormat(this.myDate));
if(isChosedDay){
this.getList(this.myDate,date,isChosedDay);
}else{
this.getList(this.myDate);
}
},
//數據格式化的處理
forMatArgs:function(){
letmarkDate=this.markDate;
letmarkDateMore=this.markDateMore;
letrangeDate=this.rangeDate;
markDate=markDate.map((k)={
returntimeUtil.dateFormat(k);
});
rangeDate=rangeDate.map((k)={
returntimeUtil.dateFormat(k);
});
return[markDate,markDateMore,rangeDate];
},
//日期表格的的樣式初始化
getList:function(date,chooseDay,isChosedDay=true){
console.log(date,chooseDay,"listCanshu",this.rangeDate);
const[markDate,markDateMore,rangeDate]=this.forMatArgs();//標簽
this.dateTop=`${date.getFullYear()}年${date.getMonth()+1}月`;//頂部的頭
letarr=timeUtil.getMonthList(this.myDate);//獲取當前日期的整個月份
for(leti=0;iarr.length;i++){
letmarkClassName="";
letk=arr[i];
k.chooseDay=false;
constnowTime=k.date;//當前遍歷的哪個時間
constt=newDate(nowTime).getTime()/1000;
//看每一天的class
for(constcofmarkDateMore){
if(c.date===nowTime){
markClassName=c.className||"";
}
}
//標記選中某些天設置class
k.markClassName=markClassName;
k.isMark=markDate.indexOf(nowTime)
if(this.rangeDate){
k.isMark=rangeDate.indexOf(nowTime)
}
//無法選中某天
k.dayHide=tthis.agoDayHide||tthis.futureDayHide;
if(k.isToday){
this.$emit("isToday",nowTime);
}
//if(this.rangeDate.length){
//
if(timeUtil.dateFormat(moment(this.rangeDate[0]).format("YYYY-MM-DD"))===nowTime||timeUtil.dateFormat(moment(this.rangeDate[6]).format("YYYY-MM-DD"))===nowTime){
//
k.chooseDay=true;
//
}else{
//
k.chooseDay=
false;
//
}
//}
varcurDate=newDate();
varpreDate=Date.parse(
newDate(curDate.getTime()-24*60*60*1000)
);//前一天
constpreDay=timeUtil.dateFormat(
moment(preDate).format("YYYY-MM-DD")
);
//處理默認當月的的前一天是被選中
if(nowTime===preDay!chooseDay!this.rangeDate.length){
k.chooseDay=true;
}else{
k.chooseDay=false;
}
letflag=!k.dayHidek.otherMonth==="nowMonth";
if(chooseDaychooseDay===nowTimeflag){
this.$emit("choseDay",nowTime);
this.historyChose.push(nowTime);
console.log(this.historyChose);
if(this.rangeDate.length){
k.chooseDay=false;
}else{
k.chooseDay=true;
}
}elseif(
this.historyChose[this.historyChose.length-1]===nowTime
!chooseDay
flag
){
console.log("進來這里了");
//處理日月的切換
if(this.rangeDate.length){
k.chooseDay=false;
}else{
if(this.chooseDay){
k.chooseDay=true;
}else{
k.chooseDay=false;
}
}
}
}
this.list=arr;
},
mounted(){
this.getList(this.myDate);
watch:{
rangeDate:{
handler(val,oldVal){
this.getList(this.myDate);
},
deep:true,
},
markDate:{
handler(val,oldVal){
this.getList(this.myDate);
},
deep:true,
},
markDateMore:{
handler(val,oldVal){
this.getList(this.myDate);
},
deep:true,
},
agoDayHide:{
handler(val,oldVal){
this.getList(this.myDate);
},
deep:true,
},
futureDayHide:{
handler(val,oldVal){
this.getList(this.myDate);
},
deep:true,
},
sundayStart:{
handler(val,oldVal){
Start();
this.getList(this.myDate);
},
deep:true,
},
/script
stylescoped
@mediascreenand(min-width:460px){
.wh_item_date:hover{
background:#00baff;
cursor:pointer;
margin:0;
padding:0;
.wh_container{
max-width:410px;
margin:auto;
list-style-type:none;
.wh_top_changge{
display:flex;
.wh_top_changgeli{
cursor:pointer;
display:flex;
color:#040b29;
font-size:18px;
flex:1;
justify-content:center;
align-items:center;
height:47px;
.wh_top_changge.wh_content_li{
cursor:auto;
flex:2.5;
.wh_content_all{
font-family:-apple-system,BlinkMacSystemFont,"PingFangSC",
"HelveticaNeue",STHeiti,"MicrosoftYahei",Tahoma,Simsun,sans-serif;
background-color:#ffffff;
width:100%;
overflow:hidden;
padding-bottom:8px;
border-radius:10px;
.wh_content{
display:flex;
flex-wrap:wrap;
padding:03%03%;
width:100%;
justify-content:center;
.wh_content:first-child.wh_content_item_tag,
.wh_content:first-child.wh_content_item{
color:#ddd;
font-size:16px;
.wh_content_item,
.wh_content_item_tag{
font-size:15px;
width:13.4%;
text-align:center;
color:#fff;
position:relative;
.wh_content_item{
height:40px;
.wh_top_tag{
width:40px;
height:40px;
line-height:40px;
margin:auto;
display:flex;
justify-content:center;
align-items:center;
color:#9b9da9;
.wh_item_date{
width:40px;
height:40px;
line-height:40px;
margin:auto;
display:flex;
justify-content:center;
align-items:center;
color:#040b29;
.wh_jiantou1{
width:12px;
height:12px;
border-top:2pxsolid#9b9da9;
border-left:2pxsolid#9b9da9;
transform:rotate(-45deg);
.wh_jiantou1:active,
.wh_jiantou2:active{
border-color:#040b29;
.wh_jiantou2{
width:12px;
height:12px;
border-top:2pxsolid#9b9da9;
border-right:2pxsolid#9b9da9;
transform:rotate(45deg);
.wh_content_item.wh_isMark{
margin:auto;
/*border-radius:10px;*/
background:rgba(235,246,255,1);
z-index:2;
.wh_content_item.wh_other_dayhide{
color:#bfbfbf;
.wh_content_item.wh_want_dayhide{
color:#9b9da9;
.wh_content_item.wh_isToday{
/*background:#00BAFF;
border-radius:10px;*/
color:rgba(130,183,225,1);
.wh_content_item.wh_pre_day{
color:red;
.wh_content_item.wh_chose_day{
background:rgba(168,208,240,1);
color:#fff;
border-radius:10px;
/style
calendar.js是生成月份盤,月數多少天的邏輯
importmomentfrom"moment";
exportdefault{
//當某月的天數
getDaysInOneMonth(date){
constyear=date.getFullYear();
constmonth=date.getMonth()+1;
constd=newDate(year,month,0);
returnd.getDate();
},
//向前空幾個
getMonthweek(date){
constyear=date.getFullYear();
constmonth=date.getMonth()+1;
constdateFirstOne=newDate(year+'/'+month+'/1');
returnthis.sundayStart
dateFirstOne.getDay()==07:dateFirstOne.getDay():
dateFirstOne.getDay()==06:dateFirstOne.getDay()-1;
},
/**
*獲取當前日期上個月或者下個月
*/
getOtherMonth(date,str='nextMonth'){
consttimeArray=this.dateFormat(date).split('/');
constyear=timeArray[0];
constmonth=timeArray[1];
constday=timeArray[2];
letyear2=year;
letmonth2;
if(str==='nextMonth'){
month2=parseInt(month)+1;
if(month2==13){
year2=parseInt(year2)+1;
month2=1;
}
}else{
month2=parseInt(month)-1;
if(month2==0){
year2=parseInt(year2)-1;
month2=12;
}
}
letday2=day;
constdays2=newDate(year2,month2,0).getDate();
if(day2days2){
day2=days2;
}
if(month210){
month2='0'+month2;
}
if(day210){
day2='0'+day2;
}
constt2=year2+'/'+month2+'/'+day2;
returnnewDate(t2);
},
//上個月末尾的一些日期
getLeftArr(date){
constarr=[];
constleftNum=this.getMonthweek(date);
constnum=this.getDaysInOneMonth(this.getOtherMonth(date,'preMonth'))-leftNum+1;
constpreDate=this.getOtherMonth(date,'preMonth');
//上個月多少開始
for(leti=0;ileftNum;i++){
constnowTime=preDate.getFullYear()+'/'+(preDate.getMonth()+1)+'/'+(num+i);
arr.push({
id:num+i,
date:nowTime,
isToday:false,
isPreDay:false,
otherMonth:'preMonth',
});
}
returnarr;
},
//下個月末尾的一些日期
getRightArr(date){
constarr=[];
constnextDate=this.getOtherMonth(date,'nextMonth');
constleftLength=this.getDaysInOneMonth(date)+this.getMonthweek(date);
const_length=7-leftLength%7;
for(leti=0;i_length;i++){
constnowTime=nextDate.getFullYear()+'/'+(nextDate.getMonth()+1)+'/'+(i+1);
arr.push({
id:i+1,
date:nowTime,
isToday:false,
isPreDay:false,
otherMonth:'nextMonth',
});
}
returnarr;
},
//format日期
dateFormat(date){
date=typeofdate==='string'newDate(date.replace(/-/g,'/')):date;
returndate.getFullYear()+'/'+(date.getMonth()+1)+'/'
+date.getDate();
},
//獲取某月的列表不包括上月和下月
getMonthListNoOther(date){
constarr=[];
constnum=this.getDaysInOneMonth(date);
constyear=date.getFullYear();
constmonth=date.getMonth()+1;
consttoDay=this.dateFormat(newDate());
console.log(toDay,'今日日期的格式化');
varcurDate=newDate();
varpreDate=Date.parse(newDate(curDate.getTime()-24*60*60*1000));//前一天
constpreDay=this.dateFormat(moment(preDate).format('YYYY-MM-DD'));
console.log(preDay,'前一日日期的格式化');
for(leti=0;inum;i++){
constnowTime=year+'/'+month+'/'+(i+1);
arr.push({
id:i+1,
date:nowTime,
isToday:toDay===nowTime,
isPreDay:false,
otherMonth:'nowMonth',
});
}
//console.log(arr,'月份日期')
ret
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 影視音樂版權獨家代理授權與版權保護合同
- 美食烹飪自媒體工作室合伙人內容創作與廣告合作協議
- 數字孿生城市規劃與設計咨詢服務協議
- 幼兒園大班音樂活動《小種子》全攻略
- 《事故傷害的防范與應對》課件
- 食堂運營團隊管理規劃
- ISO 17025實驗室管理體系培訓
- 醫學檢驗年度總結
- 《公路路面維護與管理》課件
- 《慢性腎小球腎炎》課件
- 出貨檢驗報告
- 產品追溯及模擬召回演練計劃
- 舒普電子套結機的設置和保養
- 植物中鐵的作用及缺鐵癥狀圖文演示文稿
- 合同到期協議書(3篇)
- IPC-A-610國際標準中英文對照(doc 17)
- 山大《毛澤東思想和中國特色社會主義理論體系概論》教案第3章 社會主義改造理論
- 部編版四年級下冊語文全一冊期末總復習—重點歸納整理
- (國開)2019年春電大本科水利水電工程造價管理形考3答案
- 金普新區預防性體檢人員審核表
- 礦山地質環境保護與治理恢復方案編制規范2011
評論
0/150
提交評論