微信小程序?qū)崿F(xiàn)自定義日歷功能步驟詳解
1. 創(chuàng)建日歷組件實(shí)現(xiàn)步驟:
- 創(chuàng)建日歷組件:首先,你需要?jiǎng)?chuàng)建一個(gè)日歷組件,包含顯示日期的邏輯。
- 樣式設(shè)計(jì):為日歷組件設(shè)計(jì)樣式,使其看起來(lái)美觀。
- 事件處理:添加事件處理程序,以便用戶(hù)可以選擇日期。
2. 代碼實(shí)現(xiàn)過(guò)程
編寫(xiě)calendar.wxml布局
<!-- calendar.wxml --> <navigation-bar title="日歷控件" back="{{false}}" color="#FFFFFF" background="#e6142c"></navigation-bar> <!-- 日歷部分 --> <view class="calendar"> <!-- 日歷頭部 --> <view class="calendar-header"> <view class="arrow" bindtap="prevMonth">〈</view> <view class="date-title">{{year}}年{{month}}月</view> <view class="arrow" bindtap="nextMonth">〉</view> </view> <!-- 星期標(biāo)題 --> <view class="weekday-row"> <view class="weekday" wx:for="{{weekdays}}" wx:key="*this">{{item}}</view> </view> <!-- 日期格子 --> <view class="date-rows"> <view class="date-row" wx:for="{{dateList}}" wx:for-item="row" wx:key="index"> <view class="date-cell {{item.currentMonth ? '' : 'other-month'}} {{item.isToday ? 'today' : ''}} {{item.selected ? 'selected' : ''}}" wx:for="{{row}}" wx:key="date" bindtap="selectDate" data-date="{{item.date}}"> {{item.day}} </view> </view> </view> </view>
編寫(xiě)calendar.wxss樣式
/* calendar.wxss */ page { height: 100vh; display: flex; flex-direction: column; background-color: #f5f5f5; } .calendar { background: #f5f5f5; border-radius: 10rpx; padding: 20rpx; } .calendar-header { display: flex; align-items: center; justify-content: space-between; padding: 20rpx 0; } .date-title { font-size: 32rpx; font-weight: bold; } .arrow { padding: 10rpx 20rpx; color: #666; } .weekday-row { display: flex; border-bottom: 1rpx solid #eee; padding-bottom: 10rpx; margin-bottom: 10rpx; } .weekday { flex: 1; text-align: center; font-size: 28rpx; color: #666; } .date-rows { display: flex; flex-direction: column; } .date-row { display: flex; margin: 5rpx 0; } .date-cell { flex: 1; text-align: center; padding: 15rpx 0; font-size: 28rpx; position: relative; height: 65rpx; line-height: 65rpx; margin: 5rpx; } .selected { background: #e6142c; color: #fff !important; /* 確保選中時(shí)文字顏色為白色 */ border-radius: 50%; } /* 確保今天的樣式和選中樣式可以共存 */ .today { color: #e6142c; font-weight: bold; } .today.selected { color: #fff !important; background: #e6142c; } /* 其他月份日期的樣式 */ .other-month { color: #ccc; } .other-month.selected { background: #1aad19; color: #fff !important; }
編寫(xiě)calendar.js具體邏輯實(shí)現(xiàn)
// calendar.js Page({ data: { year: 2024, month: 1, day: 1, weekdays: ['日', '一', '二', '三', '四', '五', '六'], dateList: [], selectedDate: null, dateStr: '', }, onLoad() { // 初始化當(dāng)前日期 const now = new Date() this.setData({ year: now.getFullYear(), month: now.getMonth() + 1, selectedDate: now // 設(shè)置當(dāng)前日期為選中狀態(tài) }, () => { this.generateDateList() }) wx.showToast({ title: this.data.selectedDate.toLocaleDateString(), icon:"none" }) }, // 生成日歷數(shù)據(jù) generateDateList() { const { year, month } = this.data const dateList = [] // 獲取當(dāng)月第一天和最后一天 const firstDay = new Date(year, month - 1, 1) const lastDay = new Date(year, month, 0) // 獲取當(dāng)月第一天是星期幾 const firstDayWeek = firstDay.getDay() // 獲取上個(gè)月的最后幾天 const prevMonthLastDay = new Date(year, month - 1, 0).getDate() // 當(dāng)前日期 const today = new Date() const currentDateStr = today.toDateString() // 填充上個(gè)月的日期 let row = [] for (let i = 0; i < firstDayWeek; i++) { const day = prevMonthLastDay - firstDayWeek + i + 1 const date = new Date(year, month - 2, day) row.push({ day, date: date, currentMonth: false, isToday: date.toDateString() === currentDateStr, selected: this.data.selectedDate && date.toDateString() === this.data.selectedDate.toDateString() }) } // 填充當(dāng)月日期 for (let day = 1; day <= lastDay.getDate(); day++) { const date = new Date(year, month - 1, day) row.push({ day, date: date, currentMonth: true, isToday: date.toDateString() === currentDateStr, selected: this.data.selectedDate && date.toDateString() === this.data.selectedDate.toDateString() }) if (row.length === 7) { dateList.push(row) row = [] } } // 填充下個(gè)月的日期 let nextMonthDay = 1 while (row.length < 7) { const date = new Date(year, month, nextMonthDay) row.push({ day: nextMonthDay++, date: date, currentMonth: false, isToday: date.toDateString() === currentDateStr, selected: this.data.selectedDate && date.toDateString() === this.data.selectedDate.toDateString() }) } dateList.push(row) this.setData({ dateList }) }, // 選擇日期 selectDate(e) { const selectedDate = new Date(e.currentTarget.dataset.date) this.setData({ selectedDate: selectedDate }, () => { this.generateDateList() //格式化日期 const date = new Date(selectedDate.toLocaleDateString()) wx.showToast({ title: selectedDate.toLocaleDateString(), icon:"none" }) }) }, // 生成日歷數(shù)據(jù) generateDateList() { const { year, month } = this.data const dateList = [] // 獲取當(dāng)月第一天和最后一天 const firstDay = new Date(year, month - 1, 1) const lastDay = new Date(year, month, 0) // 獲取當(dāng)月第一天是星期幾 const firstDayWeek = firstDay.getDay() // 獲取上個(gè)月的最后幾天 const prevMonthLastDay = new Date(year, month - 1, 0).getDate() // 當(dāng)前日期 const today = new Date() const currentDateStr = today.toDateString() // 選中的日期字符串(用于比較) const selectedDateStr = this.data.selectedDate ? this.data.selectedDate.toDateString() : '' // 填充上個(gè)月的日期 let row = [] for (let i = 0; i < firstDayWeek; i++) { const day = prevMonthLastDay - firstDayWeek + i + 1 const date = new Date(year, month - 2, day) const dateStr = date.toDateString() row.push({ day, date: dateStr, currentMonth: false, isToday: dateStr === currentDateStr, selected: selectedDateStr && dateStr === selectedDateStr }) } // 填充當(dāng)月日期 for (let day = 1; day <= lastDay.getDate(); day++) { const date = new Date(year, month - 1, day) const dateStr = date.toDateString() row.push({ day, date: dateStr, currentMonth: true, isToday: dateStr === currentDateStr, selected: selectedDateStr && dateStr === selectedDateStr }) if (row.length === 7) { dateList.push(row) row = [] } } // 填充下個(gè)月的日期 let nextMonthDay = 1 while (row.length < 7) { const date = new Date(year, month, nextMonthDay) const dateStr = date.toDateString() row.push({ day: nextMonthDay++, date: dateStr, currentMonth: false, isToday: dateStr === currentDateStr, selected: selectedDateStr && dateStr === selectedDateStr }) } if (row.length > 0) { dateList.push(row) } this.setData({ dateList }) }, // 上個(gè)月 prevMonth() { let { year, month } = this.data if (month === 1) { year-- month = 12 } else { month-- } this.setData({ year, month }, () => { this.generateDateList() }) }, // 下個(gè)月 nextMonth() { let { year, month } = this.data if (month === 12) { year++ month = 1 } else { month++ } this.setData({ year, month }, () => { this.generateDateList() }) } });
3. 實(shí)現(xiàn)效果圖
4. 關(guān)于作者其它項(xiàng)目視頻教程介紹
Android新聞資訊app實(shí)戰(zhàn):https://www.bilibili.com/video/BV1CA1vYoEad/?vd_source=984bb03f768809c7d33f20179343d8c8
Androidstudio開(kāi)發(fā)購(gòu)物商城實(shí)戰(zhàn):https://www.bilibili.com/video/BV1PjHfeXE8U/?vd_source=984bb03f768809c7d33f20179343d8c8
Android開(kāi)發(fā)備忘錄記事本實(shí)戰(zhàn):https://www.bilibili.com/video/BV1FJ4m1u76G?vd_source=984bb03f768809c7d33f20179343d8c8&spm_id_from=333.788.videopod.sections
Androidstudio底部導(dǎo)航欄實(shí)現(xiàn):https://www.bilibili.com/video/BV1XB4y1d7et/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8
Android使用TabLayout+ViewPager2實(shí)現(xiàn)左右滑動(dòng)切換:https://www.bilibili.com/video/BV1Mz4y1c7eX/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8
為什么要封裝BaseActivity?: https://www.bilibili.com/video/BV11S411A7R5/?vd_source=984bb03f768809c7d33f20179343d8c8
為什么要封裝BaseFragment?:https://www.bilibili.com/video/BV1Um421G7yC/?vd_source=984bb03f768809c7d33f20179343d8c8
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自定義日歷功能的文章就介紹到這了,更多相關(guān)微信小程序自定義日歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript讀取中文cookie時(shí)的亂碼問(wèn)題的解決方法
讀取中文cookie時(shí)出現(xiàn)亂碼,下面是具體的解決方法,大家以后使用過(guò)程中,盡量不要用中文。2009-10-10requireJS模塊化實(shí)現(xiàn)返回頂部功能的方法詳解
這篇文章主要介紹了requireJS模塊化實(shí)現(xiàn)返回頂部功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了requireJS的使用步驟及返回頂部功能的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果
本文主要分享Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02javascript獲取URL參數(shù)與參數(shù)值的示例代碼
本篇文章主要是對(duì)javascript獲取URL參數(shù)與參數(shù)值的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12JavaScript實(shí)現(xiàn)清空(重置)文件類(lèi)型INPUT元素值的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)清空(重置)文件類(lèi)型INPUT元素值的方法,結(jié)合實(shí)例形式分析了javascript清空input文本框的常用方法與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11BootStrap表單驗(yàn)證 FormValidation 調(diào)整反饋圖標(biāo)位置的實(shí)例代碼
這篇文章主要介紹了BootStrap表單驗(yàn)證 FormValidation 調(diào)整反饋圖標(biāo)位置的實(shí)例代碼,需要的朋友可以參考下2017-05-05JS 對(duì)象屬性相關(guān)(檢查屬性、枚舉屬性等)
這篇文章主要介紹了JS 對(duì)象屬性相關(guān)(檢查屬性、枚舉屬性等),需要的朋友可以參考下2015-04-04對(duì)于input 框限定輸入值為浮點(diǎn)型的js代碼
下面小編就為大家?guī)?lái)一篇對(duì)于input 框限定輸入值為浮點(diǎn)型的js代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09