小程序自定义时间弹框组件

小程序利用和 自定义时间选择组件

  1. 在component文件中新建picker组件

  2. html页面代码如下

  3. `



    {{ popupText }}

    请选择用车时间
    取消

    {{less.name}} {{less.name}} {{less.name}} 确定

`

4.wxss页面的代码如下

/* component/picker/picker.wxss */

/**picker**/
.pick-box {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
}
.active-style {
 border-radius:15rpx;
 position: relative;
 top: 3rpx;
 left: 3rpx;
 background: rgb(39, 90, 70,.5);
}
.picker-box {
  position: fixed;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 10000
}
.picker-style {
  position: absolute;
  z-index: 10001;
  background: white;
  bottom: 40rpx;
  bottom: -100%;
  width: 90%;
  left: 5%;
 border-radius: 30rpx;
}
.picker-line{
  display: flex;
  justify-content: center;
  align-items: center;
}
.picker-but {
 font-size: 36rpx;
 color: #11141A;
  box-sizing: border-box;
  padding: 20px 20rpx;
  padding-bottom: 0rpx;
  display: flex;
  justify-content: space-between;
}
.picker-but2{
width:646rpx;
margin: auto;
margin-top: 20rpx;
height:92rpx;
color: #fff;
line-height: 92rpx;
text-align: center;
background:rgba(7,202,123,1);
box-shadow:0rpx 4rpx 10rpx 0rpx rgba(17,20,26,0.1);
border-radius:15rpx;
}
.reminder-popup{
  width: 452rpx;
  position: absolute;
top: -80rpx;
left: 60rpx;
background: #fff;
padding: 24rpx 16rpx;
box-sizing: border-box;
font-size: 24rpx;
border-radius: 20rpx;
box-shadow:0px 4rpx 8rpx 0px rgba(0,0,0,0.1);
}
.reminder{
  margin-left: 20rpx;
  display: inline-block;
  width: 20rpx;
  height: 20rpx;
  line-height: 20rpx;
  border-radius: 50%;
  background: rgba(7,202,123,1);
}
.picker-but view {
  display: flex;
  box-sizing: border-box;
  font-size: 36rpx;
  justify-content: center;
  align-items: center;
  height: 50rpx;
  /* width: 100rpx; */
}

5.逻辑层的代码如下

const app = getApp()

