为了给触摸界面提供有力支持, 触摸事件提供了响应用户对触摸屏或者触摸板上操作的能力.
接口
touchevent
touchevent 是一类描述手指在触摸平面(触摸屏、触摸板等)的状态变化的事件。这类事件用于描述一个或多个触点,使开发者可以检测触点的移动,触点的增加和减少,等等。每 个 touch 对象代表一个触点; 每个触点都由其位置,大小,形状,压力大小,和目标 element 描述。 touchlist 对象代表多个触点的一个列表.
触摸事件的类型
为了区别触摸相关的状态改变,存在多种类型的触摸事件。可以通过检查触摸事件的 touchevent.type 属性来确定当前事件属于哪种类型
判断滑动方向
基本原理就是记录开始滑动(touchstart)和结束滑动(touchend)的坐标位置,然后进行相对位置的计算。
touchstart:function(e){ startx = e.touches[0].pagex; starty = e.touches[0].pagey; e = e || window.event; }, touchend:function(e){ const that = this; endx = e.changedtouches[0].pagex; endy = e.changedtouches[0].pagey; that.upordown(startx,starty,endx,endy); }, upordown:function (startx, starty, endx, endy) { const that = this; let direction = that.getslidedirection(startx, starty, endx, endy); switch(direction) { case 0: console.log("没滑动"); break; case 1: console.log("向上"); break; case 2: console.log("向下"); break; case 3: console.log("向左"); break; case 4: console.log("向右"); break; default: break; } }, //根据起点和终点返回方向 1:向上,2:向下,3:向左,4:向右,0:未滑动 getslidedirection:function (startx, starty, endx, endy) { const that = this; let dy = starty - endy; let dx = endx - startx; let result = 0; //如果滑动距离太短 if(math.abs(dx) < 2 && math.abs(dy) < 2) { return result; } let angle = that.getslideangle(dx, dy); if(angle >= -45 && angle < 45) { result = 4; }else if (angle >= 45 && angle < 135) { result = 1; }else if (angle >= -135 && angle < -45) { result = 2; } else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) { result = 3; } return result; }, //返回角度 getslideangle:function (dx, dy) { return math.atan2(dy, dx) * 180 / math.pi; }
原生js方法
除了h5新增的方法外,还可以用原生js判断view的滑动方向,代码如下(可直接运行):
要注意的是chrome对document.body.scrolltop一直是0,需要改成document.documentelement.scrolltop
<!doctype html> <html> <head> <meta charset="utf-8"> <title> 移动技术网(jb51.net)</title> <style> div { border: 1px solid black; width: 200px; height: 100px; overflow: scroll; } </style> </head> <body style="overflow: scroll"> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <h1>hello word</h1> <script> function scroll( fn ) { var beforescrolltop = document.documentelement.scrolltop, fn = fn || function() {}; console.log('beforescrolltop',beforescrolltop); window.addeventlistener("scroll", function() { var afterscrolltop = document.documentelement.scrolltop, delta = afterscrolltop - beforescrolltop; console.log('beforescrolltop',beforescrolltop); console.log('afterscrolltop',afterscrolltop); if( delta === 0 ) return false; fn( delta > 0 ? "down" : "up" ); beforescrolltop = afterscrolltop; }, false); } scroll(function(direction) { console.log(direction) }); </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
网友评论