前端demo: jquery版本兼容性获取e.touches

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>scaleDemo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />

        <!--    jQuery版本兼容性 -->
        <!-- <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.0/jquery.min.js"></script> -->
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

        <!--    wap端调试工具 -->
        <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
        <script>
            var vConsole = new window.VConsole(); // VConsole 默认会挂载到 `window.VConsole` 上
        </script>
        <style>
            #img_wrapper {
              height: 500px;
              width: 500px;
              background: red;
            }
        </style>
    </head>
    <body>
        <div id="img_wrapper"></div>
        <script>
            var isDoubleTouch = false; //是否为多触点
            var start = []; //存放触点坐标
            $("#img_wrapper")
              .off("touchstart touchmove touchend")
              .on({
                touchstart: function (e) {
                  e.stopPropagation();
                  // jquery3.6.4 能够获取e.touches ;jquery1.12.0 不能获取e.touches
                  let touches = e.touches || getTouches(e);
                  if (touches && touches.length >= 2) {
                    //判断是否有两个点在屏幕上
                    isDoubleTouch = true;
                    start = touches; //获得第一组两个点
                  }
                },
                touchmove: function (e) {
                  let touches = e.touches || getTouches(e);
                  if (touches && touches.length >= 2 && isDoubleTouch) {
                    //手势事件
                    let now = touches; //获得第二组两个点
                    var scale =
                      getDistance(now[0], now[1]) / getDistance(start[0], start[1]); //获得缩放比例
                    scale > 1 ? plus() : minus();
                  }
                },
                touchend: function () {
                  if (isDoubleTouch) {
                    isDoubleTouch = false;
                  }
                },
              });

            /* 两点的距离 */
            function getDistance(p1, p2) {
              var x = p2.pageX - p1.pageX,
                y = p2.pageY - p1.pageY;
              return Math.sqrt(x * x + y * y);
            }

            function plus() {
              console.log("放大了");
            }

            function minus() {
              console.log("缩小了");
            }

            /* 处理jquery1.12.0 获取不到e.touches */
            function getTouches(event) {
              let touches = [];
              for (const [key, value] of Object.entries(
                event.originalEvent.touches
              )) {
                touches.push({
                  pageX: value.pageX,
                  pageY: value.pageY,
                });
              }
              return touches;
            }
        </script>
    </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值