步奏: 添加两个事件(手指按下时,手指松开时)

bindtouchstart="touchStart" //开始bindtouchend="touchEnd"//结束

即:wxml

view bindtouchstart="touchStart" bindtouchend="touchEnd" /view

wjs:

let touchDotX = 0;//X按下时坐标let touchDotY = 0;//y按下时坐标let interval;//计时器let time = 0;//从按下到松开共多少时间*100// 触摸开始事件  touchStart: function(e) {    touchDotX = e.touches[0].pageX; // 获取触摸时的原点    touchDotY = e.touches[0].pageY;    // 使用js计时器记录时间        interval = setInterval(function() {      time++;    }, 100);  },  // 触摸结束事件  touchEnd: function(e) {    let touchMoveX = e.changedTouches[0].pageX;    let touchMoveY = e.changedTouches[0].pageY;    let tmX = touchMoveX - touchDotX;    let tmY = touchMoveY - touchDotY;    if (time 20) {      let absX = Math.abs(tmX);      let absY = Math.abs(tmY);      if (absX 2 * absY) {        if (tmX0){          console.log("左滑=====")        }else{          console.log("右滑=====")        }      }      if (absY absX * 2 && tmY0) {        console.log("上滑动=====")      }    }    clearInterval(interval); // 清除setInterval    time = 0;  }

TAG标签: 微信小程序touchstart

http://www.xiaochengxub.com/read-103505.html