微信小程序监听左右滑动事件

直接赋代码,都有注释,直接用就可以了


  // 触摸开始事件
  touchStart: function (e) {
    // console.log(e.touches[0].pageX)
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchS = [sx, sy]
  },
  // 触摸滑动事件
  touchMove: function (e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.data.touchE = [sx, sy]
  },
  // 触摸结束事件
  touchEnd: function (e) {
    let start = this.data.touchS
    let end = this.data.touchE
    // console.log(start)
    // console.log(end)
    if (start[0] < end[0] - 50) {
      // console.log('向右滑,这里可以调用方法,及页面跳转事件')

    } else if (start[0] > end[0] + 50) {
      // console.log('向左滑,这里可以调用方法,及页面跳转事件')


    } else {
      // console.log('向上或向下滑动')
    }
  },

猜你喜欢

转载自blog.csdn.net/qq_42221857/article/details/132915996