微信小程序?qū)崿F(xiàn)簡(jiǎn)單日歷效果
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)日歷效果的具體代碼,供大家參考,具體內(nèi)容如下
效果:

wxml:
<!-- 日歷 -->
? ? ? ? <view class="myReport-calendar">
? ? ? ? ? ? <view class="img">
? ? ? ? ? ? ? ? <image mode="aspectFit" src="/imgs/calendar.png"></image>
? ? ? ? ? ? </view>
? ? ? ? ? ? <view class="calendar">
? ? ? ? ? ? ? ? <view class="selectDate">
? ? ? ? ? ? ? ? ? ? <view class="date-wrap">
? ? ? ? ? ? ? ? ? ? ? ? {{year}}年{{month}}月
? ? ? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? ? ? <view class="week">
? ? ? ? ? ? ? ? ? ? <block wx:for="{{weekArr}}" wx:for-index="index" wx:for-item="item" wx:key="key">
? ? ? ? ? ? ? ? ? ? ? ? <view>{{item}}</view>
? ? ? ? ? ? ? ? ? ? </block>
? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? ? ? <view class="date-box">
? ? ? ? ? ? ? ? ? ? <block wx:for='{{dateArr}}' wx:key='key'>
? ? ? ? ? ? ? ? ? ? ? ? <view class='{{theDay == item.isToday ? "nowDay" : ""}}' data-key='{{item.isToday}}'>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <view class='date-head' data-year='{{year}}' data-month='{{month}}'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? data-datenum='{{item.dateNum}}'>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <view>{{item.dateNum}}</view>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? ? ? ? ? ? ? </view>
? ? ? ? ? ? ? ? ? ? </block>
? ? ? ? ? ?</view>
? ? ?</view>
</view>js:
// pages/report/report.js
Page({
? ? /**
? ? ?* 頁(yè)面的初始數(shù)據(jù)
? ? ?*/
? ? data: {
? ? ? ? ? ? ? ?year: 0,
? ? ? ? month: 0,
? ? ? ? weekArr: ['日', '一', '二', '三', '四', '五', '六'],
? ? ? ? dateArr: [],
? ? ? ? isToday: "",
? ? ? ? isTodayWeek: false,
? ? ? ? todayIndex: 0,
? ? ? ? theDay: "",
? ? ? ? index: '',
? ? ? ? signNum: '', //簽到數(shù)
? ? },
? ?dateInit: function (setYear, setMonth) {
? ? ? ? //全部時(shí)間的月份都是按0~11基準(zhǔn),顯示月份才+1
? ? ? ? let dateArr1 = []; //需要遍歷的日歷數(shù)組數(shù)據(jù)
? ? ? ? let arrLen = 0; //dateArr的數(shù)組長(zhǎng)度
? ? ? ? let now = setYear ? new Date(setYear, setMonth) : new Date();
? ? ? ? let year = setYear || now.getFullYear();
? ? ? ? let nextYear = 0;
? ? ? ? let month = setMonth || now.getMonth(); //沒(méi)有+1方便后面計(jì)算當(dāng)月總天數(shù)
? ? ? ? let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
? ? ? ? let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目標(biāo)月1號(hào)對(duì)應(yīng)的星期
? ? ? ? let dayNums = new Date(year, nextMonth, 0).getDate(); //獲取目標(biāo)月有多少天
? ? ? ? let obj = {};
? ? ? ? let num = 0;
? ? ? ? if (month + 1 > 11) {
? ? ? ? ? ? nextYear = year + 1;
? ? ? ? ? ? dayNums = new Date(nextYear, nextMonth, 0).getDate();
? ? ? ? }
? ? ? ? arrLen = startWeek + dayNums;
? ? ? ? for (let i = 0; i < arrLen; i++) {
? ? ? ? ? ? if (i >= startWeek) {
? ? ? ? ? ? ? ? num = i - startWeek + 1;
? ? ? ? ? ? ? ? obj = {
? ? ? ? ? ? ? ? ? ? isToday: '' + year + (month + 1) + num,
? ? ? ? ? ? ? ? ? ? dateNum: num,
? ? ? ? ? ? ? ? ? ? weight: 5
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? obj = {};
? ? ? ? ? ? }
? ? ? ? ? ? dateArr1[i] = obj;
? ? ? ? }
? ? ? ? this.setData({
? ? ? ? ? ? dateArr: dateArr1
? ? ? ? })
? ? ? ? let nowDate = new Date();
? ? ? ? let nowYear = nowDate.getFullYear();
? ? ? ? let nowMonth = nowDate.getMonth() + 1;
? ? ? ? let nowWeek = nowDate.getDay();
? ? ? ? let getYear = setYear || nowYear;
? ? ? ? let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
? ? ? ? if (nowYear == getYear && nowMonth == getMonth) {
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? isTodayWeek: true,
? ? ? ? ? ? ? ? todayIndex: nowWeek
? ? ? ? ? ? })
? ? ? ? } else {
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? ? isTodayWeek: false,
? ? ? ? ? ? ? ? todayIndex: -1
? ? ? ? ? ? })
? ? ? ? }
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
? ? ?*/
? ? onShow: function () {
? ? ? ? let now = new Date();
? ? ? ? let year = now.getFullYear();
? ? ? ? let month = now.getMonth() + 1;
? ? ? ? this.dateInit();
? ? ? ? this.setData({
? ? ? ? ? ? year: year,
? ? ? ? ? ? month: month,
? ? ? ? ? ? isToday: '' + year + month + now.getDate(),
? ? ? ? ? ? theDay: '' + year + month + now.getDate()
? ? ? ? })
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
? ? ?*/
? ? onLoad: function (options) {
? ? ? ??
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
? ? ?*/
? ? onReady: function () {
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
? ? ?*/
? ? onShow: function () {
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
? ? ?*/
? ? onHide: function () {
? ? },
? ? /**
? ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
? ? ?*/
? ? onUnload: function () {
? ? },
? ? /**
? ? ?* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶(hù)下拉動(dòng)作
? ? ?*/
? ? onPullDownRefresh: function () {
? ? },
? ? /**
? ? ?* 頁(yè)面上拉觸底事件的處理函數(shù)
? ? ?*/
? ? onReachBottom: function () {
? ? },
? ? /**
? ? ?* 用戶(hù)點(diǎn)擊右上角分享
? ? ?*/
? ? onShareAppMessage: function () {
? ? }
})wxss:
/* 日歷 */
.myReport-calendar {
? ? width: 100%;
? ? height: 700rpx;
? ? margin-top: 27rpx;
? ? margin-bottom: 40rpx;
? ? position: relative;
}
.calendar {
? ? position: absolute;
? ? top: 88rpx;
? ? left: 0;
? ? right: 0;
? ? bottom: 0;
? ? margin: 0 auto;
}
.today .day {
? ? width: 100%;
? ? height: 100%;
? ? margin: 0 auto;
? ? text-align: center;
}
.today text {
? ? display: block;
? ? width: 60rpx;
? ? text-align: center;
? ? line-height: 60rpx;
? ? color: #fff;
? ? background: #00c8be;
? ? border-radius: 100rpx;
}
.selectDate {
? ? padding-bottom: 20rpx;
? ? text-align: center;
}
.date-wrap {
? ? font-size: 32rpx;
? ? font-weight: 600;
? ? color: #232323;
}
.week {
? ? display: flex;
? ? align-items: center;
? ? justify-content: space-around;
? ? padding: 30rpx;
? ? color: #000000;
? ? font-weight: 400;
? ? font-size: 28rpx;
}
.date-box {
? ? font-size: 0;
? ? padding: 0 30rpx 30rpx 30rpx;?
? ? margin: 0 auto;
}
.date-box>view {
? ? position: relative;
? ? display: inline-block;
? ? width: 14.285%;
? ? color: #333;
? ? text-align: center;
? ? vertical-align: middle;
? ? padding-bottom: 30rpx;
}
.date-head {
? ? height: 60rpx;
? ? line-height: 60rpx;
? ? font-size: 28rpx;
}
.nowDay .date-head {
? ? width: 60rpx;
? ? border-radius: 50%;
? ? text-align: center;
? ? color: #fff;
? ? background-color: #00c8be;
? ? margin: 0 auto;
}
.nowDay .date-head:hover {
? ? background: linear-gradient(to bottom right, #009899, #19d6cb);
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)點(diǎn)擊鏈接切換顯示隱藏內(nèi)容的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊鏈接切換顯示隱藏內(nèi)容的方法,涉及javascript鼠標(biāo)事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)變換相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
uniapp使用uni-imei插件獲取手機(jī)的設(shè)備號(hào)
uniapp框架是一款開(kāi)發(fā)跨平臺(tái)應(yīng)用的工具,它支持iOS、Android以及Web等多個(gè)平臺(tái),在這些平臺(tái)中,uniapp可以訪問(wèn)某些設(shè)備的硬件信息,這篇文章主要給大家介紹了關(guān)于uniapp使用uni-imei插件獲取手機(jī)設(shè)備號(hào)的相關(guān)資料,需要的朋友可以參考下2024-01-01
JavaScript實(shí)現(xiàn)旋轉(zhuǎn)圖像的三種方法介紹
在文檔掃描Web應(yīng)用中,我們需要旋轉(zhuǎn)傾斜的或掃描方向錯(cuò)誤的文檔圖像,這篇文章主要為大家整理了使用JavaScript旋轉(zhuǎn)圖像的三種方法,希望對(duì)大家有所幫助2024-01-01
獲取URL地址中的文件名和參數(shù)的javascript代碼
JS 獲取URL地址中的文件名和參數(shù),這個(gè)版本中有詳細(xì)的注釋。2009-09-09
深入理解JavaScript系列(35):設(shè)計(jì)模式之迭代器模式詳解
這篇文章主要介紹了深入理解JavaScript系列(35):設(shè)計(jì)模式之迭代器模式詳解,迭代器模式(Iterator):提供一種方法順序一個(gè)聚合對(duì)象中各個(gè)元素,而又不暴露該對(duì)象內(nèi)部表示,需要的朋友可以參考下2015-03-03
微信小程序的開(kāi)發(fā)范式BeautyWe.js入門(mén)詳解
這篇文章主要介紹了微信小程序的開(kāi)發(fā)范式BeautyWe.js詳解,它是一套專(zhuān)注于微信小程序的企業(yè)級(jí)開(kāi)發(fā)范式,它的愿景是:讓企業(yè)級(jí)的微信小程序項(xiàng)目中的代碼,更加簡(jiǎn)單、漂亮,需要的朋友可以參考下2019-07-07
javascript排序函數(shù)實(shí)現(xiàn)數(shù)字排序
這篇文章主要介紹了javascript排序函數(shù)實(shí)現(xiàn)數(shù)字排序的相關(guān)資料,附上示例,需要的朋友可以參考下2015-06-06
JS中onpropertychange和onchange事件區(qū)別小結(jié)
當(dāng)一個(gè)HTML元素的屬性用js改變的時(shí)候,都能通過(guò)onpropertychange來(lái)捕獲。例如一個(gè)文本text對(duì)象的value屬性被頁(yè)面的腳本修改的時(shí)候,onchange無(wú)法捕獲到,而onpropertychange卻能夠捕獲。2010-07-07

