通过html5 touch事件封装手势识别组件
html5移动端新增了touchstart,touchmove,touchend事件,利用这3个事件,判断手指的点击和划动轨迹,我们可以封装各种手势的识别功能,
这3个事件和pc端的mousedown,mousemove,mouveup非常类似,不同的是touch事件可以有多个点击的点,而鼠标每次只有一个点,我们即然是做组件封装,就要考虑在pc上调试的情况,否则用手机调试非常不方便,通过对mouse事件的处理,可以一套代码同时兼容pc端和移动端。
下面来逐步封装一个滑动手势(swipe)的组件
1.判断是否触摸屏
我们使用能力检测,检测是否支持touchstart事件,就可以知道是否是触摸屏,因为触摸事件可以通过document.ontouchstart=function(){} 这样的方式定义,用in操作符判断即可,对于win8,触屏能力会在navigator对象中生成一个msPointerEnabled属性。
if ('ontouchstart' in window || 'ontouchstart' in document) {
//iOS & android
supportsTouch = true;
} else if(window.navigator.msPointerEnabled) {
//Win8
supportsTouch = true;
}
2.同时兼容鼠标和触摸屏的事件绑定
我们根据上一步的判断,如果支持toucestart就绑定对应的touchstart,touchmove,touchend事件,如果不支持,则绑定对应的3个鼠标事件
if(isSupportTouch()){
el.addEventListener('touchstart',touchStart);
el.addEventListener('touchend',touchEnd);
el.addEventListener('touchmove',touchMove);
}else{
el.addEventListener('mousedown',touchStart);
el.addEventListener('mouseup',touchEnd);
el.addEventListener('mousemove',touchMove);
}
3.获取点击的点位置信息(兼容鼠标和触摸屏)
从事件参数中可以得到位置信息,如果是鼠标,则通过e.pageX,e.pageY获取点击位置相对于页面根节点的坐标,如果是触摸屏,则e.touches对象是一个点击点位置的数组,包含多个手指的点击位置,我们暂时只处理一只手指的情况,所以取e.touches[0].pageX,e.touches[0].pageY.
function touchStart(e){
var t=e.touches?e.touches[0]:e;
startPoint={x:t.pageX,y:t.pageY};
}
4.判断手指滑动方向
在toucemove事件中判断手指划动,toucemove事件会连续触发,为了过滤掉划动距离太短的无效滑动,我们可以判断pageX和pageY和上一次位置的偏移量超过两个像素才认为是有效事件,然后再判断滑动方向,当前点击位置的(x,y)坐标,减去上一个位置的(x,y)坐标,如果x轴的差值大,就认为是左右滑,如果是y轴的差值大就认为是上下滑,再进一步判断差值 为正数则是左或上,差值为负数则为右或下。代码如下:
function getSwipeDirection(x1, x2, y1, y2) {
return Math.abs(x1 - x2) >=
Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'left' : 'right') : (y1 - y2 >0 ? 'up' : 'down')
}
5.jquery插件封装
为了更方便使用,可以封装成jquery插件,我们常说的jquery对象其实是指继随自jquery原型的对象,jquery的原型是指$.fn,只要扩展$.fn即可,
如$.fn.methodName=function(){//code}
或用$.fn.extend({
methodName:funciton(){//code}
})
完整代码如下:
function TouchEvent(){
var self=this,element=$(this);
var el=element[0],isTouching,isSwipe,startTime,startPoint,currentPoint;
if(arguments.length>1){
var eventType=arguments[0];
}
var callback=arguments[arguments.length-1];
function doAction(type,args){
args.type=type;
if(eventType){
if(eventType==type){
callback.call(self,args);
}
}else{
callback.call(self,args);
}
}
function getSwipeDirection(x1, x2, y1, y2) {
return Math.abs(x1 - x2) >=
Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'left' : 'right') : (y1 - y2 >0 ? 'up' : 'down')
}
function isSupportTouch(){
var supportsTouch = false;
if ('ontouchstart' in window || 'ontouchstart' in document) {
//iOS & android
supportsTouch = true;
} else if(window.navigator.msPointerEnabled) {
//Win8
supportsTouch = true;
}
return supportsTouch;
}
function touchStart(e){
isTouching=true;
startTime=new Date();
var t=e.touches?e.touches[0]:e;
startPoint={x:t.pageX,y:t.pageY};
}
function touchMove(e){
if(isTouching){
var t=e.touches?e.touches[0]:e;
var p={x:t.pageX,y:t.pageY};
currentPoint=p;
var x1=startPoint.x,x2=currentPoint.x,y1=startPoint.y,y2=currentPoint.y;
if(Math.abs(x1-x2)>2 || Math.abs(y1-y2)>2){
isSwipe=true;
var direction=getSwipeDirection(x1,x2,y1,y2);
//console.log(direction);
e.direction=direction;
doAction("swipe",e);
}
}
}
function touchEnd(e){
isTouching=false;
if(!isSwipe){
e["long"]=new Date()-startTime>1000;
doAction("tap",e);
//console.log("tap");
}else{
var x1=startPoint.x,x2=currentPoint.x,y1=startPoint.y,y2=currentPoint.y;
var direction=getSwipeDirection(x1,x2,y1,y2);
console.log(direction)
doAction("swipeEnd",{direction:direction});
}
isSwipe=false;
}
if(isSupportTouch()){
el.addEventListener('touchstart',touchStart);
el.addEventListener('touchend',touchEnd);
el.addEventListener('touchmove',touchMove);
//el.addEventListener('touchcancel',actionFinsh);
}else{
el.addEventListener('mousedown',touchStart);
el.addEventListener('mouseup',touchEnd);
el.addEventListener('mousemove',touchMove);
}
}
$.fn.touchEvent = TouchEvent;
通过html5 touch事件封装手势识别组件的更多相关文章
- html5 touch事件实现触屏页面上下滑动(二)
五一小长假哪都没去,睡了三天,今天晕晕沉沉的投入工作了,但还是做出了一点点效果 上周用html5的touch事件把简单的滑动做出来了,实现了持续页面上下滑动的效果,参考之前 的文章及代码html5 t ...
- html5 touch事件实现触屏页面上下滑动(一)
最近做的做那个app的项目由于用overflow:hidden导致了很多问题,于是决定研究下html5的touch事件.想找个全面点的帖子真是难死了,虽然好多关于html5 touch的文章但大多都是 ...
- 移动端touch事件封装
<meta charset="utf-8"><meta name="viewport" content="width=device- ...
- 手机端touch事件封装
var touchEvent={ /*单次触摸事件*/ tap:function(element,fn){ var startTx, startTy; element.addEventListener ...
- React-Native系列Android——Touch事件原理及状态效果
Native原生相比于Hybrid或H5最大长处是具有流畅和复杂的交互效果,触摸事件便是当中重要一项,包括点击(Click).长按(LongClick).手势(gesture)等. 以最简单常见的点击 ...
- 触摸事件,手势识别(UITouch,UIGestureRecognizer)
触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...
- 移动web touch事件
参考:http://www.cnblogs.com/dojo-lzz/p/5452575.html wap中的原生touch 事件,touchstart.touchmove.touchend.touc ...
- javascript移动设备Web开发中对touch事件的封装实例
在触屏设备上,一些比较基础的手势都需要通过对 touch 事件进行二次封装才能实现.zepto 是移动端上使用率比较高的一个类库,但是其 touch 模块模拟出来的一些事件存在一些兼容性问题,如 ta ...
- 简易封装手机浏览器touch事件
做手机开发时候,简单想用一些动作,如touchLeft,touchRight等, 使用其他库文件就要加载很多不必要的东西,流量的浪费 今天简单写了封装touch的库,简单的监听一些逻辑 onTouch ...
随机推荐
- 为什么不要在MySQL中使用UTF-8编码方式
MySQL的UTF-8编码方式 MySQL 从 4.1 版本开始支持 UTF-8,也就是 2003 年,然而目前流行的UTF-8 标准(RFC 3629)是在此之后规定的.正因此,才造就了MySQL中 ...
- 简单讲解什么是黑帽SEO
此文章主要讲的是黑帽SEO之搜索引擎劫持: SEO(Search Engine Optimization)搜索引擎优化,简单来说,就是让网站的排名更高,比如,搜索"博客"这个关键字 ...
- 入门学习C链接
参考链接:http://c.biancheng.net/view/465.html 在里面链接下载了:code:block,还有C语言入门的PDF文件. 常看网站:https://www.cnblog ...
- PAT A1025 pat ranking
有n个考场,每个考场都有若干数量个考生,现给出各个考场中考生的准考证号和分数,要求将所有考生的分数从高到低排序,并输出 #include<iostream> #include<str ...
- 移动端触摸touchstart监听事件
click.mousedown等事件适用于PC端,在移动端会有一定时间的延迟,所以更好的优化移动端体验,要用touch事件, 1.首先要添加一个监听事件,监听移动端行为 element.addEven ...
- linux 串口 拼帧处理
串口每次read数据可能不是完整的数据,参照网上的代码,写了拼帧的代码#include <stdio.h> #include <termios.h> #include < ...
- C 常用库函数memset,编译器宏定义assert
一. 总览 1.1库函数 函数名 头文件 功能 原型 说明 syslog syslog.h 记录至系统记录(日志) void syslog(int, const char *, ...) __p ...
- NSQ学习记录
一.简介 NSQ是一个基于Go语言的开源的分布式实时消息平台,他的代码托管在GitHub上. NSQ可用于大规模系统的实时消息服务,它的设计目标是为在分布式环境下提供一个强大的去除中心化的分布式服务架 ...
- Windows 10长脸了!
Windows 10一直毁誉参半,但是在微软的持续升级完善之下,同时随着时间的流逝,已经顺利成为全球第一大桌面操作系统,并开始逐渐甩开Windows 7,全球设备安装量已经达到约8亿部. 根据最新的S ...
- mysq8设置编码utf8
设置mysql默认编码utf8 以及其他配置 系统:centos7 vi /etc/my.cnf #红色部分如果以存在则在他的下方添加 [mysql] default-character-set=ut ...