基于Vue的移动端项目,有些时间原生并没用提供,需要我们自己手动封装,可以封装一些自定义指令来供全局使用。

本文封装了 tap, swipe, swipeleft, swiperight, swipedowm, swipedown, swipeup, longtap, 在项目中基本上也够用了

代码如下

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("touchmove", function(e) {
_this.move(e);
}, false); this.obj.addEventListener("touchend", function(e) {
_this.end(e);
}, false);
};
vueTouch.prototype = {
start(e) {
e.preventDefault(); // 长按时阻止默认菜单,IOS还需要 -webkit-touch-callout: none; 来禁用默认菜单
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(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(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");
}
});

Vue 移动端常用tap事件封装的更多相关文章

  1. js移动端tap事件封装

    这几天做项目,发现移动端需要触摸事件,而click肯定是不行的,于是我对tap事件封装进行了搜索,找到了一篇文章,原文地址如下:http://www.jb51.net/article/50663.ht ...

  2. 10行代码搞定移动web端自定义tap事件

    发发牢骚 移动web端里摸爬滚打这么久踩了不少坑,有一定移动web端经验的同学一定被click困扰过.我也不列外.一路走来被虐的不行,fastclick.touchend.iscroll什么的都用过, ...

  3. tap事件封装

    <!DOCTYPE html> <html lang="zh"> <head>     <meta charset="UTF-8 ...

  4. vue移动端常用组件

    3d picker组件 参考链接:https://segmentfault.com/a/1190000007253581?utm_source=tag-newest安装:npm install vue ...

  5. 移动端click延迟和tap事件

    一.click等事件在移动端的延迟 click事件在移动端和pc端均可以触发,但是在移动端有延迟现象. 1.背景 由于早期移动设备浏览网页时内容较小,为了增强用户体验,苹果公司专门为移动设备设计了双击 ...

  6. 从零开始学 Web 之 移动Web(五)touch事件的缺陷,移动端常用插件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  7. 移动端WEB开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  8. 移动端为何不使用click而模拟tap事件及解决方案

    移动端click会遇到2个问题,click会有200-300ms的延迟,同时click事件的延迟响应,会出现穿透,即点击会触发非当前层的点击事件. 为什么会存在延迟? Google开发者文档中有提到: ...

  9. 移动端click时间、touch事件、tap事件

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

随机推荐

  1. 最接近的三数之和(java实现)

    题目: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如 ...

  2. .gitlab-ci.yml简介

    关键字   script 由Runner执行的Shell脚本. image 使用docker镜像,  image:name service 使用docker  services镜像, services ...

  3. vuejs点滴

    博客0.没事的时候可以看的一些博客:https://segmentfault.com/a/1190000005832164 http://www.tuicool.com/articles/vQBbii ...

  4. Coding daily

    @2017-7月 1可视化控件的awakeFromNib不调用? 因为用代码注册了cell 2scrollView添加子控件布局无效? 最好不要用masonry,直接用frame 还有tableVie ...

  5. pip3 freeze导出依赖包 --Python3

    1.导出依赖包到*.txt文件 pip3 freeze>blueflag.txt 2.导出后的结果

  6. 一次完整的http事务的过程

    1.域名解析 2.发起TCP三次握手 3.建立TCP连接以后发起http请求 4.服务器端响应请求,浏览器得到html代码 5.浏览器解析html代码并请求html中的资源 6.浏览器对页面进行渲染呈 ...

  7. apply、call、bind的区别

    apply.call.bind这三种方法一般用来改变this指向. apply()方法接收两个参数,一个是函数运行的作用域this,另一个是参数数组 call()方法接收两个参数,一个是函数运行的作用 ...

  8. Andriod Studio两种签名机制V1和V2的区别

    Android Studio 2.2以上版本打包apk的时候,我们会发现多了个签名版本(v1.v2)选择,如下图红色方框所示 问题描述(v1和v2) Android 7.0中引入了APK Signat ...

  9. C#中用NPOI的excel导出

    //机构表导出 private static List<User2> amininf = new BLL.Bll().GetUser2s(); //定义数据源导出对象 #region 导出 ...

  10. C#设置随机整数

    JQuery var x = 5;//最大值var y = 1;//最小值var rand = parseInt(Math.random() * (x - y + 1) + y); Mvc控制器 Ra ...