当前位置:   article > 正文

小程序 picker-view 自定义_微信小程序 picker-view

微信小程序 picker-view

序:接到一个控制选择器30分钟到120分钟的,使用系统自带的picker 的有点不符合,颜色不能自定义,这就很烦,需要亲自动手,也不见丰衣足食呀,没办法只能软饭硬吃啦!

效果图:
组件:

countdown.js

  1. const hours = ['00','01','02'];
  2. var minutes = []
  3. for (let i = 30; i <= 59; i++) {
  4. minutes.push(i)
  5. }
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. cancelText: {
  12. type: String,
  13. value: "取消"
  14. },
  15. confirmText: {
  16. type: String,
  17. value: "确定"
  18. },
  19. hour:{
  20. type: Number,
  21. value: 0
  22. },
  23. minute:{
  24. type: Number,
  25. value: 0
  26. },
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. hours,
  33. hour: 0,
  34. minutes,
  35. minute: 30,
  36. value: [0, 0],
  37. isDialog: false,
  38. },
  39. observers:{
  40. 'hour': function(hour) {
  41. minutes = [];
  42. let start = 0;
  43. let end = 59;
  44. if(hour == 0){
  45. start = 30;
  46. end = 59;
  47. }else if(hour == 1){
  48. start = 0;
  49. end = 59;
  50. }else{
  51. start = 0;
  52. end = 0;
  53. }
  54. for (let i = start; i <= end; i++) {
  55. if(i < 10){
  56. minutes.push('0'+i)
  57. }else{
  58. minutes.push(i)
  59. }
  60. }
  61. let val = this.data.value;
  62. this.setData({
  63. minutes: minutes,
  64. minute: minutes[val[1]],
  65. })
  66. }
  67. },
  68. /**
  69. * 组件的方法列表
  70. */
  71. methods: {
  72. // 监听选择器
  73. bindChange(e) {
  74. const val = e.detail.value;
  75. this.setData({
  76. value: val,
  77. hour: this.data.hours[val[0]],
  78. minute: this.data.minutes[val[1]],
  79. })
  80. },
  81. //隐藏弹框
  82. hideDialog(){
  83. let that = this;
  84. that.setData({
  85. isDialog: false,
  86. })
  87. },
  88. //展示弹框
  89. showDialog(){
  90. let that = this;
  91. that.setData({
  92. isDialog: true,
  93. })
  94. },
  95. //触发取消回调
  96. _cancelEvent(){
  97. this.triggerEvent("cancelCountDown");
  98. this.hideDialog()
  99. },
  100. //触发成功回调
  101. _confirmEvent(){
  102. let that = this;
  103. this.triggerEvent("confirmCountDown",{
  104. hour: that.data.hour,
  105. minute: that.data.minute,
  106. value: that.data.value,
  107. });
  108. },
  109. // 禁止弹窗时 屏幕滚动
  110. preventTouchMove(){
  111. return true;
  112. }
  113. }
  114. })

countdown.wxml

  1. <!--component/countdown/countdown.wxml-->
  2. <view class="{{isDialog ? 'dialog_isShow' : ''}}"
  3. catchtouchmove="preventTouchMove">
  4. <!-- 遮罩 -->
  5. <view class="dialog_mask" bindtap="_cancelEvent"></view>
  6. <view class="dialog_content">
  7. <view class="dialog_tle_flex">
  8. <view class="dialog_tle"></view>
  9. <view class="dialog_tle"></view>
  10. </view>
  11. <!-- 选择器 -->
  12. <picker-view class="dialog_picker" indicator-style="height:50px" value="{{value}}" bindchange="bindChange" mask-class="mask">
  13. <picker-view-column>
  14. <view wx:for="{{hours}}" wx:key="index" class="dialog_item {{index==value[0]?'dialog_item_active':''}}">{{item}}</view>
  15. </picker-view-column>
  16. <picker-view-column>
  17. <view wx:for="{{minutes}}" wx:key="index" class="dialog_item {{index==value[1]?'dialog_item_active':''}}">{{item}}</view>
  18. </picker-view-column>
  19. </picker-view>
  20. <!-- 按钮 -->
  21. <view class="dialog_flex">
  22. <view bindtap="_cancelEvent" class="btn" hover-class="btn_hover">{{cancelText}}</view>
  23. <view bindtap="_confirmEvent" class="btn" hover-class="btn_hover">{{confirmText}}</view>
  24. </view>
  25. </view>
  26. </view>

