uniapp左滑删除

触摸事件(touch)会在用户手指放在屏幕上面的时候、在屏幕上滑动的时候或者是从屏幕上移开的时候出发。下面具体说明:

touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发。
touchmove事件:当手指在屏幕上滑动的时候连续地触发。
touchend事件:当手指从屏幕上离开的时候触发。

除了常见的DOM属性,触摸事件还包含下面三个用于跟踪触摸的属性。也就是存在于事件的默认参数event中。
  touches:表示当前跟踪的触摸操作的touch对象的数组。
  targetTouches:特定于事件目标的Touch对象的数组。
  changeTouches:表示自上次触摸以来发生了什么改变的Touch对象的数组。
  
  每个Touch对象包含的属性如下。
  clientX:触摸目标在视口中的x坐标。
  clientY:触摸目标在视口中的y坐标。
  identifier:标识触摸的唯一ID。
  pageX:触摸目标在页面中的x坐标。
  pageY:触摸目标在页面中的y坐标。
  screenX:触摸目标在屏幕中的x坐标。
  screenY:触摸目标在屏幕中的y坐标。
  target:触目的DOM节点目标。

<template>
  <view
    class="box"
    :style="{right:move+'px'}"
    @touchstart="drawStart"
    @touchmove="drawMove"
    @touchend="drawEnd"
  >
    <view class="b1">
      左滑删除
    </view>
    <view class="b2" @click="d()">
      删除
    </view>
  </view>
</template>
<script setup lang="ts">
const startX = ref(0)
const move = ref(0)
const d = async () => {
  const res = uni.showModal({
    title: '确定删除吗',
    confirmColor: '#e61f19',
  })
  if ((await res).confirm) {
    console.log('删除')
  }
}
// 开始触摸滑动
const drawStart = e => {
  console.log('开始触发')
  // const touch = e.touches[0]
  console.log(e.touches[0].clientX)

  startX.value = e.touches[0].clientX
}
// //触摸滑动
const drawMove = e => {
  console.log('滑动')
  move.value = startX.value - e.touches[0].clientX
  console.log(move.value)
  if (move.value > 100) {
    move.value = 100
  } else if (move.value < 0) {
    move.value = 0
  }
}
// //触摸滑动结束
const drawEnd = e => {
  console.log('滑动结束')
  // const touch = e.touches[0]
  console.log(e)
  if (move.value > 100) {
    move.value = 100
  } else if (move.value < 0) {
    move.value = 0
  }
}

</script>
<style scoped lang="scss">
.box {
  display: flex;
  position: relative;
}
.b1 {
  width: 750rpx;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: large;
  background-color: #fff;
  margin-top: 10px;
  border: 1px solid rgb(0 0 0);
}
.b2 {
  position: absolute;
  right: -100px;
  width: 100px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: large;
  background-color: rgb(243 88 88);
  margin-top: 10px;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值