diff --git a/README.md b/README.md index 373ded2..7f8e8e6 100644 --- a/README.md +++ b/README.md @@ -14,36 +14,42 @@ import vueTouch from 'kim-vue-touch' Vue.use(vueTouch) ``` -* kim-vue-touch提供了点击、长按、左滑、右滑、上滑、下滑等事件, -* 当你不需要传参时可以通过v-tap="vueTouch"的形式调用方法, -* 当你需要传参时v-tap="(e)=>vueTouch('点击',e)"的方式调用,e是event对象 -* 当指令搭配v-for使用时,一定要确保key值的唯一性,否则不能保证数据刷新之后事件被重新绑定(参考vue就地复用机制) -* 尽量避免对数组、对象的直接赋值操作,可能会导致参数不更新 + +- kim-vue-touch 提供了点击、长按、左滑、右滑、上滑、下滑等事件, +- 指令 v-touchall='touchOption' +- touchOption 可分别配置启用哪些操作(如点击、长按、滑动,及其回调函数),以及长按延时,见 demo +- 当指令搭配 v-for 使用时,一定要确保 key 值的唯一性,否则不能保证数据刷新之后事件被重新绑定(参考 vue 就地复用机制) +- 尽量避免对数组、对象的直接赋值操作,可能会导致参数不更新 ``` -``` \ No newline at end of file +``` diff --git a/src/App.vue b/src/App.vue index ad491b5..7250d1d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,53 +1,57 @@ \ No newline at end of file diff --git a/src/index.js b/src/index.js index c856dfa..d06bf9c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,116 +1,117 @@ -function vueTouch(el,binding,type,vnode){ - 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=binding.value; - this.obj.addEventListener("touchstart",(e)=>{ - this.start(e); - },false); - this.obj.addEventListener("touchend",(e)=>{ - this.end(e); - },false); - this.obj.addEventListener("touchmove",(e)=>{ - this.move(e); - },false); - // vnode.key = this.randomString() - -}; -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.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(e); - if(Math.abs(disX)>Math.abs(disY)){ - if(disX>10){ - this.touchType=="swiperight"&&this.vueCallBack(e); - }; - if(disX<-10){ - this.touchType=="swipeleft"&&this.vueCallBack(e); - }; - }else{ - if(disY>10){ - this.touchType=="swipedown"&&this.vueCallBack(e); - }; - if(disY<-10){ - this.touchType=="swipeup"&&this.vueCallBack(e); - }; - }; - }else{ - if(this.longTouch&&this.vueMoves){ - this.touchType=="tap"&&this.vueCallBack(e); - this.vueLeave=false - }; - }; - }, - move:function(e){ - this.vueMoves=false; - }, - randomString:function(){ - var len = 10; -    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; -    var maxPos = $chars.length; -    var pwd = ''; -    for (var i = 0; i < len; i++) { - pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); -    } -    return pwd; - } -}; +function vueTouch(el, binding) { + this.activeMetheds = binding.value + this.delay = binding.value.longtapDelay ? binding.value.longtapDelay : 1000 + this.doubleDelay = binding.value.doubletapDelay + ? binding.value.doubletapDelay + : 300 -export default { - install: function (Vue, options) { - Vue.directive("tap",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"tap",vnode); - } - }); - Vue.directive("swipe",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"swipe",vnode); - } - }); - Vue.directive("swipeleft",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"swipeleft",vnode); - } - }); - Vue.directive("swiperight",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"swiperight",vnode); - } - }); - Vue.directive("swipedown",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"swipedown",vnode); - } - }); - Vue.directive("swipeup",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"swipeup",vnode); - } - }); - Vue.directive("longtap",{ - bind:function(el,binding,vnode){ - new vueTouch(el,binding,"longtap",vnode); + this.obj = el + this.binding = binding + this.vueTouches = { x: 0, y: 0 } + + this.ifTouchStandby = true + this.callbacks = {} + this.lastClickTime = null + this.timeOneClick = null + this.obj.addEventListener( + 'touchstart', + e => { + this.start(e) + }, + false + ) + this.obj.addEventListener( + 'touchend', + e => { + this.end(e) + }, + false + ) +} + +vueTouch.prototype = { + start: function(e) { + this.ifTouchStandby = true + this.vueTouches = { + x: e.changedTouches[0].pageX, + y: e.changedTouches[0].pageY + } + if (this.activeMetheds.longtap) { + this.time = setTimeout( + function() { + if (this.ifTouchStandby) { + this.activeMetheds.longtap() + this.ifTouchStandby = false + } + }.bind(this), + this.delay + ) + } + }, + end: function(e) { + var disX = e.changedTouches[0].pageX - this.vueTouches.x + var disY = e.changedTouches[0].pageY - this.vueTouches.y + if (this.time) clearTimeout(this.time) + if (this.ifTouchStandby) { + if (this.activeMetheds.doubletap) { + } + if (Math.abs(disX) > 10 || Math.abs(disY) > 10) { + if (Math.abs(disX) > Math.abs(disY)) { + if (disX > 10 && this.activeMetheds.swiperight) { + this.activeMetheds.swiperight() + } + if (disX < -10 && this.activeMetheds.swipeleft) { + this.activeMetheds.swipeleft() + } + } else { + if (disY > 10 && this.activeMetheds.swipedown) { + this.activeMetheds.swipedown() + } + if (disY < -10 && this.activeMetheds.swipeup) { + this.activeMetheds.swipeup() + } + } + } else if (this.activeMetheds.tap) { + if (!this.activeMetheds.doubletap) { + this.activeMetheds.tap() + } else { + if (!this.timeOneClick) { + this.timeOneClick = setTimeout( + function() { + this.activeMetheds.tap() + }.bind(this), + this.doubleDelay + ) + } + + if (this.lastClickTime) { + let twice = Date.now() + if (twice - this.lastClickTime < this.doubleDelay) { + this.activeMetheds.doubletap() + if (this.timeOneClick) clearTimeout(this.timeOneClick) + } else { + this.lastClickTime = Date.now() + this.timeOneClick = setTimeout( + function() { + this.activeMetheds.tap() + }.bind(this), + this.doubleDelay + ) } - }); + } else { + this.lastClickTime = Date.now() + } + } + } } -} \ No newline at end of file + } +} +export default { + install: function(Vue, options) { + Vue.directive('touchall', { + bind: function(el, binding) { + new vueTouch(el, binding) + } + }) + } +} diff --git a/src/main.js b/src/main.js index fedae44..1fbe29c 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,7 @@ import Vue from 'vue/dist/vue.js' import App from './App.vue' -import touch from 'kim-vue-touch' -// import touch from './index' +// import touch from 'kim-vue-touch' +import touch from './index' // import touch from '../dist/index' Vue.config.productionTip = false @@ -11,4 +11,4 @@ new Vue({ el: '#app', components: { App }, template: '' -}) \ No newline at end of file +})