欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序?qū)崿F(xiàn)自定義日歷功能步驟詳解

 更新時(shí)間:2025年01月23日 09:54:07   作者:浩宇軟件開(kāi)發(fā)  
這篇文章介紹了如何在微信小程序中實(shí)現(xiàn)自定義日歷功能,包括創(chuàng)建日歷組件、編寫(xiě)代碼實(shí)現(xiàn)、添加樣式和事件處理等步驟,文章還提供了其他與Android開(kāi)發(fā)相關(guā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)文章

最新評(píng)論