最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1. 創(chuàng)建日歷組件實現(xiàn)步驟:

  • 創(chuàng)建日歷組件:首先,你需要創(chuàng)建一個日歷組件,包含顯示日期的邏輯。
  • 樣式設(shè)計:為日歷組件設(shè)計樣式,使其看起來美觀。
  • 事件處理:添加事件處理程序,以便用戶可以選擇日期。

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>
    <!-- 星期標(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>

編寫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() {
    // 初始化當(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()
    // 獲取上個月的最后幾天
    const prevMonthLastDay = new Date(year, month - 1, 0).getDate()
    // 當(dāng)前日期
    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()
      })
    }
    // 填充當(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 = []
      }
    }
    // 填充下個月的日期
    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()
    // 獲取上個月的最后幾天
    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() : ''
    // 填充上個月的日期
    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 = []
      }
    }
    // 填充下個月的日期
    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. 關(guān)于作者其它項目視頻教程介紹

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底部導(dǎo)航欄實現(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

到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自定義日歷功能的文章就介紹到這了,更多相關(guān)微信小程序自定義日歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 分享9個最好用的JavaScript開發(fā)工具和代碼編輯器

    分享9個最好用的JavaScript開發(fā)工具和代碼編輯器

    這篇文章主要介紹了9個最好用的JavaScript開發(fā)工具和代碼編輯器,需要的朋友可以參考下
    2015-03-03
  • 基于javascript實現(xiàn)圖片滑動效果

    基于javascript實現(xiàn)圖片滑動效果

    這篇文章主要為大家詳細介紹了基于javascript實現(xiàn)圖片滑動效果的相關(guān)資料,具有一定的參考價值,感興趣的朋友可以參考一下
    2016-05-05
  • js實現(xiàn)雙擊圖片放大單擊縮小的方法

    js實現(xiàn)雙擊圖片放大單擊縮小的方法

    這篇文章主要介紹了js實現(xiàn)雙擊圖片放大單擊縮小的方法,涉及js操作圖片及onclick與ondblclick事件的使用技巧,需要的朋友可以參考下
    2015-02-02
  • ES6學(xué)習(xí)筆記之let、箭頭函數(shù)和剩余參數(shù)

    ES6學(xué)習(xí)筆記之let、箭頭函數(shù)和剩余參數(shù)

    ES6為我們在函數(shù)的使用上也提供了許多的便捷的東西,下面這篇文章主要給大家介紹了關(guān)于ES6學(xué)習(xí)筆記之let、箭頭函數(shù)和剩余參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • javascript之querySelector和querySelectorAll使用介紹

    javascript之querySelector和querySelectorAll使用介紹

    其實關(guān)于querySelector和querySelectorAll的介紹說明很多。在此主要是做個記錄
    2011-12-12
  • 詳解JavaScript中typeof與instanceof用法

    詳解JavaScript中typeof與instanceof用法

    typeof用以獲取一個變量或者表達式的類型而instanceof用于判斷一個變量是否某個對象的實例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-10-10
  • jquery 實現(xiàn)輸入郵箱時自動補全下拉提示功能

    jquery 實現(xiàn)輸入郵箱時自動補全下拉提示功能

    大家在做Web項目,都會有注冊登錄模塊,如果是郵箱注冊,想要在輸入@后觸發(fā)下拉框顯示各個郵箱的功能。下面介紹一款jQuery實現(xiàn)輸入郵箱的時候,可自動補全郵箱地址,也可稱為是“輸入提示”的功能,比如輸入aaa時,自動變成aaa@163.com,有效提升網(wǎng)頁的人性化體驗
    2015-10-10
  • 前端使用div+svg畫流程圖的詳細流程記錄

    前端使用div+svg畫流程圖的詳細流程記錄

    流程圖的邏輯結(jié)構(gòu)通常由一系列節(jié)點和連線構(gòu)成,每一個節(jié)點代表了流程中的一個步驟或決策點,而連線則表示節(jié)點之間的流轉(zhuǎn)關(guān)系,這篇文章主要介紹了前端使用div+svg畫流程圖的相關(guān)資料,需要的朋友可以參考下
    2025-09-09
  • JS隨機生成不重復(fù)數(shù)據(jù)的實例方法

    JS隨機生成不重復(fù)數(shù)據(jù)的實例方法

    這篇文章介紹了JS隨機生成不重復(fù)數(shù)據(jù)的實例方法,有需要的朋友可以參考一下
    2013-07-07
  • JS設(shè)計模式之訪問者模式的用法詳解

    JS設(shè)計模式之訪問者模式的用法詳解

    JS訪問者模式是一種行為型設(shè)計模式,用于將算法與對象結(jié)構(gòu)分離, 該模式允許你定義新的操作(訪問者)而無需修改現(xiàn)有對象結(jié)構(gòu)(被訪問者), 通過這種方式,你可以在不改變對象結(jié)構(gòu)的情況下添加新的操作,本文就給大家詳細的講講JS訪問者模式的用法
    2023-08-08

最新評論

安仁县| 增城市| 汉川市| 凌云县| 漾濞| 林西县| 德阳市| 墨竹工卡县| 会昌县| 江孜县| 子长县| 巴塘县| 嵩明县| 岑溪市| 若羌县| 贡嘎县| 白朗县| 恭城| 新津县| 阿合奇县| 谢通门县| 福鼎市| 同德县| 金阳县| 会理县| 攀枝花市| 湄潭县| 沾益县| 德阳市| 凤凰县| 称多县| 江口县| 剑阁县| 名山县| 岳普湖县| 琼海市| 曲松县| 广宁县| 清涧县| 龙岩市| 垫江县|