微信小程序日历弹框组件

不念不忘少年蓝@ 2022-12-09 15:58 432阅读 0赞

参考文章:https://blogai.cn/posts/46

calendar.json

  1. {
  2. "usingComponents": { },
  3. "disableScroll": true
  4. }

calendar.wxml

  1. <view class="titleDate">
  2. <button class="cu-btn" bindtap="lastMonth">
  3. <text class="cuIcon-back"></text>
  4. </button>
  5. {
  6. {currentYear}} - {
  7. {currentMonth+1}}
  8. <button class="cu-btn" bindtap="nextMonth">
  9. <text class="cuIcon-right"></text>
  10. </button>
  11. </view>
  12. <view class="title">
  13. <view wx:for='{
  14. {weeks}}' wx:key='index'>{
  15. {item}}</view>
  16. </view>
  17. <view class="calender {
  18. {anmation? 'animation-slide-skew':''}}">
  19. <view wx:for='{
  20. {monthNum+week}}' class="cal-body {
  21. {index-week+1==currentDay&&nowMonth==currentMonth&&nowYear==currentYear?'now':''}}" wx:key='index' data-day="{
  22. {index-week+1}}" bindtap="choseDay">
  23. <view class="{
  24. {index-week+1>0?'cal-text ':''}} {
  25. {choseDay==index-week+1&&currentMonth==choseMonth?'chose':''}}">{
  26. {index-week+1 >0?index-week+1:''}}</view>
  27. </view>
  28. </view>
  29. <view>
  30. </view>
  31. <picker-view wx:if='{
  32. {datePick}}' class="picker" mask-class='mask' indicator-class='indicator' bindchange="bindChange">
  33. <picker-view-column class='column'>
  34. <view wx:for='{
  35. {24}}' style="line-height:50rpx;" wx:key='index'>{
  36. {index
  37. <10? '0'+index:index}}</view>
  38. </picker-view-column>
  39. <picker-view-column class='column'>
  40. <view wx:for='{
  41. {60}}' style="line-height:50rpx;" wx:key='imdex'>{
  42. {index
  43. <10? '0'+index:index}}</view>
  44. </picker-view-column>
  45. </picker-view>
  46. <view>您选择的是:{
  47. {choseDate}} {
  48. {choseHours}} : {
  49. {choseMin}}</view>
  50. <view>
  51. <view class="toToday" bindtap="initDay"></view>
  52. </view>

calendar.wxss

  1. /* pages/sigle/calendar/calendar.wxss */
  2. page {
  3. background: #fff;
  4. }
  5. .datePick {
  6. margin-top: 20rpx;
  7. }
  8. .indicator {
  9. height: 50rpx;
  10. width: 80%;
  11. margin-left: 10%;
  12. border-radius: 10rpx;
  13. }
  14. .column {
  15. text-align: center;
  16. color: cornflowerblue;
  17. font-weight: bold;
  18. font-size: 34rpx;
  19. }
  20. .picker {
  21. height: 150rpx;
  22. width: 80%;
  23. margin: 0 auto;
  24. }
  25. .text {
  26. width: 150px;
  27. height: 60px;
  28. margin-left: 20px;
  29. }
  30. .move {
  31. width: 100px;
  32. height: 60px;
  33. position: absolute;
  34. right: 0;
  35. transform: translateX(100px);
  36. background: rgb(245, 51, 93);
  37. }
  38. .calender {
  39. margin: 0 auto;
  40. width: 90%;
  41. height: 600rpx;
  42. color: rgb(68, 68, 68);
  43. display: grid;
  44. grid-template-columns: repeat(7, 1fr);
  45. grid-column-gap: 20rpx;
  46. transition: height 1s;
  47. }
  48. .titleDate {
  49. display: flex;
  50. justify-content: center;
  51. align-items: center;
  52. font-size: 32rpx;
  53. font-weight: bold;
  54. }
  55. .cu-btn {
  56. display: flex;
  57. justify-content: center;
  58. align-items: center;
  59. width: 60rpx !important;
  60. }
  61. .title {
  62. border-bottom: 4rpx solid cornflowerblue;
  63. font-size: 30rpx;
  64. font-weight: bold;
  65. height: 80rpx;
  66. line-height: 80rpx;
  67. text-align: center;
  68. margin: 0 auto;
  69. width: 90%;
  70. color: rgb(93, 98, 136);
  71. display: grid;
  72. grid-template-columns: repeat(7, 1fr);
  73. grid-column-gap: 20rpx;
  74. }
  75. .cal-body {
  76. width: 80rpx;
  77. margin-top: 20rpx;
  78. font-size: 30rpx;
  79. font-weight: bold;
  80. height: 80rpx;
  81. text-align: center;
  82. }
  83. .cal-body .cal-text {
  84. line-height: 80rpx;
  85. border-bottom: 1rpx dashed rgba(100, 148, 237, 0.747);
  86. }
  87. .now {
  88. background: cornflowerblue;
  89. color: #fff;
  90. font-size: 32rpx;
  91. font-weight: bold;
  92. border-radius: 50%;
  93. box-shadow: 2rpx 2rpx 12rpx cornflowerblue;
  94. }
  95. .chose {
  96. background: rgb(20, 20, 20);
  97. color: #fff;
  98. font-size: 32rpx;
  99. font-weight: bold;
  100. border-radius: 14rpx;
  101. box-shadow: 2rpx 2rpx 12rpx rgb(122, 122, 122);
  102. }
  103. .toToday {
  104. text-align: center;
  105. width: 100rpx;
  106. height: 100rpx;
  107. border-radius: 50%;
  108. line-height: 100rpx;
  109. position: absolute;
  110. bottom: 30rpx;
  111. font-weight: bold;
  112. right: 30rpx;
  113. color: #fff;
  114. background: cornflowerblue;
  115. box-shadow: 2rpx 2rpx 20rpx rgba(123, 154, 211, 0.808);
  116. }

