Vue 移动端常用tap事件封装
基于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事件封装的更多相关文章
- js移动端tap事件封装
这几天做项目,发现移动端需要触摸事件,而click肯定是不行的,于是我对tap事件封装进行了搜索,找到了一篇文章,原文地址如下:http://www.jb51.net/article/50663.ht ...
- 10行代码搞定移动web端自定义tap事件
发发牢骚 移动web端里摸爬滚打这么久踩了不少坑,有一定移动web端经验的同学一定被click困扰过.我也不列外.一路走来被虐的不行,fastclick.touchend.iscroll什么的都用过, ...
- tap事件封装
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...
- vue移动端常用组件
3d picker组件 参考链接:https://segmentfault.com/a/1190000007253581?utm_source=tag-newest安装:npm install vue ...
- 移动端click延迟和tap事件
一.click等事件在移动端的延迟 click事件在移动端和pc端均可以触发,但是在移动端有延迟现象. 1.背景 由于早期移动设备浏览网页时内容较小,为了增强用户体验,苹果公司专门为移动设备设计了双击 ...
- 从零开始学 Web 之 移动Web(五)touch事件的缺陷,移动端常用插件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 移动端WEB开发,click,touch,tap事件浅析
一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...
- 移动端为何不使用click而模拟tap事件及解决方案
移动端click会遇到2个问题,click会有200-300ms的延迟,同时click事件的延迟响应,会出现穿透,即点击会触发非当前层的点击事件. 为什么会存在延迟? Google开发者文档中有提到: ...
- 移动端click时间、touch事件、tap事件
一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...
随机推荐
- 查漏补缺之——Java多线程
复习面试题中遇到锁的内容当时大一学习的时候感觉懵懂,现在重新复习一下. 1.1多线程 1.1.1线程 1.什么是线程 线程是程序执行的一条路径,一个进程中包含多条进程 2.并行与并发 并行是两个任务同 ...
- js数组和数组去重的几种简单的方法
http://blog.csdn.net/liangklfang/article/details/49300417 1.证明一个对象是数组的方法. 方法(1) [].constructor === A ...
- (15)线程---Condition条件
功能:也是通过阻塞控制线程数量.类似信号量\进程池\线程池的作用 语法:wait from threading import Condition con= Condition() conn.acq ...
- spark中的combineByKey函数的用法
一.函数的源码 /** * Simplified version of combineByKeyWithClassTag that hash-partitions the resulting RDD ...
- Practical Node.js (2018版) 第8章:Building Node.js REST API Servers
Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...
- Android中如何解决editText一进入activity就自动获取焦点的bug
有时候我们在进入activity 的时候,EditText会自动聚焦 有人说搞个宽高 都为0dp的EditText 但是我们搞代码的肯定要从实际层面解决,这样更能说服人 所以只需要在EditText的 ...
- javaScript_Demo 全选和反选单选框
进行单选的全选和反选 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- Hexo-使用
hexo 写新文章 创建新的文章 ``` bash $ hexo new "Hexo-使用" ``` 生成md文件 ``` bash $ hexo generate ``` 写文章 ...
- CentOS 每个版本的区别
当我们下载CentOS 7 时会发现有几个版本可以选择,如下: 1.CentOS-7-DVD版本:DVD是标准安装盘,一般下载这个就可以了. 2.CentOS-7-NetInstall版本:网络安装镜 ...
- logging模块使用
日志介绍 日志级别: 默认显示级别为warning,(critical>error>warning>info>debug>notset) 日志格式配置,测试使用 impo ...