首页 > 作文

html5触摸事件判断滑动方向的实现

更新时间:2023-04-03 08:33:12 阅读: 评论:0

为了给触摸界面提供有力支持, 触摸事件提供了响应用户对触摸屏或者触摸板上操作的能力.

接口

touchevent

touchevent 是一类描述手指在触摸平面(触摸屏、触摸板等)的状态变化的事件。这类事件用于描述一个或多个触点,使开发者可以检测触点的移动,触点的增加和减少,等等。每 个 touch 对象代表一个触点; 每个触点都由其位置,大小,形状,压力大剪纸图片简单的又漂亮小,和目标 element 描述。 touchlist 对象代表多个触点的一个列表.

触摸事件的类型

为了区别触摸相关的状态改变,存在多种类型的触摸事件。可以通过检查触摸事件的 touchevent.type 属性来确定当前事件属于哪种类型

touchstart:当用户在触摸平面上放置了一个触点时触发。touchend:当一个触点被用户从触摸平面上移除(当用户将一个手指离开触摸平面)时触发。touchmove:当用户在触摸平面上移动触点时触发。touchcancel:当触点由于某些原因被中断时触发。

判断滑动方向

基本原理就是记录开始滑动(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) {      ca 0:        console.log("没滑动");        break;      ca 1:        console.log("向上");        break;      ca 2:        console.log("向下");        break;      ca 3:        console.log("向左");        break;      ca 4:        console.log("向右");        break;      default:        break;    }  },//根据起点和终点返回方向 1:向上,2:向下,3:向左,4:向右,0:未滑动  getslid请讲普通话edirection: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;    }el if (angle >= 45 && angle < 135) {      result = 1;    }el if (angle >= -135 && angle < -45) {      result = 2;    }    el 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 chart="utf-8">  <title> www.887551.com(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 fal;      fn( delta > 0 ? "down" : "up" );      beforescrolltop = afterscrolltop;    }, fal);  }  scroll(function(direction) { console.log(direction) });</script></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-03 08:33:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d078788bcec094d9262ce1b29c8e0e2d.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:html5触摸事件判断滑动方向的实现.doc

本文 PDF 下载地址:html5触摸事件判断滑动方向的实现.pdf

标签:触点   事件   平面   用户
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图