countdown.wxss

  1. /* component/countdown/countdown.wxss */
  2. .dialog_mask{display: none;opacity: 0;background-color: rgba(0, 0, 0, 0.68);width: 100%;position: fixed;left: 0;top: 0;bottom: 0; z-index:888;}
  3. .dialog_content{max-width: 480px;width: 100%;text-align: center;padding:20px;background-color: #F5EEEC;border-radius: 15px;color:#333;position: fixed; z-index: 999;left:-50%;transform: translate(-50%,-50%);top: 50%;box-sizing: border-box;opacity: 0;transition: all .3s ease-in-out;}
  4. .dialog_isShow .dialog_mask {display: block;opacity: 1;}
  5. .dialog_isShow .dialog_content {opacity: 1;z-index: 999;left:50%;}
  6. /* 时、分 */
  7. .dialog_content .dialog_tle_flex{width:200px;margin: 0 auto 10px;display: inline-flex;align-items: center;justify-content: space-around;}
  8. .dialog_content .dialog_tle{color: #190933;font-size: 22px;font-weight: 500;}
  9. /* 按钮 */
  10. .dialog_content .dialog_flex{display: flex;align-items: center;justify-content: space-around;margin-top: 15px;}
  11. .dialog_content .dialog_flex .btn{padding:8px 35px;background: linear-gradient(180deg, #FFDBD5 0%, #FEE4DF 100%);box-shadow: 0 2px 5px #f3b5a8, 0 -2px 5px #f5d7d1;border-radius: 20px;transition: all 0.3s ease-in-out; }
  12. .dialog_content .dialog_flex .btn_hover{background: linear-gradient(90deg, rgba(255, 244, 244, 0) 0%, #FFF4F4 50%, rgba(255, 244, 244, 0) 100%);}
  13. /* 选择器 */
  14. .dialog_content .dialog_picker{position: relative;width: 200px; height: 252px;margin: auto;font-size: 18px;background-color: #F5EEEC;border-top: 2px solid #EDCFC9;border-bottom: 2px solid #EDCFC9;box-sizing: border-box;}
  15. .dialog_content .mask {background: none;}
  16. .dialog_content .mask::after { position: absolute;content: '';top: 0;left: 0;right: 0;bottom: 0;width: 100%; height: 100%;background: linear-gradient(to bottom,
  17. rgba(245, 238, 236,.95) 0%,rgba(245, 238, 236,.5) 100px,
  18. rgba(245, 238, 236,0) 125px,
  19. rgba(245, 238, 236,.5) 150px,rgba(245, 238, 236,.95) 100%) no-repeat;}
  20. .dialog_content .dialog_item{line-height: 50px;color: #000;font-weight: 600;}
在pages中使用:
  1. xxx.json
  2. "usingComponents": {
  3. "countdown": "../component/countdown/countdown"
  4. }
  5. xxx.js
  6. onReady() {
  7. this.countdown = this.selectComponent('#countdown');
  8. },
  9. // 时间 => 自定义_打开选择器
  10. timeCustomTab(e){
  11. let that = this;
  12. that.countdown.showDialog();
  13. },
  14. // 定时 => 自定义_确定
  15. _confirmPicker(e){
  16. console.log("确定",e.detail)
  17. this.countdown.hideDialog();
  18. },
  19. xxx.wxml
  20. <view class="time_tab" hover-class="time_tab_active" bindtap="timeCustomTab">自定义时间</view>
  21. <!-- 自定义选择器 -->
  22. <countdown
  23. id="countdown"
  24. bind:changePicker="_changePicker"
  25. bind:confirmCountDown="_confirmPicker"
  26. >
  27. </countdown>

详细代码已贴出,乖乖!有问题请评论,看到了会尽量帮你解决!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/326196
推荐阅读
相关标签
  

闽ICP备14008679号