//directive.js
import Vue from 'vue'
/*touch事件*/
Vue.directive('touch',{
isTap: function(tapObj){
let isTap = Math.abs(tapObj.pagX-tapObj.endX) > 10 || Math.abs(tapObj.pagY-tapObj.endY) > 10;
let _x = tapObj.pagX-tapObj.endX;
let _y = tapObj.pagY-tapObj.endY;
let ads = Math.abs(_x)-Math.abs(_y)
if(_x > 10 && ads>0)
this.tapObj.direction = 'left'
if(_x < -10 && ads>0)
this.tapObj.direction = 'right'
if(tapObj.pagY-tapObj.endY > 10 && ads<0)
this.tapObj.direction = 'up'
if(tapObj.pagY-tapObj.endY < -10 && ads<0)
this.tapObj.direction = 'down'
return isTap;
},
update: function (fn) {
var me = this;
me.tapObj = {};
me.touches = [];
if(typeof fn !== 'function')
return console.error('The param of directive "v-touch" must be a function!');
me.handler = (e) => {
e.tapObj = me.tapObj;
fn.call(self,e);
}
this.el.addEventListener('touchstart',function(event){
me.touches = [];
let touches = event.touches[0];
me.touches.push(touches);
me.tapObj.pagX = touches.pageX;
me.tapObj.pagY = touches.pageY;
me.tapObj.clientX = touches.clientX;
me.tapObj.clientY = touches.clientY;
},false)
let movehandler = function(event){
this.touches.push(event.touches[0]);
let objStar = this.touches[0];
let objEnd = event.touches[0];
if(Math.abs(objStar.clientX - objEnd.clientX) > Math.abs(objStar.clientY - objEnd.clientY)){
event.preventDefault();
}
}
this.el.addEventListener('touchmove',movehandler.bind(this),false)
this.el.addEventListener('touchend',function(event){
if(me.touches.length>1){
let touches = me.touches.pop();
me.tapObj.endX = touches.pageX;
me.tapObj.endY = touches.pageY;
me.tapObj.clientEndX = touches.clientEndX;
me.tapObj.clientEndY = touches.clientEndY;
if (me.isTap(me.tapObj))
me.handler(event);
}
me.el.removeEventListener('touchmove',movehandler,false)
},false)
}
})
/*监听粘贴事件*/
Vue.directive('paste',{
update: function (fn) {
let me = this;
if(typeof fn !== 'function')
return console.error('The param of directive "v-parse" must be a function!');
me.handler = () => {
fn.call();
}
this.el.addEventListener('paste',function(event){
me.handler()
})
}
})
Vue手机端实现touch,paste指令
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- (By: Kath & kimmy) 最近在做的一个几月vue的移动端小demo,其中有一块是实现各个页面的统一换...
- 手机端html5触屏事件(touch事件) touchstart:触摸开始的时候触发 touchmove:手指在屏...
- vue 中的自定义指令是对底层 dom 进行操作,下面以实现滚动到底部加载数据,实现无限加载来介绍如何自定义一个简...