经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序日历弹窗选择器代码实例
来源:jb51  时间:2019/5/10 8:38:47  对本文有异议

应公司需求,写了一个弹窗日历选择器,感觉用着还不错,封装了一下,分享给大家,希望大家有什么意见可以指出来相互交流共同改进!

先上一个效果图:(当天日期为2018-4-18)

时间改为5月份的效果图:

直接上代码:

wxml:

  1. <view class="weui-cells weui-cells_after-title" style='margin-top:100rpx;'>
  2. <view class="weui-cell weui-cell_access" hover-class="weui-cell_active" catchtap='showModalBtn'>
  3. <view class="weui-cell__bd">选择时间</view>
  4. <view class="weui-cell__ft weui-cell__ft_in-access">{{chooseDate}}</view>
  5. </view>
  6. </view>
  7. <view class="modal-mask" bindtap="hideModal" catchtouchmove="preventTouchMove" hidden="{{showModal}}"></view>
  8. <view class="modal-dialog" hidden="{{showModal}}">
  9. <view class='modalBox'>
  10. <view class='box'>
  11. <view class='calendarBox'>
  12. <view class='calendarTitle'>
  13. 当前月份:
  14. <text style='font-size:46rpx;'>{{thisMonth}}</text>
  15. </view>
  16. <block wx:for="{{week}}" wx:key="item">
  17. <view class="week">{{week[index]}}</view>
  18. </block>
  19. <block wx:for="{{weekNum}}" wx:key="item">
  20. <view class="week" style="border-bottom:0;color:transparent">0</view>
  21. </block>
  22. <block wx:for="{{dayList}}" wx:key="item">
  23. <view class='week' style="border-bottom:0;background-color:{{dayIndex>index?'#f1f1f1':(tapThis==index?'#1296db':'')}};color:{{tapThis==index?'white':''}}" catchtap="chooseDate" data-index="{{index}}" data-value="{{item}}">{{item}}</view>
  24. </block>
  25. </view>
  26. </view>
  27. </view>
  28. </view>

wxss:

  1. .modalBox{
  2. width: 100%;
  3. font-size: 32rpx;
  4. }
  5. .box{
  6. margin: 0 auto;
  7. width: 630rpx;
  8. }
  9. .calendarTitle{
  10. /* margin: 0 auto;
  11. width: 630rpx; */
  12. width: 100%;
  13. height: 80rpx;
  14. line-height: 80rpx;
  15. text-align: center;
  16. border-bottom: 1rpx solid #ddd;
  17. }
  18. .calendarBox{
  19. /* width: 630rpx; */
  20. width:100%;
  21. margin: 0 auto;
  22. border:1rpx solid #ddd;
  23. }
  24. .week{
  25. display: inline-block;
  26. width:90rpx;
  27. height: 80rpx;
  28. text-align: center;
  29. line-height: 80rpx;
  30. border-bottom: 1rpx solid #ddd;
  31. }
  32. .dateBtn{
  33. width:100%;
  34. height: 80rpx;
  35. display: flex;
  36. justify-content: space-between;
  37. margin-top: 20rpx;
  38. }
  39. .dateBtn>button{
  40. width: 45%;
  41. height: 100%;
  42. display:flex;
  43. justify-content: center;
  44. align-items: center;
  45. margin: 0;
  46. font-size: 36rpx;
  47. }
  48. /* 模态框 */
  49. .modal-mask {
  50. width: 100%;
  51. height: 100%;
  52. position: fixed;
  53. top: 0;
  54. left: 0;
  55. background: #000;
  56. opacity: 0.5;
  57. overflow: hidden;
  58. z-index: 9000;
  59. }
  60. .modal-dialog {
  61. width: 85%;
  62. padding: 100rpx 30rpx 30rpx 30rpx;
  63. overflow: hidden;
  64. position: fixed;
  65. top: 20%;
  66. left: 0;
  67. right: 0;
  68. margin: 0 auto;
  69. z-index: 9999;
  70. background: rgba(255, 255, 255, 1);
  71. border-radius: 5rpx;
  72. }

