移动端多指触屏事件兼容
来源:互联网 发布:网易云音乐数据库 编辑:程序博客网 时间:2024/06/05 20:56
ios下有多指触屏事件,如
gesturestart: 当手指触摸屏幕,并且有两根以上手指执行;
gesturechange:当有两根或多根手指在屏幕上,并且有手指移动的时候触发;
gestureend:当多余两根手指头抬起时执行;
本事件下有我们常用的事件如多指缩放 e.scale 多指旋转e.rotation 等,这里我们处理缩放事件
但是此事件在安卓下并不兼容,显然这事件是不可行的,为了兼容安卓和iOS,我们需要自己去封装多指事件;
这里我只贴出部分代码解释,首先,封装一个setGesture方法,此方法就是完全替代iOS下的多指事件,
setGesture方法接收一个参数inti,此参数为一个对象,对象里有四个属性,分别为el、start、change和end,
el是必须有的,为传入的元素,如 document.getElementById(id);
start是可选的,和iOS下gesturestart的属性相同:实现当手指触摸屏幕,并且有两根以上手指执行;
change是可选的,和iOS下gesturechange的属性相同:实现当有两根或多根手指在屏幕上,并且有手指移动的时候触发;
end是可选的,和iOS下gestureend的属性相同:当多余两根手指头抬起时执行;
如
setGesture({ el: 元素, start: function (e) { //相应的逻辑... }, change: function (e) { //相应的逻辑... }, end: function (e) { //相应的逻辑... }})
/*inti:{ el:element, //元素对象(必须) start:fn, change:fn, end:fn,}*/function setGesture(inti) { var el = inti.el; if (!el) return; var scaleStart = 0; var isGesture = false; var startPoint = []; //斜率 Math.atan2(y,x) 由一条直线与x轴正方向所成的角的正切 返回值的弧度 //角度转弧度 Math.PI/180 //弧度转角度 rad*180/Math.PI el.addEventListener('touchstart', function (e) { if (e.touches.length >= 2) { isGesture = true;//记录进行了多指操作 startPoint[0] = { x: e.touches[0].pageX, y: e.touches[0].pageY }; startPoint[1] = { x: e.touches[1].pageX, y: e.touches[1].pageY }; inti.start && inti.start.call(el, e);//手指按下时执行 } }); el.addEventListener('touchmove', function (e) { if (e.touches.length >= 2 && isGesture) { var nowPoint = []; //移动时手指的坐标点 nowPoint[0] = { x: e.touches[0].pageX, y: e.touches[0].pageY }; nowPoint[1] = { x: e.touches[1].pageX, y: e.touches[1].pageY }; var startDis = getDis(startPoint[1], startPoint[0]); var nowDis = getDis(nowPoint[1], nowPoint[0]); e.scale = nowDis / startDis;//手指移动时的距离与开始时的手指距离的比值 var startDeg = getDeg(startPoint[1], startPoint[0]); var nowDeg = getDeg(nowPoint[1], nowPoint[0]); e.rotation = nowDeg - startDeg;//多指旋转的角度 inti.change && inti.change.call(el, e);//手指移动时执行 } }); el.addEventListener('touchend', function (e) { if (isGesture) { //e.touches:当前屏幕的手指列表 e.targetTouches:当前元素的手指列表 if (e.touches.length < 2 || e.targetTouches.length < 1) { isGesture = false; inti.end && inti.end.call(el, e);//手指抬起时执行 } } });}function getDis(point1, point2) { var x = point2.x - point1.x; var y = point2.y - point1.y; return Math.sqrt(x * x + y * y);}function getDeg(point1, point2) { var x = point2.x - point1.x; var y = point2.y - point1.y; return Math.atan2(y, x) * 180 / Math.PI;}
完整demo下载 https://images-lm.oss-cn-hangzhou.aliyuncs.com/multiFinger.zip ,不足之处,请交流之,联系 qq:1033256773
若陛下您觉得满意,可随意给臣打赏,几毛几分都是对微臣的支持!
阅读全文
1 0
- 移动端多指触屏事件兼容
- 移动手机web页面转屏事件兼容解决方案
- html5移动设备浏览器触屏事件兼容处理
- javascript事件机制详解(涉及移动兼容)
- jQuery UI draggable(拖动)兼容移动端并且模拟click点击事件,兼容安卓
- 【移动端兼容问题研究】事件机制详解(涉及移动兼容)
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
- div移动层精简示例(event事件兼容多浏览器)
- div移动层精简示例(event事件兼容多浏览器)
- 产品设计 用户体验优化之兼容PC与移动端的回车搜索响应事件设计
- 绑定事件的时候,如果pc与移动端都用,可以做一下,pc与移动端的兼容
- 移动端兼容总结
- 兼容鼠标滚轮事件
- 浏览器兼容Event事件
- 兼容tap事件
- javascript键盘事件兼容
- JDK各个版本的新特性jdk1.5-jdk1.8
- [C#]利用EasyHook创建安装本地钩子[教程翻译+理解]
- 转型
- [LeetCode]Integer to Roman
- 刚学if else 用的不好 各位大神不要见笑
- 移动端多指触屏事件兼容
- 在centos环境下部署yara
- HttpClient发送HTTP请求
- Battle test(Locust and Apache Bench)
- chartjs图标插件
- iOS 多语言版本的开发(三)
- 常见的汉字字符集编码
- html学习笔记之一:表单控件
- C++primer(第五版) 第二章 学习笔记