Vue.directive('longpress',function(el,binding,vNode){
console.log(el,binding,vNode)
let timer = null;
var start = function(e){
if(e.type == 'click') return;
if(timer==null){
timer = setTimeout(function() {
binding.value()
}, 1000);
}
};
var cancel = function(){
if(timer!== null){
clearTimeout(timer)
timer = null
}
};
el.addEventListener('mousedown',start);
el.addEventListener('touchstart',start);
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);
el.addEventListener("touchend", cancel);
el.addEventListener("touchcancel", cancel);
})
<span v-for="item in history" v-longpress="pressDlt">{{item}}</span>正确书写方式
<span v-for="item in history" v-longpress="pressDlt(item)">{{item}}</span>这样写会报错
如何把item传进去
vue自定义可传参指令
猜你喜欢
转载自blog.csdn.net/qq_42928070/article/details/104797266
今日推荐
周排行