国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 永定县| 进贤县| 武威市| 贵州省| 大方县| 克什克腾旗| 波密县| 凯里市| 甘谷县| 丰镇市| 浠水县| 望谟县| 阳城县| 广饶县| 从化市| 蓬溪县| 连南| 图片| 唐海县| 蒲城县| 太谷县| 剑阁县| 辽宁省| 安康市| 阳城县| 海阳市| 昂仁县| 云安县| 杭州市| 德庆县| 汝南县| 两当县| 舟曲县| 吉木萨尔县| 敦煌市| 富川| 平舆县| 玛纳斯县| 会东县| 鸡西市| 重庆市|