vue自定義移動端touch事件之點擊、滑動、長按事件
用法:
**HTML**
<div id="app" class="box"
v-tap="vuetouch" //vuetouch為函數(shù)名,如沒有參數(shù),可直接寫函數(shù)名
v-longtap="{fn:vuetouch,name:'長按'}" //如果有參數(shù)以對象形式傳,fn 為函數(shù)名
v-swipeleft="{fn:vuetouch,name:'左滑'}"
v-swiperight="{fn:vuetouch,name:'右滑'}"
v-swipeup="{fn:vuetouch,name:'上滑'}"
v-swipedown="{fn:vuetouch,name:'下滑'}"
>{{ name }}</div>
**js**
kim=new Vue({
el:"#app",
data:{
name:"開始"
},
methods:{
vuetouch:function(s,e){
this.name=s.name;
}
}
});
js核心內(nèi)容
function vueTouch(el,binding,type){
var _this=this;
this.obj=el;
this.binding=binding;
this.touchType=type;
this.vueTouches={x:0,y:0};
this.vueMoves=true;
this.vueLeave=true;
this.longTouch=true;
this.vueCallBack=typeof(binding.value)=="object"?binding.value.fn:binding.value;
this.obj.addEventListener("touchstart",function(e){
_this.start(e);
},false);
this.obj.addEventListener("touchend",function(e){
_this.end(e);
},false);
this.obj.addEventListener("touchmove",function(e){
_this.move(e);
},false);
};
vueTouch.prototype={
start:function(e){
this.vueMoves=true;
this.vueLeave=true;
this.longTouch=true;
this.vueTouches={x:e.changedTouches[0].pageX,y:e.changedTouches[0].pageY};
this.time=setTimeout(function(){
if(this.vueLeave&&this.vueMoves){
this.touchType=="longtap"&&this.vueCallBack(this.binding.value,e);
this.longTouch=false;
};
}.bind(this),1000);
},
end:function(e){
var disX=e.changedTouches[0].pageX-this.vueTouches.x;
var disY=e.changedTouches[0].pageY-this.vueTouches.y;
clearTimeout(this.time);
if(Math.abs(disX)>10||Math.abs(disY)>100){
this.touchType=="swipe"&&this.vueCallBack(this.binding.value,e);
if(Math.abs(disX)>Math.abs(disY)){
if(disX>10){
this.touchType=="swiperight"&&this.vueCallBack(this.binding.value,e);
};
if(disX<-10){
this.touchType=="swipeleft"&&this.vueCallBack(this.binding.value,e);
};
}else{
if(disY>10){
this.touchType=="swipedown"&&this.vueCallBack(this.binding.value,e);
};
if(disY<-10){
this.touchType=="swipeup"&&this.vueCallBack(this.binding.value,e);
};
};
}else{
if(this.longTouch&&this.vueMoves){
this.touchType=="tap"&&this.vueCallBack(this.binding.value,e);
this.vueLeave=false
};
};
},
move:function(e){
this.vueMoves=false;
}
};
Vue.directive("tap",{
bind:function(el,binding){
new vueTouch(el,binding,"tap");
}
});
Vue.directive("swipe",{
bind:function(el,binding){
new vueTouch(el,binding,"swipe");
}
});
Vue.directive("swipeleft",{
bind:function(el,binding){
new vueTouch(el,binding,"swipeleft");
}
});
Vue.directive("swiperight",{
bind:function(el,binding){
new vueTouch(el,binding,"swiperight");
}
});
Vue.directive("swipedown",{
bind:function(el,binding){
new vueTouch(el,binding,"swipedown");
}
});
Vue.directive("swipeup",{
bind:function(el,binding){
new vueTouch(el,binding,"swipeup");
}
});
Vue.directive("longtap",{
bind:function(el,binding){
new vueTouch(el,binding,"longtap");
}
});
2018-03-08
有朋友提出一個bug
“v-for循環(huán) 生命周期后 獲取不到新值 比如更新了數(shù)據(jù)”
這個問題是v-for的就地復(fù)用機(jī)制導(dǎo)致的,也就是可以復(fù)用的dom沒有重復(fù)渲染,官方給出的方法是需要為每項提供一個唯一 key 屬性。理想的 key 值是每項都有的且唯一的 id。
<div v-for="item in items" :key="item.id"> <!-- 內(nèi)容 --> </div>
我的解決方案是directive的鉤子函數(shù)參數(shù)有一個vnode,這個是虛擬dom節(jié)點,給vnode.key賦予一個隨機(jī)id,強(qiáng)制dom刷新。
Vue.directive("tap",{
bind:function(el,binding,vnode){
vnode.key = randomString()//randomString會返回一個隨機(jī)字符串
new vueTouch(el,binding,"tap");
}
});
最新的版本已經(jīng)在GitHub更新
https://github.com/904790204/vue-touch
總結(jié)
以上所述是小編給大家介紹的vue自定義移動端touch事件之點擊、滑動、長按事件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue的圖片需要用require的方式進(jìn)行引入問題
這篇文章主要介紹了vue的圖片需要用require的方式進(jìn)行引入問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
在Vue中使用xlsx組件實現(xiàn)Excel導(dǎo)出功能的步驟詳解
在現(xiàn)代Web應(yīng)用程序中,數(shù)據(jù)導(dǎo)出到Excel格式是一項常見的需求,Vue.js是一種流行的JavaScript框架,允許我們構(gòu)建動態(tài)的前端應(yīng)用程序,本文將介紹如何使用Vue.js和xlsx組件輕松實現(xiàn)Excel數(shù)據(jù)導(dǎo)出功能,需要的朋友可以參考下2023-10-10
vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果
這篇文章主要介紹了vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場景分析
今天遇到了這樣一個場景,在數(shù)據(jù)更新之后,使用this.$nextTick(()=>{console.log(this.$refs.xxx)}) 獲取不到改dom,但是用setTimeout能夠獲取到,在此記錄一下,感興趣的朋友跟隨小編一起看看吧2023-02-02
moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決
這篇文章主要介紹了moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
vue使用$emit時,父組件無法監(jiān)聽到子組件的事件實例
下面小編就為大家分享一篇vue使用$emit時,父組件無法監(jiān)聽到子組件的事件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

