VUE长按事件需求功能实现

最近公司提了一个需求,需要长按添加删除按钮,我以下为大家写一个简单的长按事件,拿走就能应用到你的项目中。以为我们公司还要点击的话还要跳一个页面,在div中又添加了一个click事件,长按不跳转,反之跳转。 

 

<div class="weui_cell" v-for="(item,index) in labelList" @touchstart="showDeleteButton(item.id)" @click="labelDetails(item.id)">
      内容
</div>

// 或者这样
<div class="weui_cell" v-for="(item,index) in labelList" @touchstart="showDeleteButton(item.id)" @touchend="emptyTime">
      内容
</div>
在data中声明变量,用来清空定时器
data:{
    Loop:null,
},
在方法中将以下方法复制过去即可应用。
showDeleteButton:function (e) {
    clearTimeout(this.Loop); //再次清空定时器,防止重复注册定时器
    this.Loop = setTimeout(function() {
        alert("长按了")
    }.bind(this), 1000);
},
//清空
emptyTime:function () {
    clearTimeout(this.Loop); //清空定时器,防止重复注册定时器
},
手机浏览器中,长按可选中文本,在项目中出现这种现象,会很难受最好还是禁用此功能为上。* {
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}


这样一个简单的长按事件就写好了,如果想要更多问题请关注我,谢谢大家支持!!!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue中可以通过`@touchstart`、`@touchmove`和`@touchend`等事件实现事件和取消事件的效果。具体实现方法如: 1. 事件 在需要实现事件的元素上添加`@touchstart`事件,并在事件处理函数中使用`setTimeout`函数来实现事件的效果。例如: ```html <template> <div @touchstart="startLongPress">按我</div> </template> <script> export default { methods: { startLongPress() { this.timer = setTimeout(() => { console.log('事件触发了') }, 1000) } } } </script> ``` 在上面的代码中,我们在`startLongPress`方法中使用`setTimeout`函数来实现事件的效果,当用户按元素1秒钟后,就会触发`console.log`语句输出“事件触发了”。 2. 取消事件 在需要取消事件的元素上添加`@touchmove`和`@touchend`事件,并在事件处理函数中使用`clearTimeout`函数来取消事件的效果。例如: ```html <template> <div @touchstart="startLongPress" @touchmove="cancelLongPress" @touchend="cancelLongPress">按我</div> </template> <script> export default { methods: { startLongPress() { this.timer = setTimeout(() => { console.log('事件触发了') }, 1000) }, cancelLongPress() { clearTimeout(this.timer) } } } </script> ``` 在上面的代码中,我们在`cancelLongPress`方法中使用`clearTimeout`函数来取消事件的效果,当用户在按元素的过程中移动手指或者松开手指时,就会取消事件的效果。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值