js:

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. showModal:true,
  7. weekLength:7,
  8. week:["日","一","二","三","四","五","六"],
  9. dayList:[],
  10. weekNum:0,
  11. tapThis:0,
  12. thisMonth:0,
  13. thisYear:0,
  14. dayIndex:0,
  15. chooseDate:"",
  16. },
  17. getWeek(year,month,day){
  18. var that = this;
  19. var d = new Date();
  20. d.setFullYear(year);
  21. d.setMonth(month-1);
  22. d.setDate(1);
  23. var n = d.getDay();
  24. var arr =[];
  25. var Index=0;
  26. var dayN=1;
  27. for(var i = 0; i<day; i++){
  28. arr.push(dayN++);
  29. }
  30. var now = new Date();
  31. var nowYear = now.getFullYear();
  32. var nowMonth = now.getMonth()+1;
  33. var nowDay = now.getDate();
  34. var val = 1;
  35. if(year==nowYear){
  36. if(month==nowMonth){
  37. Index=arr.indexOf(nowDay);
  38. console.log(Index);
  39. val = nowDay;
  40. }
  41. }
  42. that.setData({
  43. weekNum:n,
  44. dayList:arr,
  45. dayIndex:Index,
  46. tapThis:Index,
  47. thisMonth:month,
  48. thisYear:year,
  49. chooseDate:year+"-"+month+"-"+val,
  50. })
  51. },
  52. chooseDate(e){
  53. var that = this;
  54. var n = e.currentTarget.dataset.index;
  55. var val = e.currentTarget.dataset.value;
  56. console.log(n);
  57. if(n>=that.data.dayIndex){
  58. that.setData({
  59. tapThis:n,
  60. chooseDate:that.data.thisYear+"-"+that.data.thisMonth+"-"+val,
  61. showModal:true,
  62. })
  63. }
  64. },
  65. /**
  66. * 弹出框蒙层截断touchmove事件
  67. */
  68. preventTouchMove: function () {
  69. },
  70. /**
  71. * 隐藏模态对话框
  72. */
  73. hideModal() {
  74. var that = this;
  75. that.setData({
  76. showModal: true,
  77. })
  78. },
  79. showModalBtn(){
  80. var that = this;
  81. that.setData({
  82. showModal:false
  83. })
  84. },
  85. /**
  86. * 生命周期函数--监听页面加载
  87. */
  88. onLoad: function (e) {
  89. var that = this;
  90. that.getWeek("2018","04","31"); //使用方法: 在此函数内传入年、月、日(此月的天数)即可。
  91. }
  92. })

代码设计思路:

1、此代码是符合我们公司实际情况定制的,选择哪个月份,需要传递哪个月份的参数,比如我要2018-04的日历选择器,那么我需要在 getWeek() 中传递年,月,日(此月的总天数)作为参数,代码会自动计算出当月的一号是星期几并且排版好!

如果不知道此月的天数 ,这里还提供如下代码方便各位码友计算出各个月份的天数

  1. getDayNum(year,month){ //传入参数年份 和 要计算的月份, 可以为字符串,也可以为数字。
  2. var that = this;
  3. var d = new Date();
  4. d.setFullYear(year);
  5. d.setMonth(month);
  6. d.setDate(0);
  7. console.log(d.getDate()); //d.getDate() 即为此月的总天数!
  8. },

2、具体思路就是:根据传递的参数计算出当月的第一天为星期几,然后从星期几开始排列,通过循环{{总天数}}次,让日期循环出来。然后再获取当前日期,判断日历弹窗与当前月份是否吻合,如果吻合,就要将在当前日期之前的日期都设置为不可点击状态。然后就是点击获取值,整个日历流程完毕。

以上所述是小编给大家介绍的微信小程序日历弹窗选择器详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号