基于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. 如何在nginx容器中使用ping、nslookup、ip、curl 等工具?

    Nginx镜像太精简了,启动一个容器进行测试时,常用的网络工具都没有,可以使用下面的命令进行安装.也可以直接起一个busybox容器进行测试. apt update #ping apt install ...

  2. JS实时获取输入框中的值

    实时获取input输入框中的值需要oninput和onpropertychange属性来实现.原因是onpropertychange属性为IE专属,而oninput属性支持大部分浏览器包括IE9及以上 ...

  3. SDWebImage 的简单使用方法

    第一步,下载SDWebImage,导入工程 第二步,在需要的地方导入头文件:#import   "UIImageView+WebCache.h" 第三步,调用sd_setImage ...

  4. GIS中的引擎:地图引擎

    什么是地图引擎?它和地图软件有什么区别? 引擎一词是英文单词engine的音译,通常指发动机,就是动力输出设备.诸如汽车.轮船.飞机的动力提供的核心设备就是引擎.IT领域中,常听说的有搜索引擎.图形引 ...

  5. 20175227张雪莹 2018-2019-2 《Java程序设计》第四周学习总结

    20175227张雪莹 2018-2019-2 <Java程序设计>第四周学习总结 教材学习内容总结 一.子类和父类. 1.子类只继承父类中的protected和public访问权限的成员 ...

  6. 学生信息管理系统(C语言)

    #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student ...

  7. LINQ分组取出第一条数据

    Person1: Id=, Name="Test1" Person2: Id=, Name="Test1" Person3: Id=, Name="T ...

  8. Java启动工程时,加载固定数据到Map中(不用每次访问数据库)

    如下:启动工程时,加载配置文件的用户名和密码到map中, 通过getKeyByValue()获取即可 import java.util.HashMap; import java.util.Map; i ...

  9. jvm内存溢出问题

     Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出 J ...

  10. python魔术方法(__init__,__call__,被双下划线包围的方法)

    转载link:http://pycoders-weekly-chinese.readthedocs.io/en/latest/issue6/a-guide-to-pythons-magic-metho ...