vue长按事件

3 篇文章 0 订阅

 html

<template>
 <div>
	<div class="list wauto" @touchstart="gtouchstart(1)" @touchmove="gtouchmove()" @touchend="gtouchend(1)">长按事件</div>
  </div>
</template>

js

<script>
	export default {
		data() {
			return {
				timeOutEvent:0
			}
		},
		mounted: function() {
		},
		methods: {
			//开始按
	          gtouchstart(item){
	          	 var self = this;
	              this.timeOutEvent = setTimeout(function(){
	                  self.longPress(item)
	              },500);//这里设置定时器,定义长按500毫秒触发长按事件,时间可以自己改,个人感觉500毫秒非常合适
	              return false;
	          },
	          //手释放,如果在500毫秒内就释放,则取消长按事件,此时可以执行onclick应该执行的事件
	          gtouchend(item){
	              clearTimeout(this.timeOutEvent);//清除定时器
	              if(this.timeOutEvent!=0){
	                  //这里写要执行的内容(尤如onclick事件)
	                  this.goChat(item);
	              }
	              return false;
	          },
	          //如果手指有移动,则取消所有事件,此时说明用户只是要移动而不是长按
	          gtouchmove(){
	              clearTimeout(this.timeOutEvent);//清除定时器
	              this.timeOutEvent = 0;
	
	          },
	          //真正长按后应该执行的内容
	          longPress(id){
	              this.timeOutEvent = 0;
	              //执行长按要执行的内容,如弹出菜单
	              console.log("执行的内容");
	          },
	          goChat(id){
	              console.log("点击");
	              
	          }
			
		}
	}
</script>

 

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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`函数来取消事件的效果,当用户在按元素的过程中移动手指或者松开手指时,就会取消事件的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WebCsDn_TDCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值