微信小程序日歷組件使用方法詳解
這個(gè)日歷采用小程序組件化開(kāi)發(fā),有興趣的同學(xué)可以引用本組件(怎么引用不多贅述,自行去微信小程序開(kāi)發(fā)api了解)
wxml
<!--pages/components/calender.wxml--> <view class='calender'> <view class='operate'> <text catchtap='reduce'>減少</text> <text catchtap="add">增加</text> </view> <view class='year'> <text>{{year}}年</text> <text>{{currentMonth}}月</text> </view> <view class='week'> <block wx:for="{{weekArr}}" wx:key="{{index}}"> <text>{{item}}</text> </block> </view> <view class='date'> <block wx:for="{{dateArr}}" wx:key="{{index}}"> <text>{{item-(weekNum-1)<=0?"":item-(weekNum-1)>yearMonth[currentMonth-1]?"":item-(weekNum-1)}}</text> </block> </view> </view>
js
// pages/components/calender.js Component({ data:{ weekArr:["日","一","二","三","四","五","六"], yearMonth:[], rowNum:"", dateArr:[], currentMonth:"", year:"", weekNum:"" }, created:function(){}, attached:function(){ let T = new Date() this.setData({ currentMonth: T.getMonth() + 1, year: T.getFullYear() }) //判斷閏年 let yeartype = (this.data.year % 4 == 0) && (this.data.year % 100 != 0 || this.data.year % 400 == 0) if ( yeartype ){ this.setData({ yearMonth: [31, 29 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] }) }else{ this.setData({ yearMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] }) } let currentMonthDay = this.data.year + "-" + this.data.currentMonth +"-01" let weekStr = "" this.setData({ weekNum: new Date(currentMonthDay).getDay(), rowNum: Math.ceil((this.data.yearMonth[this.data.currentMonth] + new Date(currentMonthDay).getDay())/7) }) for( let i=0 ; i<this.data.rowNum ; i++ ){ for( let j = 0 ; j<7 ; j++ ){ this.data.dateArr.push(i*7+j) } } this.setData({ dateArr:this.data.dateArr }) }, methods:{ //獲取下一個(gè)月份 add:function(){ this.triggerEvent("addone") this.setData({ dateArr: [] }) if (this.data.currentMonth==12 ){ this.setData({ currentMonth: 1, year:this.data.year+1 }) }else{ this.setData({ currentMonth: this.data.currentMonth + 1 }) } let currentMonthDay = this.data.year + "-" + this.data.currentMonth + "-01" let weekStr = "" this.setData({ weekNum: new Date(currentMonthDay).getDay(), rowNum: Math.ceil((this.data.yearMonth[this.data.currentMonth-1] + new Date(currentMonthDay).getDay()) / 7) }) for (let i = 0; i < this.data.rowNum; i++) { for (let j = 0; j < 7; j++) { this.data.dateArr.push(i * 7 + j) } } this.setData({ dateArr: this.data.dateArr }) }, //獲取上一個(gè)月份 reduce:function(){ this.triggerEvent("reduceone") this.setData({ dateArr:[] }) if (this.data.currentMonth == 1) { this.setData({ currentMonth: 12, year: this.data.year - 1 }) } else { this.setData({ currentMonth: this.data.currentMonth - 1 }) } let currentMonthDay = this.data.year + "-" + this.data.currentMonth + "-01" let weekStr = "" this.setData({ weekNum: new Date(currentMonthDay).getDay(), rowNum: Math.ceil((this.data.yearMonth[this.data.currentMonth-1] + new Date(currentMonthDay).getDay()) / 7) }) for (let i = 0; i < this.data.rowNum; i++) { for (let j = 0; j < 7; j++) { this.data.dateArr.push(i * 7 + j) } } this.setData({ dateArr: this.data.dateArr }) console.log(this.data.dateArr) } } })
wxss
/* pages/components/calender.wxss */ .operate{ width:100%; display: flex; flex-direction: row; justify-content: space-around; font-size: 32rpx; color:#000; padding-bottom: 40rpx; } .year{ padding:0 30%; display: flex; flex-direction: row; justify-content: space-around; font-size:32rpx; color:#404040; margin-bottom: 40rpx; } .calender{ display: flex; flex-direction: column; padding:0 4.5%; width:91%; border-top:1rpx solid #eaeaea; padding-top:30rpx; color:#404040; } .calender .week{ display: flex; flex-direction: row; } .calender .week text{ width:14%; text-align: center; font-size:26rpx; } .calender .date text{ width:14%; display: inline-block; text-align: center; font-size:26rpx; color:#000; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript Set與Map數(shù)據(jù)結(jié)構(gòu)詳細(xì)分析
大家心里是否產(chǎn)生過(guò)這樣的疑問(wèn),JS中既然已經(jīng)有對(duì)象這種數(shù)據(jù)結(jié)構(gòu),我們?yōu)槭裁催€要再單獨(dú)去使用Set或者M(jìn)ap呢?下面這篇文章主要給大家介紹了關(guān)于ES6中Set和Map數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-11-11javascript實(shí)現(xiàn)計(jì)算指定范圍內(nèi)的質(zhì)數(shù)示例
這篇文章主要介紹了javascript實(shí)現(xiàn)計(jì)算指定范圍內(nèi)的質(zhì)數(shù),涉及javascript數(shù)值計(jì)算與判斷相關(guān)操作技巧,需要的朋友可以參考下2018-12-12Layui事件監(jiān)聽(tīng)的實(shí)現(xiàn)(表單和數(shù)據(jù)表格)
這篇文章主要介紹了Layui事件監(jiān)聽(tīng)的實(shí)現(xiàn)(表單和數(shù)據(jù)表格),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10js阻止瀏覽器默認(rèn)行為觸發(fā)的通用方法(推薦)
下面小編就為大家?guī)?lái)一篇js阻止瀏覽器默認(rèn)行為觸發(fā)的通用方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05JS+html5 canvas實(shí)現(xiàn)的簡(jiǎn)單繪制折線(xiàn)圖效果示例
這篇文章主要介紹了JS+html5 canvas實(shí)現(xiàn)的簡(jiǎn)單繪制折線(xiàn)圖效果,結(jié)合實(shí)例形式分析了js結(jié)合HTML5 canvas技術(shù)實(shí)現(xiàn)圖形繪制的數(shù)值運(yùn)算與數(shù)組遍歷等操作技巧,需要的朋友可以參考下2017-03-03uniapp項(xiàng)目實(shí)踐封裝通用請(qǐng)求上傳以及下載方法總結(jié)
在日常開(kāi)發(fā)過(guò)程中,前端經(jīng)常要和后端進(jìn)行接口聯(lián)調(diào),獲取并且渲染數(shù)據(jù)到頁(yè)面中,接下來(lái)就總結(jié)一下?uniapp?中獲取請(qǐng)求、文件下載和上傳的一些方法2023-09-09JavaScript實(shí)現(xiàn)的瀏覽器下載文件的方法
本文通過(guò)一段簡(jiǎn)單的代碼給大家介紹了js實(shí)現(xiàn)瀏覽器下載文件的方法,需要的的朋友參考下吧2017-08-08javascript 動(dòng)態(tài)參數(shù)判空操作
在做交友中心的頁(yè)面的時(shí)候,有一個(gè)javascript函數(shù),它的第二個(gè)參數(shù)是動(dòng)態(tài)的。2008-12-12