微信小程序實現(xiàn)自定義日歷功能步驟詳解
1. 創(chuàng)建日歷組件實現(xiàn)步驟:
- 創(chuàng)建日歷組件:首先,你需要創(chuàng)建一個日歷組件,包含顯示日期的邏輯。
- 樣式設計:為日歷組件設計樣式,使其看起來美觀。
- 事件處理:添加事件處理程序,以便用戶可以選擇日期。
2. 代碼實現(xiàn)過程
編寫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>
<!-- 星期標題 -->
<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>編寫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;
/* 確保選中時文字顏色為白色 */
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;
}編寫calendar.js具體邏輯實現(xiàn)
// calendar.js
Page({
data: {
year: 2024,
month: 1,
day: 1,
weekdays: ['日', '一', '二', '三', '四', '五', '六'],
dateList: [],
selectedDate: null,
dateStr: '',
},
onLoad() {
// 初始化當前日期
const now = new Date()
this.setData({
year: now.getFullYear(),
month: now.getMonth() + 1,
selectedDate: now // 設置當前日期為選中狀態(tài)
}, () => {
this.generateDateList()
})
wx.showToast({
title: this.data.selectedDate.toLocaleDateString(),
icon:"none"
})
},
// 生成日歷數(shù)據(jù)
generateDateList() {
const {
year,
month
} = this.data
const dateList = []
// 獲取當月第一天和最后一天
const firstDay = new Date(year, month - 1, 1)
const lastDay = new Date(year, month, 0)
// 獲取當月第一天是星期幾
const firstDayWeek = firstDay.getDay()
// 獲取上個月的最后幾天
const prevMonthLastDay = new Date(year, month - 1, 0).getDate()
// 當前日期
const today = new Date()
const currentDateStr = today.toDateString()
// 填充上個月的日期
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()
})
}
// 填充當月日期
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 = []
}
}
// 填充下個月的日期
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 = []
// 獲取當月第一天和最后一天
const firstDay = new Date(year, month - 1, 1)
const lastDay = new Date(year, month, 0)
// 獲取當月第一天是星期幾
const firstDayWeek = firstDay.getDay()
// 獲取上個月的最后幾天
const prevMonthLastDay = new Date(year, month - 1, 0).getDate()
// 當前日期
const today = new Date()
const currentDateStr = today.toDateString()
// 選中的日期字符串(用于比較)
const selectedDateStr = this.data.selectedDate ? this.data.selectedDate.toDateString() : ''
// 填充上個月的日期
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
})
}
// 填充當月日期
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 = []
}
}
// 填充下個月的日期
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
})
},
// 上個月
prevMonth() {
let {
year,
month
} = this.data
if (month === 1) {
year--
month = 12
} else {
month--
}
this.setData({
year,
month
}, () => {
this.generateDateList()
})
},
// 下個月
nextMonth() {
let {
year,
month
} = this.data
if (month === 12) {
year++
month = 1
} else {
month++
}
this.setData({
year,
month
}, () => {
this.generateDateList()
})
}
});3. 實現(xiàn)效果圖

4. 關于作者其它項目視頻教程介紹
Android新聞資訊app實戰(zhàn):https://www.bilibili.com/video/BV1CA1vYoEad/?vd_source=984bb03f768809c7d33f20179343d8c8
Androidstudio開發(fā)購物商城實戰(zhàn):https://www.bilibili.com/video/BV1PjHfeXE8U/?vd_source=984bb03f768809c7d33f20179343d8c8
Android開發(fā)備忘錄記事本實戰(zhàn):https://www.bilibili.com/video/BV1FJ4m1u76G?vd_source=984bb03f768809c7d33f20179343d8c8&spm_id_from=333.788.videopod.sections
Androidstudio底部導航欄實現(xiàn):https://www.bilibili.com/video/BV1XB4y1d7et/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8
Android使用TabLayout+ViewPager2實現(xiàn)左右滑動切換: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
到此這篇關于微信小程序實現(xiàn)自定義日歷功能的文章就介紹到這了,更多相關微信小程序自定義日歷內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
分享9個最好用的JavaScript開發(fā)工具和代碼編輯器
這篇文章主要介紹了9個最好用的JavaScript開發(fā)工具和代碼編輯器,需要的朋友可以參考下2015-03-03
ES6學習筆記之let、箭頭函數(shù)和剩余參數(shù)
ES6為我們在函數(shù)的使用上也提供了許多的便捷的東西,下面這篇文章主要給大家介紹了關于ES6學習筆記之let、箭頭函數(shù)和剩余參數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
javascript之querySelector和querySelectorAll使用介紹
其實關于querySelector和querySelectorAll的介紹說明很多。在此主要是做個記錄2011-12-12
詳解JavaScript中typeof與instanceof用法
typeof用以獲取一個變量或者表達式的類型而instanceof用于判斷一個變量是否某個對象的實例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-10-10
jquery 實現(xiàn)輸入郵箱時自動補全下拉提示功能
大家在做Web項目,都會有注冊登錄模塊,如果是郵箱注冊,想要在輸入@后觸發(fā)下拉框顯示各個郵箱的功能。下面介紹一款jQuery實現(xiàn)輸入郵箱的時候,可自動補全郵箱地址,也可稱為是“輸入提示”的功能,比如輸入aaa時,自動變成aaa@163.com,有效提升網頁的人性化體驗2015-10-10

