国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue自定義移動(dòng)端touch事件之點(diǎn)擊、滑動(dòng)、長按事件

2024-05-06 15:32:48
字體:
供稿:網(wǎ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");  }});            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 疏勒县| 太保市| 垣曲县| 美姑县| 丽江市| 汪清县| 南安市| 河池市| 方山县| 辰溪县| 浦城县| 井冈山市| 苍山县| 通化县| 五家渠市| 车致| 荥阳市| 新津县| 巴楚县| 阳新县| 苗栗县| 福州市| 茶陵县| 乳山市| 淄博市| 淅川县| 遂溪县| 曲周县| 大城县| 紫阳县| 新泰市| 洛隆县| 奉化市| 通河县| 晋江市| 鹤壁市| 广德县| 东乌珠穆沁旗| 垦利县| 科技| 宜宾县|