1.在view标签绑定上touchStart,touchMove,touchEnd事件
<view bindtouchstart='touchStart' bindtouchmove='touchMove' bindtouchend="touchEnd">监听左右滑动事件</view>
2.在js中写上
Page({
/**
* 页面的初始数据
*/
data: {
touchS : [0,0],
touchE : [0,0]
},
// 触摸开始事件
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('向上或向下滑动')
}
},
})