• 微信小程序 typescript 开发日历界面


    1.界面代码

    1. <view class="o-calendar">
    2. <view class="o-calendar-container" >
    3. <view class="o-calendar-titlebar">
    4. <view class="o-left_arrow" bind:tap="prevMonth">view>
    5. {{year}}年{{month}}月
    6. <view class="o-right_arrow" bind:tap="nextMonth">view>
    7. <view class="o-btn-current-month" bind:tap="locateThisMonth">
    8. <image class="o-btn-current-month-img" src="../../images/month.svg" />
    9. view>
    10. view>
    11. <view class="o-calendar-weekdays">
    12. <view wx:for="{{weekdays}}" wx:key="id" class="o-calendar-weekdays-item" >{{item}}view>
    13. view>
    14. <view class="o-calendar-weeks">
    15. <block wx:for="{{calendarData}}" wx:key="index" >
    16. <view class="o-calendar-week">
    17. <block wx:for="{{item}}" wx:key="index">
    18. <block wx:if="{{item===0}}">
    19. <view class="o-calendar-item">view>
    20. block>
    21. <block wx:else>
    22. <block wx:if="{{today && item===day}}">
    23. <view class="o-calendar-day o-calendar-day-cur" bindtap="selectDate" data-value='{{item}}'>{{item}}view>
    24. block>
    25. <block wx:elif="{{item===selectedDay}}">
    26. <view class="o-calendar-day o-calendar-day-selected" bindtap="selectDate" data-value='{{item}}'>{{item}}view>
    27. block>
    28. <block wx:else>
    29. <view class="o-calendar-day" bindtap="selectDate" data-value='{{item}}'>{{item}}view>
    30. block>
    31. block>
    32. block>
    33. view>
    34. block>
    35. view>
    36. view>
    37. view>

    2.样式设置

    1. page{
    2. --o-calendar-background-color: #F1EBFE;
    3. }
    4. .o-calendar {
    5. width: 100%;
    6. display: flex;
    7. flex-direction: column;
    8. box-sizing: border-box;
    9. padding: 10rpx 30rpx;
    10. }
    11. .o-calendar-container{
    12. padding: 0rpx 16rpx;
    13. background-color: var(--o-calendar-background-color);
    14. border-radius: 20rpx;
    15. }
    16. .o-calendar-titlebar{
    17. display: flex;
    18. justify-content: center;
    19. align-items: center;
    20. height: 50rpx;
    21. font-size: 18px;
    22. font-weight: bold;
    23. padding-top: 20rpx;
    24. }
    25. .o-left_arrow {
    26. width: 50rpx;
    27. height: 50rpx;
    28. position: absolute;
    29. left: 180rpx;
    30. }
    31. .o-right_arrow {
    32. width: 50rpx;
    33. height: 50rpx;
    34. position: absolute;
    35. right: 180rpx;
    36. }
    37. .o-btn-current-month{
    38. width: 50rpx;
    39. height: 50rpx;
    40. position: absolute;
    41. right: 80rpx;
    42. }
    43. .o-btn-current-month-img{
    44. width: 100%;
    45. height: 100%;
    46. }
    47. .o-calendar-weekdays{
    48. width: 100%;
    49. height: 80rpx;
    50. display: flex;
    51. justify-content: space-between;
    52. align-items: center;
    53. }
    54. .o-calendar-weekdays-item{
    55. width: 60rpx;
    56. line-height: 60rpx;
    57. text-align: center;
    58. margin: 5rpx;
    59. }
    60. .o-calendar-week{
    61. width: 100%;
    62. display: flex;
    63. justify-content: space-between;
    64. }
    65. .o-calendar-item{
    66. width: 60rpx;
    67. height: 60rpx;
    68. position: relative;
    69. line-height: 60rpx;
    70. margin: 5rpx;
    71. }
    72. .o-calendar-day {
    73. width: 60rpx;
    74. height: 60rpx;
    75. position: relative;
    76. display: flex;
    77. line-height: 60rpx;
    78. text-align: center;
    79. justify-content: center;
    80. margin: 5rpx;
    81. border-radius: 50%;
    82. cursor: pointer;
    83. background-color: #fff;
    84. }
    85. .o-calendar-day-cur{
    86. font-weight: bold;
    87. color: #fff;
    88. background-color: #F6AD77;
    89. }
    90. .o-calendar-day-selected{
    91. font-weight: bold;
    92. color: #fff;
    93. background-color: rgb(7, 3, 1);
    94. }

    3.typescript脚本处理

    1. /**
    2. * 页面的初始数据
    3. */
    4. data: {
    5. year: 0, //记录年
    6. month: 0, //记录月
    7. day:0,//记录日
    8. selectedDay:0,
    9. calendarData: [], //记录日期
    10. weekdays: ['日', '一', '二', '三', '四', '五', '六'],
    11. today:false,
    12. },
    13. /**
    14. * 生命周期函数--监听页面加载
    15. */
    16. onLoad() {
    17. this.renderCalendar();
    18. },
    19. renderCalendar: function () {
    20. //获取当前日期
    21. const currentDate:Date = new Date();
    22. const currentYear:number = currentDate.getFullYear();
    23. const currentMonth:number = currentDate.getMonth() + 1;
    24. const currentDay:number = currentDate.getDate();
    25. //获取年月日,未设置时默认当前日期
    26. const year:number = this.data.year || currentYear;
    27. const month:number = this.data.month || currentMonth;
    28. const day:number = this.data.day || currentDay;
    29. // 获取指定月份的总天数
    30. const totalDays:number = new Date(year, month, 0).getDate();
    31. // 获取指定月份第一天是星期几
    32. let firstDayOfWeek:number = new Date(year, month - 1, 1).getDay();
    33. //构建周期偏移实际数据
    34. let total = firstDayOfWeek + totalDays;
    35. // Math.ceil(total/7) -> 实际周期数
    36. total = Math.ceil(total/7)*7;
    37. // 定义存放日历数据的数组
    38. const calendarData:number[][] = [];
    39. // 初始化一个星期的数组
    40. let week:number[] = [];
    41. let num:number =0;
    42. for(let i=1;i<=total;i+=7){
    43. week = [];
    44. for(let j=0;j<7;j++){
    45. num = i-firstDayOfWeek +j
    46. week.push(num<0 || num>totalDays? 0: num)
    47. }
    48. calendarData.push(week);
    49. }
    50. // 将生成的日历数据更新到页面数据中,使页面重新渲染
    51. this.setData({
    52. year: year,
    53. month: month,
    54. day:day,
    55. calendarData: calendarData as any,
    56. selectedDay:this.data.selectedDay,
    57. today: year==currentYear && month==currentMonth && day==currentDay,
    58. });
    59. },
    60. selectDate(event: any){
    61. //console.log(event);
    62. this.setData({
    63. selectedDay: event.currentTarget.dataset.value,
    64. });
    65. },
    66. prevMonth(event: any){
    67. this.data.month -= 1;
    68. if(this.data.month==0){
    69. this.data.year -= 1;
    70. this.data.month = 12;
    71. }
    72. this.data.selectedDay=0;
    73. this.renderCalendar();
    74. },
    75. nextMonth(event: any){
    76. this.data.month += 1;
    77. if(this.data.month==13){
    78. this.data.year += 1;
    79. this.data.month = 1;
    80. }
    81. this.data.selectedDay=0;
    82. this.renderCalendar();
    83. },
    84. locateThisMonth(event: any){
    85. this.data.year=0;
    86. this.data.month=0;
    87. this.data.day=0;
    88. this.data.selectedDay=0;
    89. this.renderCalendar();
    90. },

    4.效果

  • 相关阅读:
    C/C++内存管理
    河南分销商城小程序开发跟分销系统区别是什么?
    【蓝桥杯Web】第十三届蓝桥杯(Web 应用开发)省赛真题
    install flash_atten
    剑指 Offer 10- II. 青蛙跳台阶问题
    idea Springboot 教师标识管理系统开发mysql数据库web结构java编程计算机网页源码maven项目
    cloc工具统计代码量
    八股乱背,力扣不会!下辈子远离计算机
    Redis集群搭建
    C++ Qt TCP协议,处理粘包、拆包问题,加上数据头来处理
  • 原文地址:https://blog.csdn.net/z329600208z/article/details/140085775