Component({
  properties: {
    pickData: {
      type: Object,
      value: {},
      observer: function (newVal) {
        if (typeof newVal === 'object') {
          this.setData({ listData: Object.values(newVal) })
        }
      }
    },
    popupText: {
      type: String,
      value: "预约用车: 司机将会在规定时间抵达上车点,您也可联系师傅提前用车。"
    },
    sureStyle: {
      type: String,
      value: ''
    },
    cancelStyle: {
      type: String,
      value: ''
    },
    openOrColse: {
      type: Boolean,
      value: false,
      observer: function (newVal) {
        if (String(newVal) === 'true') {
          this.setData({ popupTextShow: true })
          // this._openClosePicker()
        }
      }
    },
    open: {
      type: Boolean,
      value: false,
      observer: function (newVal) {
        if (String(newVal) === 'true') {
          this.setData({ isOpen: true })
          this._openClosePicker()
        }
      }
    },
    maskStyle: {
      type: String,
      value: ''
    },
    indicatorStyle: {
      type: String,
      value: ''
    },
  },
  data: {
    popupTextShow: false,
    isOpen: false,
    pickerBoxAnimation: {},
    pickerAnimation: {},
    pickeDate: [],
    listData: [],
    value: [],
    info: {
      H: '',
      W: ''
    }
  },
  attached() {
    this._getScreen()
  },
  methods: {
    //倒计时3秒 利用counting来判断是否正在进行倒计时,避免重复
    _countDown(that, count) {
      console.log(this.data.timeCountDownTop)
      console.log(this.data.popupTextShow)
      if (!this.data.popupTextShow) {
        count = 0
        that.setData({
          timeCountDownTop: '0'
        })
        return
      }
      if (count == 0) {
        console.log('在处理数据吗')
        that.setData({
          timeCountDownTop: '0',
          popupTextShow: false
        })
        return;
      }
      that.setData({
        popupTextShow: true,
        timeCountDownTop: count,
      })
      setTimeout(function () {
        count--;
        that._countDown(that, count);
      }, 1000);
    },
    _reminder() {
      let that = this
      this.setData({ popupTextShow: !this.data.popupTextShow })
      that._countDown(that, 4);
      this.triggerEvent('reminder', this.data.popupTextShow)

    },
    _getScreen() {
      let that = this
      wx.getSystemInfo({
        success: function (res) {
          let H = res.windowHeight
          let W = res.windowWidth
          const { info } = that.data
          info.H = H
          info.W = W
          that.setData({ info })
        }
      })
    },
    _closePicker() {
      let { pickeDate } = this.data
      this.triggerEvent('close', pickeDate)
      this.setData({ isOpen: false })
      this._openClosePicker(1)
    },
    _bindChange(e) {
      this.setData({
        pickeDate: e.detail
      })
      var pickeDate = e.detail
      this.triggerEvent('colChange', e.detail)
    },
    // _bindpickend(e){
    //   console.log(e)
    //   this.triggerEvent('bindEnd', e.detail)
    // },
    _surePicker() {
      let { pickeDate } = this.data
      if (JSON.stringify(pickeDate) === '[]') {
        pickeDate = [0, 0, 0]
      }
      console.log(pickeDate)
      this.triggerEvent('sure', pickeDate)
      this.setData({ isOpen: false })
      this._openClosePicker(1)
    },
    _openClosePicker(flag) {
      let animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })
      let animationBox = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })
      if (flag === 1) {
        animation.bottom('-100%').step()
        animationBox.bottom('-100%').step()
      } else {
        animation.bottom(0).step()
        animationBox.bottom(20).step()
      }
      this.setData({
        pickerBoxAnimation: animation.export(),
        pickerAnimation: animationBox.export()
      })
    }
  }
})

6.在需要用到组件的页面
引入组件在json文件中 “picker”: “/component/picker/picker”

{{callCar_nowTime.name?callCar_nowTime.name:'预约时间'}} 在以上对应的绑定方法和属性在js层进行处理 data:{ _popupText: '打车', //在时间上还有个温馨提示 callCarPicker: {}, //这是渲染的时间列表 openPicker: false, openPopup: false, value: '' }, onReady: function () { this.setData({ callCarPicker: this.initTimePicker(), //初始化数据根据自己需要的时间形式进行处理 indicatorStyle: 'red' }) }, 以下三个方法就是对应的这个时间组件需要的数据 ``` //初始化时间选择器 initTimePicker: function () { let nowTime = this.getAbsTime(new Date()); nowTime.setMinutes(nowTime.getMinutes() + 30); //预约30分钟后 let day = nowTime.getDate(); let hour = nowTime.getHours(); let min = nowTime.getMinutes(); let minArr = []; let hourArr = []; let dayArr = []; let minIndex = parseInt(min / 10); if (minIndex < 5) { for (let i = minIndex + 1; i <= 5; i++) { minArr.push({ value: i * 10, name: i * 10 + '分' }) } for (let i = 0; i < 24; i++) { if (i >= hour) { hourArr.push({ name: i + '点', value: i }); } } for (let i = 0; i < 3; i++) { dayArr.push({ // name: (nowTime.getMonth() + 1) + '月' + nowTime.getDate() + '日', name: i == 0 ? '今天' : i == 1 ? '明天' : '后天', value: Date.parse(nowTime) }); nowTime.setDate(nowTime.getDate() + 1); }
} else {
  minArr = [{
    name: '0分',
    value: 0
  }, {
    name: '10分',
    value: 10
  }, {
    name: '20分',
    value: 20
  }, {
    name: '30分',
    value: 30
  }, {
    name: '40分',
    value: 40
  }, {
    name: '50分',
    value: 50
  }];
  for (let i = 0; i < 24; i++) {
    if (i > hour) {
      hourArr.push({
        name: i + '点',
        value: i
      });
    }
  }
  if (hourArr.length === 0) {
    for (let i = 0; i < 24; i++) {
      hourArr.push({
        name: i + '点',
        val: i
      });
    }
    for (let i = 0; i < 3; i++) {
      nowTime.setDate(nowTime.getDate() + 1);
      dayArr.push({
        name: i == 0 ? '今天' : i == 1 ? '明天' : '后天',
        // name: (nowTime.getMonth() + 1) + '月' + nowTime.getDate() + '日',
        value: Date.parse(nowTime)
      });
    }
  } else {
    for (let i = 0; i < 3; i++) {
      dayArr.push({
        name: i == 0 ? '今天' : i == 1 ? '明天' : '后天',
        // name: (nowTime.getMonth() + 1) + '月' + nowTime.getDate() + '日',
        value: Date.parse(nowTime)
      });
      nowTime.setDate(nowTime.getDate() + 1);
    }
  }
}
return [dayArr, hourArr, minArr];

},
// 获取时分
getDayMin: function () {
let minArr = [];
let hourArr = [];
for (let i = 0; i < 24; i++) {
hourArr.push({
name: i + ‘点’,
value: i
});
}
for (let i = 0; i < 6; i++) {
minArr.push({
name: (i * 10) + ‘分’,
value: i * 10
});
}
return [hourArr, minArr];
},

