Touch事件的tap,longtap封装--来自小野

mdn地址:

https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent

https://developer.mozilla.org/zh-CN/docs/Web/API/Touch

click 300ms延迟

等待是否有第二次点击,也就是双击行为,

PC上的dbclick在移动端也失效了

touchstart touchmove touchend touchcancel

touchcancel发生在什么时候?触摸滑动的过程中,有消息弹出,或者电话接入等情况时,这时会有覆盖在当前页面的消息处理,如果我们去操作消息等,就会触发touchcancel

TouchEvent

type:touchstart,touchmove,touchend,touchcancel

touchstart时的target就是touchmove,touchend事件发生时的target

touches:TouchList:保存触点

changedTouches:TouchList,与当前文档相关的触点

targetTouches:TouchList,作用在当前target上的触点

TouchList中有Touch对象

fastclick.js

https://github.com/ftlabs/fastclick/blob/main/lib/fastclick.js

swiper

https://www.swiper.com.cn/demo/index.html

封装及使用

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <link rel="stylesheet" href="">
  <style>
    html {
      /*按照iPhone5的宽 设置成100px */
      font-size: 26.66667vw;
    }

    #box {
      width: 1rem;
      height: 1rem;
      background-color: orange;
    }
  </style>
</head>

<body>
  <div id="box"></div>
  <script>
    const Touch = function (selector) {
      return Touch.prototype.init(selector)
    }
    Touch.prototype = {
      init: function (selector) {
        if (typeof selector === 'string') {
          this.elem = document.querySelector(selector)
        }
        return this
      },
      // 单击
      tap: function (callBack) {
        this.elem.addEventListener('touchstart', fn)
        this.elem.addEventListener('touchend', fn)
        let sTime;
        let eTime;

        function fn(e) {
          e.preventDefault()
          switch (e.type) {
            case 'touchstart':
              sTime = +new Date()
              break;
            case 'touchend':
              eTime = +new Date()
              if (eTime - sTime < 500) {
                callBack.call(this, e)
              }
            default:
              break;
          }
        }
      },
      longtap: function (callBack) {
        this.elem.addEventListener('touchstart', fn, false)
        this.elem.addEventListener('touchmove', fn, false)
        this.elem.addEventListener('touchend', fn, false)

        let t = null;
        let _self = this
        function fn(e) {
          e.preventDefault()
          switch (e.type) {
            case 'touchstart':
              t = setTimeout(() => {
                callBack.call(_self, e)
              }, 300);
              break;
            case 'touchmove':
              clearTimeout(t)
              t = null;
              break
            case 'touchend':
              clearTimeout(t)
              t = null;
              break
            default:
              break;
          }
        }

      }
    }

    window.$ = window.Touch = Touch
    $('#box').tap(function (e) {
      console.log(1)
      console.log(e.target)
    })
    $('#box').longtap(function (e) {
      console.log(2)
      console.log(e)
    })
  </script>
</body>

</html>

还有下面这篇文章对于fastclick的讲解也很详细,附上链接

fastclick 解决移动端click事件延迟300ms和点击穿透问题_程序不了猿的博客-CSDN博客_fastclick 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值