移动端监听滑动事件

本文详细介绍了如何使用JavaScript的addEventListener和removeEventListener方法来监听和移除滑动事件。通过示例代码展示了如何在touchstart、touchmove和touchend事件中捕获用户的滑动操作,并根据滑动距离执行相应逻辑,例如在滑动超过一定阈值时触发轮播下一页。
摘要由CSDN通过智能技术生成

addEventListener()与removeEventListener()可参考:https://blog.csdn.net/qq_29606781/article/details/67650869

    // 监听滑动事件
    let startPosition = {},
      deltaY = 0,
      endPosition = {};
    document.addEventListener('touchstart', touchFunc, false);
    document.addEventListener('touchmove', touchFunc, false);
    document.addEventListener('touchend', touchFunc, false);
    function touchFunc(e) {
      let event = e || window.e;
      let touch = {};
      switch (event.type) {
        case 'touchstart':
          deltaY = 0;
          touch = event.touches[0];
          startPosition = {
            x: touch.pageX,
            y: touch.pageY
          };
          break;
        case 'touchend':
          console.log(deltaY); //根据滑动的距离 让轮播下一页
          if (deltaY < -100 ) {
            swipe.value.next();
            isTouch.value = false;
          }
          break;
        case 'touchmove':
          touch = event.touches[0];
          endPosition = {
            x: touch.pageX,
            y: touch.pageY
          };
          deltaY = endPosition.y - startPosition.y;
          break;
      }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值