亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2018年07月10日 11:41:17   作者:金大光  
這篇文章主要介紹了vue自定義移動(dòng)端touch事件之點(diǎn)擊、滑動(dòng)、長按事件的實(shí)例代碼,需要的朋友可以參考下

用法:

**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)站的支持!

相關(guān)文章

  • Vue.js 和 MVVM 的注意事項(xiàng)

    Vue.js 和 MVVM 的注意事項(xiàng)

    MVVM 是Model-View-ViewModel 的縮寫,它是一種基于前端開發(fā)的架構(gòu)模式,Vue.js 是一個(gè)提供 MVVM 風(fēng)格的雙向數(shù)據(jù)綁定的 Javascript 庫,專注于View 層。這篇文章給大家介紹Vue.js 和 MVVM 的注意事項(xiàng),感興趣的朋友一起看看吧
    2016-11-11
  • vue的圖片需要用require的方式進(jìn)行引入問題

    vue的圖片需要用require的方式進(jìn)行引入問題

    這篇文章主要介紹了vue的圖片需要用require的方式進(jìn)行引入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 在Vue中使用xlsx組件實(shí)現(xiàn)Excel導(dǎo)出功能的步驟詳解

    在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-10
  • vue3+vite3+typescript實(shí)現(xiàn)驗(yàn)證碼功能及表單驗(yàn)證效果

    vue3+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
  • 基于Vue.js 實(shí)現(xiàn)簡易拖拽指令

    基于Vue.js 實(shí)現(xiàn)簡易拖拽指令

    在 Vue.js 中,我們可以通過自定義指令的方式來實(shí)現(xiàn)拖拽功能,使得代碼更加模塊化和可復(fù)用,本文將介紹如何基于 Vue.js 實(shí)現(xiàn)一個(gè)簡易的拖拽指令,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • 使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場(chǎng)景分析

    使用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-02
  • moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決

    moment轉(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)示例

    這篇文章主要介紹了使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • vue3+koa實(shí)現(xiàn)文件上傳功能的全過程記錄

    vue3+koa實(shí)現(xiàn)文件上傳功能的全過程記錄

    開發(fā)項(xiàng)目的時(shí)候,用到文件上傳的功能很常見,包括單文件上傳和多文件上傳,下面這篇文章主要給大家介紹了關(guān)于vue3+koa實(shí)現(xiàn)文件上傳功能的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例

    vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例

    下面小編就為大家分享一篇vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02

最新評(píng)論