vue自定義移動(dòng)端touch事件之點(diǎn)擊、滑動(dòng)、長按事件
用法:
**HTML** <div id="app" class="box" v-tap="vuetouch" //vuetouch為函數(shù)名,如沒有參數(shù),可直接寫函數(shù)名 v-longtap="{fn:vuetouch,name:'長按'}" //如果有參數(shù)以對(duì)象形式傳,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
有朋友提出一個(gè)bug
“v-for循環(huán) 生命周期后 獲取不到新值 比如更新了數(shù)據(jù)”
這個(gè)問題是v-for的就地復(fù)用機(jī)制導(dǎo)致的,也就是可以復(fù)用的dom沒有重復(fù)渲染,官方給出的方法是需要為每項(xiàng)提供一個(gè)唯一 key 屬性。理想的 key 值是每項(xiàng)都有的且唯一的 id。
<div v-for="item in items" :key="item.id"> <!-- 內(nèi)容 --> </div>
我的解決方案是directive的鉤子函數(shù)參數(shù)有一個(gè)vnode,這個(gè)是虛擬dom節(jié)點(diǎn),給vnode.key賦予一個(gè)隨機(jī)id,強(qiáng)制dom刷新。
Vue.directive("tap",{ bind:function(el,binding,vnode){ vnode.key = randomString()//randomString會(huì)返回一個(gè)隨機(jī)字符串 new vueTouch(el,binding,"tap"); } });
最新的版本已經(jīng)在GitHub更新
https://github.com/904790204/vue-touch
總結(jié)
以上所述是小編給大家介紹的vue自定義移動(dòng)端touch事件之點(diǎn)擊、滑動(dòng)、長按事件,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Vue實(shí)現(xiàn)移動(dòng)端左右滑動(dòng)效果的方法
- vue2.0移動(dòng)端滑動(dòng)事件vue-touch的實(shí)例代碼
- vue移動(dòng)端的左右滑動(dòng)事件詳解
- 寫一個(gè)移動(dòng)端慣性滑動(dòng)&回彈Vue導(dǎo)航欄組件 ly-tab
- vue實(shí)現(xiàn)一個(gè)移動(dòng)端屏蔽滑動(dòng)的遮罩層實(shí)例
- vue2.0 better-scroll 實(shí)現(xiàn)移動(dòng)端滑動(dòng)的示例代碼
- vue移動(dòng)端實(shí)現(xiàn)手機(jī)左右滑動(dòng)入場(chǎng)動(dòng)畫
- vue移動(dòng)端實(shí)現(xiàn)手指滑動(dòng)效果
- 移動(dòng)端滑動(dòng)切換組件封裝 vue-swiper-router實(shí)例詳解
- vue3實(shí)現(xiàn)移動(dòng)端滑動(dòng)模塊
相關(guān)文章
vue的圖片需要用require的方式進(jìn)行引入問題
這篇文章主要介紹了vue的圖片需要用require的方式進(jìn)行引入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03在Vue中使用xlsx組件實(shí)現(xiàn)Excel導(dǎo)出功能的步驟詳解
在現(xiàn)代Web應(yīng)用程序中,數(shù)據(jù)導(dǎo)出到Excel格式是一項(xiàng)常見的需求,Vue.js是一種流行的JavaScript框架,允許我們構(gòu)建動(dòng)態(tài)的前端應(yīng)用程序,本文將介紹如何使用Vue.js和xlsx組件輕松實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)出功能,需要的朋友可以參考下2023-10-10vue3+vite3+typescript實(shí)現(xiàn)驗(yàn)證碼功能及表單驗(yàn)證效果
這篇文章主要介紹了vue3+vite3+typescript實(shí)現(xiàn)驗(yàn)證碼功能及表單驗(yàn)證效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場(chǎng)景分析
今天遇到了這樣一個(gè)場(chǎng)景,在數(shù)據(jù)更新之后,使用this.$nextTick(()=>{console.log(this.$refs.xxx)}) 獲取不到改dom,但是用setTimeout能夠獲取到,在此記錄一下,感興趣的朋友跟隨小編一起看看吧2023-02-02moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決
這篇文章主要介紹了moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例
這篇文章主要介紹了使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue3+koa實(shí)現(xiàn)文件上傳功能的全過程記錄
開發(fā)項(xiàng)目的時(shí)候,用到文件上傳的功能很常見,包括單文件上傳和多文件上傳,下面這篇文章主要給大家介紹了關(guān)于vue3+koa實(shí)現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下2023-01-01vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例
下面小編就為大家分享一篇vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02