手机小程序滑动返回是什么事件

手机小程序滑动返回是什么事件,第1张

微信小程序的滑动事件是通过bindtouchmove实现的,通过比较滑动事件前后的坐标判断滑动方向,微信小程序通过三个事件共同作用实现了触摸滑动事件,即 bingtouchstart、bindtouchmove 和 bindtouchend 事件。

WXML:

<view class='btn' bindtouchstart='touchStart' bindtouchmove='touchMove' bindtouchend='touchEnd'>

OK

</view>

JS:

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('静止')

}

},

在 touchstart 时,监听到触摸开始时的 (x, y)位置;在 touchMove 方法中持续监听触摸点的位置(x, y),并保存在 data 中;在 touchEnd 方法中对开始的触摸位置和结束的触摸位置进行判断,如果移动距离大于 50 则判定为发生触摸滑动事件。

在上面示例中,当 X 轴方向的移动超过 50 时即判定为左滑或右滑,相应的也可以通过判断 Y 轴方向的滑动长度,来判断上滑或是下滑,由此实现触摸滑动的功能。

更多信息联系我的微

微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend。

这三个事件最重要的属性是pageX和pageY,表示X,Y坐标。

touchstart在触摸开始时触发事件

touchend在触摸结束时触发事件

touchmove触摸的过程中不断激发这个事件

这三个事件都有一个timeStamp的属性,查看timeStamp属性,可以看到顺序是touchstart =>touchmove=>touchmove =>··· =>touchmove =>touchend。

在微信小程序中可以通过绑定事件来实现滑动调整数值的功能。可以使用“touchmove”事件,在滑动时不断获取到当前滑块的位置。通过计算相关信息,可以得到需要调整的数值,有助于实现上述功能。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7724811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存