calendar.js

  1. const weeks = ['日', '一', '二', '三', '四', '五', '六']
  2. function getWeelDay(year, month, day) {
  3. return new Date(year)
  4. }
  5. Page({
  6. /** * 页面的初始数据 */
  7. data: {
  8. weeks: ['日', '一', '二', '三', '四', '五', '六'],
  9. datePick: false
  10. },
  11. start_(e) {
  12. this.setData({
  13. Start: e.touches[0].pageX,
  14. animation: false
  15. })
  16. console.log('start', this.data.Start)
  17. },
  18. move_(e) {
  19. let x = e.touches[0].pageX - this.data.Start
  20. if (x > -100 && x < 0) {
  21. this.setData({
  22. movex: x
  23. })
  24. }
  25. this.setData({
  26. show: e.touches[0].pageX - this.data.Start > 0 ? 'hide' : 'show'
  27. })
  28. console.log('move', x)
  29. },
  30. end_(e) {
  31. if (this.data.show == 'show') {
  32. this.setData({
  33. movex: -100,
  34. animation: true
  35. })
  36. } else {
  37. this.setData({
  38. movex: 0,
  39. animation: true
  40. })
  41. }
  42. // this.setData({
  43. // ListTouchDirection: null
  44. // })
  45. },
  46. /** * 生命周期函数--监听页面加载 */
  47. onLoad: function(options) {
  48. // let date = new Date()
  49. // let currentYear = date.getFullYear(),
  50. // currentMonth = date.getMonth(),
  51. // currentDay = date.getDate()
  52. // let monthNum = new Date(currentYear,currentMonth+1,0).getDate()
  53. // let week = new Date(currentYear, currentMonth, 1).getDay()
  54. // this.setData({
  55. // currentYear:currentYear,
  56. // currentMonth:currentMonth,
  57. // currentDay:currentDay,
  58. // monthNum: monthNum,
  59. // week: week,
  60. // nowYear: currentYear,
  61. // nowMonth: currentMonth,
  62. // nowDay: currentDay
  63. // })
  64. this.initDay()
  65. },
  66. initDay() {
  67. //获取当前选择的年月日
  68. let date = new Date()
  69. let currentYear = date.getFullYear(),
  70. currentMonth = date.getMonth(),
  71. currentDay = date.getDate()
  72. //获取当年,当月的天数 getDate(),此时month+1
  73. let monthNum = new Date(currentYear, currentMonth + 1, 0).getDate()
  74. //获取该月1号是周几,注意此时month不加1
  75. let week = new Date(currentYear, currentMonth, 1).getDay()
  76. this.setData({
  77. currentYear: currentYear,
  78. currentMonth: currentMonth,
  79. currentDay: currentDay,
  80. monthNum: monthNum,
  81. week: week,
  82. nowYear: currentYear, //这里的now代表今天对应的日期,存在data中,点击回到今天时再从data中取出
  83. nowMonth: currentMonth,
  84. nowDay: currentDay
  85. })
  86. },
  87. /** * 生命周期函数--监听页面初次渲染完成 */
  88. onReady: function() {
  89. },
  90. bindChange(e) {
  91. console.log(e.detail.value[0], e.detail.value[1])
  92. this.setData({
  93. choseHours: e.detail.value[0],
  94. choseMin: e.detail.value[1]
  95. })
  96. },
  97. choseDay(e) {
  98. let index_ = e.currentTarget.dataset.day
  99. if (index_ > 0) {
  100. if (this.data.choseMonth == this.data.currentMonth && this.data.choseDay == index_) {
  101. this.setData({
  102. choseDay: '',
  103. choseYear: '',
  104. choseMonth: '',
  105. choseDay: '',
  106. datePick: false
  107. })
  108. } else {
  109. console.log(this.data.currentYear, this.data.currentMonth, index_)
  110. let choseDate = this.data.currentYear + '' + this.data.currentMonth + '' + index_
  111. this.setData({
  112. choseDate: choseDate,
  113. choseYear: this.data.currentYear,
  114. choseMonth: this.data.currentMonth,
  115. choseDay: index_,
  116. datePick: true
  117. })
  118. }
  119. }
  120. },
  121. lastMonth() {
  122. this.setData({
  123. anmation: true
  124. })
  125. //计算余数,除以12之后的余数便是要减的月份数
  126. let Remainder = this.data.currentMonth % 12;
  127. if (this.data.currentMonth < 1) {
  128. //parseInt(this.data.currentMonth / 12) 计算整数,得到的值就是要减的年
  129. //如果当前选择的月份是负数,需要减整数后再-1
  130. var currentYear = this.data.currentYear - 1 - parseInt(this.data.currentMonth / 12)
  131. var currentMonth = 12 - Math.abs(Remainder) - 1
  132. } else {
  133. var currentYear = this.data.currentYear + parseInt(this.data.currentMonth / 12)
  134. var currentMonth = Remainder - 1
  135. }
  136. let monthInt = parseInt(this.data.currentMonth / 12)
  137. // let currentDay = this.data.currentDay;
  138. let monthNum = new Date(currentYear, currentMonth + 1, 0).getDate()
  139. let week = new Date(currentYear, currentMonth, 1).getDay()
  140. this.setData({
  141. currentYear: currentYear,
  142. currentMonth: currentMonth,
  143. monthNum: monthNum,
  144. week: week
  145. })
  146. setTimeout(() => {
  147. this.setData({
  148. anmation: false
  149. })
  150. }, 1000)
  151. console.log(currentYear, currentMonth, week, monthNum)
  152. },
  153. nextMonth() {
  154. this.setData({
  155. anmation: true
  156. })
  157. let Remainder = (this.data.currentMonth + 1) % 12;
  158. if (Remainder < 0) {
  159. var currentYear = this.data.currentYear - 1 - parseInt(Remainder / 12)
  160. var currentMonth = 12 - Math.abs(Remainder)
  161. } else {
  162. console.log(Remainder)
  163. var currentYear = Remainder == 0 ? this.data.currentYear + 1 : this.data.currentYear + parseInt(Remainder / 12)
  164. var currentMonth = Remainder
  165. }
  166. let monthInt = parseInt(this.data.currentMonth / 12)
  167. // let currentDay = this.data.currentDay;
  168. let monthNum = new Date(currentYear, currentMonth + 1, 0).getDate()
  169. let week = new Date(currentYear, currentMonth, 1).getDay()
  170. this.setData({
  171. currentYear: currentYear,
  172. currentMonth: currentMonth,
  173. monthNum: monthNum,
  174. week: week
  175. })
  176. setTimeout(() => {
  177. this.setData({
  178. anmation: false
  179. })
  180. }, 1000)
  181. console.log(currentYear, currentMonth, week, monthNum)
  182. }
  183. })

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读