本文實(shí)例為大家分享了小程序自定義日歷展示的具體代碼,供大家參考,具體內(nèi)容如下
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了南關(guān)免費(fèi)建站歡迎大家使用!
重點(diǎn)在于將數(shù)個(gè)月的日期拆分重組,然后再統(tǒng)一以月為輸出。詳細(xì)操作代碼注釋有說(shuō)明。
<view class="flex box box-tb box-align-center"> <!--整體日期循環(huán)部分 --> <block wx:for="{{allMan}}" wx:for-item="item" > <view class="calendar pink-color box box-tb"> <view class="top-handle fs28 box box-lr box-align-center box-pack-center"> <view class="date-area box box-lr box-align-center box-pack-center years-month">{{item.year || "--"}} 年 {{item.Month || "--"}} 月</view> </view> <!--展示星期幾的頭部 --> <view class="weeks box box-lr box-pack-center box-align-center day-week"> <view class="flex week fs28" wx:for="{{weeks_ch}}" wx:key="{{index}}" data-idx="{{index}}">{{item}}</view> </view> <view class="days box box-lr box-wrap"> <view class="grid white-color box box-align-center box-pack-center" wx:for="{{item.empyt}}" wx:key="{{index}}" data-idx="{{index}}"> </view> <!--循環(huán)日期 --> <block wx:for="{{item.day}}" wx:for-item="item" wx:key="{{index}}"> <view class="grid white-color box box-align-center box-pack-center" data-idx="{{index}}" data-dayz="{{item.dayZ}}" bindtap="{{item.canChoosed ? 'tapDayItemX' : ''}}"> <view class="day {{item.dependence ? 'border-instinct pink-bg' : ''}} {{item.startDay ? 'border-radius dependence-bg' : ''}} {{item.endDay ? 'border-right-radius dependence-bg' : ''}} {{item.canChoosed ? '' : 'grays'}} box box-align-center box-pack-center"> {{item.today}} </view> </view> </block> </view> </view> </block> </view>
.js:
let choose_year = null,//選擇的年 choose_month = null;//選擇的月 let clickNum=new Array();//點(diǎn)擊日期的次數(shù),做顯示判斷處理 let date = new Date(); let cur_year = date.getFullYear(); let cur_month = date.getMonth() + 1; let weeks_ch = ['日', '一', '二', '三', '四', '五', '六']; //日歷的顯示 let allMan = new Array();//循環(huán)總?cè)諝v容器 let haveClickNum=false;//是否可點(diǎn)擊選擇日期 let havInitDate = false;//是否已經(jīng)重置數(shù)據(jù) let conf = {//拿去給Page onLoad() { if (allMan.length != 0) {//重新進(jìn)入界面發(fā)現(xiàn)已經(jīng)有數(shù)據(jù),直接渲染 havInitDate = false; clickNum = new Array(); haveClickNum = true; this.setData({ allMan, weeks_ch }) }else{ allMan = new Array();//非常重要的主輸出,計(jì)算好的每月數(shù)據(jù)都合拼在里面 clickNum = new Array(); let that = this; let empytGrids = this.calculateEmptyGrids(cur_year, cur_month);//計(jì)算空格 let days = this.calculateDays(cur_year, cur_month);//計(jì)算日期總數(shù) let DogDays = { year: cur_year, Month: cur_month, empyt: empytGrids, day: days };//組裝年、月、星期一前的空格、日 allMan.push(DogDays); let newMonth = cur_month + 1; let newYear = cur_year; if (newMonth > 12) {//新年重置1月份 newYear = cur_year + 1; newMonth = 1; } let empytGridsTwos = this.calculateEmptyGridsTwo(newYear, newMonth);//計(jì)算新月份的空格 let dayTwos = this.calculateDaysTwo(newYear, newMonth);//計(jì)算日期總數(shù) let catDays = { year: newYear, Month: newMonth, empyt: empytGridsTwos, day: dayTwos }; allMan.push(catDays); let sakura = days.concat(dayTwos);//將存放日期的內(nèi)容統(tǒng)一合并為數(shù)組,單獨(dú)處理是否可選等展示狀態(tài),屬于中間件數(shù)組 let isTrue = new Array();//存放可選 for (var i = 0; i < sakura.length; i++) {//循環(huán)處理 if (sakura[i].canChoosed == true) { isTrue.push(sakura[i]); } else { continue } } let addMoreMonth = function () {//添加更多的月份 let isTrue = new Array();//是否可選的日期數(shù)組,為N個(gè)月等可選日期總?cè)蛰斪雠袛? for (var i = 0; i < sakura.length; i++) { if (sakura[i].canChoosed == true) { isTrue.push(sakura[i]); } else { continue } } let newMonthThree = newMonth + 1; let newYearThree = newYear; if (newMonthThree > 12) { newYearThree = newYear + 1; newMonthThree = 1; } let empytGridsThree = that.calculateEmptyGridsTwo(newYearThree, newMonthThree);//計(jì)算空格 let dayThree = that.calculateDaysTwo(newYearThree, newMonthThree, 91 - isTrue.length);//計(jì)算日期總數(shù),61減,今天不算 let duckDays = { year: newYearThree, Month: newMonthThree, empyt: empytGridsThree, day: dayThree }; sakura = sakura.concat(dayThree); allMan.push(duckDays); if (parseInt(91 - isTrue.length) > parseInt(dayThree.length)) {//第n個(gè)月是否足夠顯示需要的可選日期長(zhǎng)度 for (var i = 0; i < dayThree.length; i++) { if (dayThree[i].canChoosed == true) { isTrue.push(dayThree[i]); } else { continue } } let newMonthFour = newMonthThree + 1; let newYearFour = newYearThree; if (newMonthFour > 12) { newYearFour = newYearThree + 1; newMonthFour = 1; } let empytGridsFour = that.calculateEmptyGridsTwo(newYearFour, newMonthFour);//計(jì)算空格 let dayFour = that.calculateDaysTwo(newYearFour, newMonthFour, 91 - isTrue.length);//計(jì)算日期總數(shù),61減,今天不算 let wolfDays = { year: newYearFour, Month: newMonthFour, empyt: empytGridsFour, day: dayFour }; sakura = sakura.concat(dayFour); allMan.push(wolfDays); } else {//不足夠繼續(xù)增加第n+1個(gè)月 let newMonthFour = newMonthThree + 1; let newYearFour = newYearThree; if (newMonthFour > 12) { newYearFour = newYearThree + 1; newMonthFour = 1; } let empytGridsFour = that.calculateEmptyGridsTwo(newYearFour, newMonthFour);//計(jì)算空格 let dayFour = that.calculateDaysTwo(newYearFour, newMonthFour, -1);//計(jì)算日期總數(shù),61減,今天不算 let wolfDays = { year: newYearFour, Month: newMonthFour, empyt: empytGridsFour, day: dayFour }; sakura = sakura.concat(dayFour); allMan.push(wolfDays); } } if (isTrue.length < 90) { addMoreMonth(); } this.setData({ allMan, weeks_ch }) } }, onShow: function () { // console.log(allMan); // allMan = new Array();//循環(huán)總?cè)諝v容器 // 頁(yè)面顯示 }, getThisMonthDays(year, month) { return new Date(year, month, 0).getDate(); }, getFirstDayOfWeek(year, month) { return new Date(Date.UTC(year, month - 1, 1)).getDay(); }, calculateEmptyGrids(year, month) { let firstDayOfWeek = this.getFirstDayOfWeek(year, month); let empytGrids = []; if (firstDayOfWeek > 0) { for (let i = 0; i < firstDayOfWeek; i++) { empytGrids.push(i); } } return (empytGrids); }, calculateEmptyGridsTwo(year, month) { let firstDayOfWeek = this.getFirstDayOfWeek(year, month); let empytGridsTwo = []; if (firstDayOfWeek > 0) { for (let i = 0; i < firstDayOfWeek; i++) { empytGridsTwo.push(i); } } return (empytGridsTwo); }, calculateDays(year, month) { let days = []; let date = new Date(); let thisMonthDays = this.getThisMonthDays(year, month);//這個(gè)月的總?cè)諗?shù) let d = date.getDate(); let weeHours=date.getHours(); let zaa = year+month+date.getDate(); for (let i = 1; i <= thisMonthDays; i++) { if (year+month+i == zaa){ days.push({ day: i, dayZ: cur_year+"-"+cur_month+"-"+i, choosed: false, canChoosed: true, today: i }); } else if (year + month + i < zaa){ if (weeHours <= 2 && year + month + i == zaa-1){//加個(gè)判斷:是否凌晨3點(diǎn)之前,是的話就拿日期集-1來(lái)對(duì)前一日可選 days.push({ day: i, dayZ: cur_year + "-" + cur_month + "-" + i, choosed: false, canChoosed: true, today: i }); }else{ days.push({ day: i, dayZ: cur_year + "-" + cur_month + "-" + i, choosed: false, canChoosed: false, today: i }); } } else{ days.push({ day: i, dayZ: cur_year + "-" + cur_month + "-" + i, choosed: false, canChoosed: true, today: i }); } } this.setData({ // days }); return (days); }, calculateDaysTwo(year, month,takeNum) { let days_two = []; let date = new Date(); let thisMonthDays = this.getThisMonthDays(year, month); let d = date.getDate(); let zaa = year+"-"+month+"-"+date.getDate(); if (takeNum) { console.log(takeNum); } for (let i = 1; i <= thisMonthDays; i++) { takeNum--; if (takeNum<0){ days_two.push({ day: i, dayZ: year + "-" + month + "-" + i, choosed: false, canChoosed: false, today: i }); }else{ days_two.push({ day: i, dayZ: year + "-" + month + "-" + i, choosed: false, canChoosed: true, today: i }); } } this.setData({ // days_two }); return (days_two); }, handleCalendar(e) { let handle = e.currentTarget.dataset.handle; let cur_year = this.data.cur_year; let cur_month = this.data.cur_month; if (handle === 'prev') { let newMonth = cur_month - 1; let newYear = cur_year; if (newMonth < 1) { newYear = cur_year - 1; newMonth = 12; } this.calculateDays(newYear, newMonth); this.calculateEmptyGrids(newYear, newMonth); this.setData({ cur_year: newYear, cur_month: newMonth }); } else { let newMonth = cur_month + 1; let newYear = cur_year; if (newMonth > 12) { newYear = cur_year + 1; newMonth = 1; } this.calculateDays(newYear, newMonth); this.calculateEmptyGrids(newYear, newMonth); this.setData({ cur_year: newYear, cur_month: newMonth }); } }, tapDayItemX(e) {//點(diǎn)擊日期處理 if (clickNum.length >= 2)//點(diǎn)了兩次,不可再點(diǎn) return; if (haveClickNum){//是否已經(jīng)選擇過(guò)日期 this.initChosedDate();//重置選擇過(guò)的樣式 let dayZ = e.currentTarget.dataset.dayz; for (let i = 0; i < allMan.length; i++) { let li = allMan[i].day; for (let k = 0; k < li.length; k++) { if (li[k].dayZ == dayZ) { if (clickNum.length == 0) { li[k].startDay = true; } if (clickNum.length == 1) { li[k].endDay = true; } }else{//已選擇的區(qū)間日期重置 li[k].dependence = false; } } } this.setData({//渲染 allMan }) clickNum.push(integerDate(dayZ));//格式化日期,準(zhǔn)備拿去比較函數(shù)處理 }else{//第一次進(jìn)入生命周期,從沒(méi)選擇過(guò)的處理 let dayZ = e.currentTarget.dataset.dayz; for (let i = 0; i < allMan.length; i++) { let li = allMan[i].day; for (let k = 0; k < li.length; k++) { if (li[k].dayZ == dayZ) { if (clickNum.length == 0) { li[k].startDay = true; } if (clickNum.length == 1) { li[k].endDay = true; } } } } this.setData({//渲染 allMan }) clickNum.push(integerDate(dayZ));//格式化日期,準(zhǔn)備拿去比較函數(shù)處理 } if (clickNum.length < 2)//點(diǎn)擊了第二次,進(jìn)行teturn下面的渲染操作 return clickNum.sort(compare);//拿去比較函數(shù)處理 let dayDiff = GetDateDiff(clickNum[0], clickNum[1]); let startDay = clickNum[0];//第一位為開(kāi)始日期 let startMonth = new Date(clickNum[0]).getMonth() + 1;//開(kāi)始的月,跨月顯示用 let formatStartDay = new Date(clickNum[0]).getFullYear() + '-' + startMonth + '-' + new Date(clickNum[0]).getDate();//格式化開(kāi)始日期,后面用于給總輸出allman做判斷 let endMonth = new Date(clickNum[1]).getMonth() + 1;//結(jié)束的月,跨月顯示用 let formatEndDay = new Date(clickNum[1]).getFullYear() + '-' + endMonth + '-' + new Date(clickNum[1]).getDate();//格式化結(jié)束日期,后面用于給總輸出allman做判斷 let endDay = clickNum[1];//第二位為結(jié)束日期 for (let i = 0; i < allMan.length; i++) {//循環(huán)總輸出allMan數(shù)組得出每月的日期數(shù)組,將開(kāi)始、結(jié)束日期帶進(jìn)去做改變顯示狀態(tài)處理 let li = allMan[i].day; for (let k = 0; k < li.length; k++) {//每個(gè)月的日期數(shù)組,拆分更細(xì) let foxDay = integerDate(li[k].dayZ); if (startDay < foxDay && foxDay < endDay){ li[k].dependence = true;//已選擇的區(qū)間日期 } else if (li[k].dayZ == formatStartDay){//開(kāi)始日期狀態(tài)處理 li[k].startDay = true; li[k].endDay = false; } else if (li[k].dayZ == formatEndDay) {//結(jié)束日期狀態(tài)處理 li[k].startDay = false; li[k].endDay = true; } } } this.setData({//再渲染 allMan }) wx.navigateBack({ delta: 1, }) }, initChosedDate(){ if (havInitDate) return for (let i = 0; i < allMan.length; i++) { let li = allMan[i].day; for (let k = 0; k < li.length; k++) { li[k].startDay = false; li[k].endDay = false; } } havInitDate=true; }, }; Page(conf); let compare = function (x, y) {//比較函數(shù),那個(gè)日期誰(shuí)先誰(shuí)后,star/end if (x < y) { return -1; } else if (x > y) { return 1; } else { return 0; } } function GetDateDiff(startDate, endDate) { let dates = Math.abs((startDate - endDate)) / (1000 * 60 * 60 * 24); return dates; } function integerDate(choseDate){ let integerDay = new Date(Date.parse(choseDate.replace(/-/g, "/"))).getTime(); return integerDay; }
.wxss:
/* pages/calendar/calendar.wxss */ .top-handle { height: 80rpx; } .prev { text-align: right; height: 80rpx; } .next { height: 80rpx; } .prev-handle { width: 80rpx; height: 100%; } .next-handle { width: 80rpx; height: 100%; } .date-area { width: 50%; height: 80rpx; text-align: center; } .weeks { height: 50rpx; line-height: 50rpx; opacity: 0.5 } .week { text-align: center; } .days { height: 500rpx; } .grid { width: 107.1428571429rpx; } .day { width: 100%; height: 100%; color: #000; font-size: 26rpx; font-weight: 200; } .border-instinct{ position: relative; left: 0; top: 0; color: #fff; } .border-radius { border-radius:5px 0 0 5px; position: relative; left: 0; top: 0; color: #fff; } .border-right-radius { border-radius:0 5px 5px 0; position: relative; left: 0; top: 0; color: #fff; } .pink-bg { background-color: #ffe5b4; color: #f6ac1d; } .dependence-bg { background-color: #d9246b; } .purple-bg { background-color: #b8b8f1; } .right-triangle::after { content: ""; display: block; width: 0; height: 0; border: 15rpx solid transparent; border-left-color: #ff629a; position: absolute; right: -22rpx; top: 18rpx; } .left-triangle::before { content: ""; display: block; width: 0; height: 0; border: 15rpx solid transparent; border-right-color: #ff629a; position: absolute; left: -22rpx; top: 18rpx; } .tips { text-align: center; margin-top: 20rpx; margin-bottom: 20rpx; } .types { background-color: #ffedf4; height: 50rpx; } .type-dot { width: 25rpx; height: 25rpx; border-radius: 50%; margin-right: 10rpx; } .type-dot-ymq { color:#FF7CA0; background-color: #FF7CA0; } .type-dot-ycq { color: rgb(255, 200, 202); background-color: rgb(255, 200, 202); } .type-dot-aqq { color: rgb(118, 191, 92); background-color: rgb(118, 191, 92); } .type-dot-yyq { color: #FF7CA0; background-color: #FF7CA0; } .type-dot-plr { color: rgb(211, 189, 215); background-color: rgb(211, 189, 215); } .types-desc { padding: 0 20rpx; } .type-name { margin-top: 50rpx; margin-bottom: 30rpx; } .type-desc { padding: 0 35rpx; line-height: 38rpx; } .explain { border-top: 1px solid #eee; width: 90%; margin: 20rpx 5% 20rpx 5%; padding: 20rpx 0; } .explain-title { font-weight: bold; margin-bottom: 15rpx; } .explain-item { padding: 8rpx 20rpx; color: #fff; } .left-border-radius { border-top-left-radius: 20rpx; border-bottom-left-radius: 20rpx; } .right-border-radius { border-top-right-radius: 20rpx; border-bottom-right-radius: 20rpx; } .picker-btns { height: 120rpx; line-height: 120rpx; border-bottom: 1rpx solid #FF7CA0; } .picker-confirm { margin-right: 50rpx; } .picker-cancel { margin-left: 50rpx; } .picker-view { color:#FF7CA0; text-align: center; } .box { display: flex; } .box-lr { flex-direction: row; } .box-rl { flex-direction: row-reverse; } .box-tb { flex-direction: column; } .box-bt { flex-direction: column-reverse; } .box-pack-center { justify-content: center; } .day-week{ color:#ccc; } .box-pack-start { justify-content: flex-start; } .box-pack-end { justify-content: flex-end; } .box-pack-between { justify-content: space-between; } .box-pack-around { justify-content: space-around; } .box-align-center { align-items: center; } .years-month{ color: #000; } .box-align-start { align-items: flex-start; } .box-align-end { align-items: flex-end; } .self-align-center { align-self: center; margin: 0 auto; } .self-align-start { align-self: flex-start; } .self-align-end { align-self: flex-end; } .self-align-stretch { align-self: stretch; } .box-wrap { flex-wrap: wrap; } .box-nowrap { flex-wrap: nowrap; } .flex { flex-grow: 1; background: #fff; } .shrink { flex-shrink: 1; } .bg { background-image: linear-gradient(to bottom, #faefe7, #ffcbd7); overflow: hidden; } .brown-color { color: #784344; } .pink-color { color: #ff629a; } .white-color { color: #fff; } .fs24 { font-size: 24rpx; } .fs28 { font-size: 28rpx; } .fs32 { font-size: 32rpx; } .fs36 { font-size: 36rpx; } /*灰色顯示 */ .grays{ color: #ccc; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前標(biāo)題:小程序自定義日歷效果
文章位置:http://chinadenli.net/article46/jgggeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站制作、企業(yè)建站、商城網(wǎng)站、網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)