js长按事件

方法一、touch事件

var self = this;
var longClick = false;
$(".product").on({
    touchstart: function(e){
        longClick = false;//设置初始为false
        timeOutEvent = setTimeout(function(){
            //此处为长按事件-----在此显示遮罩层及删除按钮
            longClick=true;//假如长按,则设置为1
        },500);
    },
    touchmove: function(){
        clearTimeout(timeOutEvent);
        timeOutEvent = 0;
        e.preventDefault();
    },
    touchend: function(e){
        clearTimeout(timeOutEvent);
        if(timeOutEvent!=0 && longClick==false){
            //此处为点击事件----在此处添加跳转详情页
        }
        return false;
    }
});

方法二、vue项目中vue-touch 的press和pressup替代touchstart和touchend

 <v-touch v-on:press="onPress(record.uid,index)" v-on:pressup="onPressUp">
   <router-link :to="{name:'chats',params:{talkUserId: record.uid,talkName:record.nickname}}" class="chats-link">
     <router-link :to="{name:'userInfo',params:{userId:record.uid,userName:record.nickname}}">
         <img :src="record.avatar">
     </router-link>
     <div class="private-msg-center">
         <h4>{{record.nickname}}</h4>
         <p>{{record.ct}}</p>
     </div>
     <div class="private-msg-right">
         <span class="private-msg-time">{{record.createtime}}</span>
         <span class="private-msg-num" v-if="record.unread != '0'">{{record.unread}}</span>
     </div>
   </router-link>
</v-touch>      


 //删除聊天
            onPress:function(uid,index) {
            var _this = this;
            _this.longClick = false;
            _this.timeOutEvent = setTimeout(function() {
                _this.longClick = true;
                console.log("长按");
                _this.$fui.confirm('删除该聊天', () => {
                    $.ajax({
                        type: "POST",
                        url: "/whisper/Index/deleteChatRecord",
                        data: {'sid':uid},
                        success: function(data){ 
                            var recodeData = _this.recordList;
                            console.log(data);
                            _this.$fui.toast(data.msg);
                            if(data.code == 1) {
                                recodeData.splice(index,1);
                                _this.recordList = recodeData;
                            }
                        },
                        error:function(data) {
                            console.log(data);
                            _this.$fui.toast('删除失败,请重新操作');
                        }
                    })
                });
            },500);
        },
        onPressUp:function() {
            var _this = this;
            if(_this.timeOutEvent) {
                clearTimeout(_this.timeOutEvent);
                _this.timeOutEvent = null;
            }
            return false;
        },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值