mpvue vue 长按录音,上滑取消,下拉恢复

 

 

需求: mpvue 开发小程序实现 按住录音上滑取消,下拉恢复

知识点: touchstart touchend touchmove 事件

效果:

 

<button
      @touchstart="touchstart"
      @touchend="touchend"
      @touchmove="touchmove"
    >{{btRecoding?'松开结束':'按住说话'}}</button>
    <div class="shade" v-if="btRecoding" catchtouchmove="ture">
      <div class="blackBoxSpeak" v-if="blackBoxSpeak">
        <div>
          <img src="https://gitee.com/sanghongxv/blogImage/raw/master/ic_record.gif" />
        </div>
        <div class="blackBoxSpeakConent">手指上划,取消录制</div>
      </div>
      <div class="blackBoxPause" v-else>
        <img src="https://gitee.com/sanghongxv/blogImage/raw/master/ic_release_to_cancel.png" />
        <div class="blackBoxSpeakConent" style="background:red">松开手指,取消录制</div>
      </div>
    </div>

catchtouchmove="ture" 阻止页面滚动

<style lang="scss" scoped>
button{
  z-index: 999;
  position: absolute;
  bottom: 0;
}
.shade {
  z-index: 998;
  position: absolute;
  top: 0;
  width: 100vw;
  height: 100vh;
  img {
    width: 148rpx;
    height: 246rpx;
  }
  .blackBoxSpeak,
  .blackBoxPause {
    width: 343rpx;
    height: 343rpx;
    border-radius: 24rpx;
    background-color: rgba($color: #000000, $alpha: 0.7);
    margin: auto;
    padding: 32rpx;
    position: relative;
    text-align: center;
    top: 188rpx;
  }
  .blackBoxSpeakConent {
    position: absolute;
    bottom: 24rpx;
    color: #fff;
    text-align: center;
    font-size: 30rpx;
    padding: 12rpx 0;
    width: 85%;
    margin: auto;
    border-radius: 16rpx;
  }
}
</style>
<script>
export default {
  name: '',
  data() {
    return {
      loop:'',
      btRecoding:false,
      blackBoxSpeak:false,
      startY:'',
      timeOutEvent:0
    };
  },
  watch:{
  },
  mounted() {},
  methods: {
    // 手指开始触发
    touchstart(e) {
      let i = 1
      this.blackBoxSpeak = true
      this.startY = e.clientY
      // 每1秒打印一次(无实质用途)
      this.loop = setInterval(()=>{
        console.log(`${i++}`);           
      },1000)
      this.timeOutEvent = 0
      // 长按1000毫秒后执行
      this.timeOutEvent = setTimeout(()=>{
        this.startRecord()
      },1000)
      return false
    },
    // 长按超过1000毫秒
    startRecord(){
       // 调用录音方法
       this.btRecoding = true
    },
    // 短按
    startRecordClick(){
      // 长按没有超过1000毫秒
    },
    // 手指离开屏幕触发
    touchend() {
      console.log(`结束`);
      clearTimeout(this.loop)
      clearTimeout(this.timeOutEvent)
      if (_this.timeOutEvent == 0) {
        this.startRecordClick()
      } else {
        this.btRecoding = false
        // 长按结束调用保存录音或者其实事件
      } 
    },
    // 滑动触发
    touchmove(e) {
      const endY = e.clientY
      if(this.startY<endY){
        this.blackBoxSpeak = true
      } else {
        this.blackBoxSpeak = false 
      }
    }
  },
};
</script>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值