//获取东八区时间
getAbsTime: function () {
let currentZoneTime = new Date();
let currentZoneHours = currentZoneTime.getHours();
let offsetZone = currentZoneTime.getTimezoneOffset() / 60;

if (offsetZone > 0) {
  // 大于0的是西区(西区晚) 西区应该用时区绝对值加京八区 重新设置时间
  // 西区时间比东区时间晚 所以加时区间隔
  offsetZone = offsetZone + 8;
  currentZoneTime.setHours(currentZoneHours - offsetZone)
} else {
  // 小于0的是东区(东区早)  东区时间直接跟京八区相加
  offsetZone += 8;
  currentZoneTime.setHours(currentZoneHours + offsetZone);
}
return currentZoneTime

},

以下分别是绑定的方法

//打车-改变预约时间
_colChange: function (e) {
// console.log(e)
let picker = this.initTimePicker();
var that = this;
let val = e.detail.value[0];
console.log(val, ‘看数据’)
if (val == 0) {
that.setData({
callCarPicker: picker
})
} else {
console.log(‘走这边了吗’)
that.setData({
‘callCarPicker[1]’: this.getDayMin()[0],
‘callCarPicker[2]’: this.getDayMin()[1],
})
// console.log(this.getDayMin()[0])
console.log(this.data.callCarPicker)
}

},
// 确认选中时间
_sure(e) {
var that = this;
let val = e.detail.value ? e.detail.value : e.detail;
let picker = that.data.callCarPicker;
console.log(picker)
this.setData({
openPicker: false,
callCar_nowTime: {
name: picker[0][val[0]].name + ’ ’ + (picker[1][val[1]].value > 9 ? picker[1][val[1]].value : ‘0’ + picker[1][val[1]].value) + ‘点’ + (picker[2][val[2]].value > 9 ? picker[2][val[2]].value : ‘0’ + picker[2][val[2]].value) + ‘分’,
value: new Date(picker[0][val[0]].value).setHours(picker[1][val[1]].value, picker[2][val[2]].value)
}
});
console.log(this.data.callCar_nowTime)
},
// 取消选择时间
_close(e) {
console.log(e.detail)
this.setData({
openPicker: false
})
console.log(‘点击了取消’)
},
// 打开时间选择器弹框
_openPick() {
let picker = this.initTimePicker();
this.setData({
openPicker: true,
callCarPicker: picker
})
},

最终呈现效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200110102317241.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzgzMTMwMg==,size_16,color_FFFFFF,t_70)
点击小绿色点点会出现一个温馨提示弹窗
不过这个有个不好之处就是我选择了时间点击了确认之后再次点开我的时间还是这个样子展示,不是显示我之前选中的时间,希望哪位大佬能帮我瞅瞅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值