自定义 uniapp 小程序端双击事件

565 阅读1分钟

近日使用 uniapp 写小程序的代码时,需要用到双击顶部回到顶部的功能,才得知小程序原来是不可双击的,所以就上网咨询,得知可利用时间戳实现双击事件,仅此记录...

talk is cheap, show me the code

<template>
    <view @click="backTop">
      ...
    </view>
</template><script>
  export default {
    data() {
        return {
            lastClickTime: 0
        }
    },
    methods: {
      backTop() {
        let timestamp = Date.parse(new Date()); // 获取当前时间戳
        let result = timestamp - this.lastClickTime
        if(result <= 300) { // 自定义间隔
          uni.pageScrollTo({
            scrollTop: 0,
            duration://动画时间
            success,
            fail,
            complete
          })
        }
        this.lastClickTime = timestamp
      }
    }
  }
</script>