touchstart,touchmove判断手机中滑屏方向
2016-10-10 18:03
357 查看
滑动屏幕 touchstart:接触屏幕时触发,touchmove:活动过程触发,touchend:离开屏幕时触发
首先获取手接触屏幕时的坐标X,Y
//获取接触屏幕时的X和Y $('body').bind('touchstart',function(e){ startX = e.originalEvent.changedTouches[0].pageX, startY = e.originalEvent.changedTouches[0].pageY; });
然后获取滑动的坐标,并使用后面的坐标减去前面的坐标,通过获取的值判断其滑动方向。因为手滑动方向一般不是水平或者垂直的,所以可使用Math.abs()进行比较,比如:像右上角滑动,当往上滑动的距离大于往右的距离时,取其往上滑动的距离,即往上滑动。
$('body').bind('touchmove',function(e){ //获取滑动屏幕时的X,Y endX = e.originalEvent.changedTouches[0].pageX, endY = e.originalEvent.changedTouches[0].pageY; //获取滑动距离 distanceX = endX-startX; distanceY = endY-startY; //判断滑动方向 if(Math.abs(distanceX)>Math.abs(distanceY) && distanceX>0){ console.log('往右滑动'); }else if(Math.abs(distanceX)>Math.abs(distanceY) && distanceX<0){ console.log('往左滑动'); }else if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY<0){ console.log('往上滑动'); }else if(Math.abs(distanceX)<Math.abs(distanceY) && distanceY>0){ console.log('往下滑动'); }else{ console.log('点击未滑动'); } });
相关文章推荐
- touchstart,touchmove判断手机中滑屏方向
- touchstart,touchmove判断手机中滑屏方向
- 封装判断touch手指滑动方向的函数
- Unity3d 触屏手机滑动事件(上下左右方向精确判断)
- 移动互联网终端的touch事件,touchstart, touchend, touchmove
- html 移动互联网终端的javascript touch事件,touchstart, touchend, touchmove
- 移动互联网终端的touch事件判断方向
- html 移动互联网终端的javascript touch事件,touchstart, touchend, touchmove
- [前端] touch事件解析判断拖动方向
- 使用javascript实现手机上的touchmove效果
- ios开发-判断手机方向改变
- 移动互联网终端的touch事件,touchstart, touchend, touchmove
- js判断手机浏览器屏幕方向
- webapp上的蒙版,包含使用touchstart,touchmove
- android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色
- 用touchStart事件代替click处理手机页面上的触摸事件
- 用touchStart事件代替click处理手机页面上的触摸事件
- [转]html 移动互联网终端的javascript touch事件,touchstart, touchend, touchmove
- 使用jq实现手机上的touchmove效果
- jQuery -- touch事件之滑动判断(左右上下方向)