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

手机小程序滑动返回是什么事件,第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手指触摸后移动

touchcancel手指肆迅旁触摸动作被打断,如来电提醒,d窗

touchend手指触摸动作结束

tap手指触摸后马上离开

longtap手指触摸裂橡后,超过350ms再离开昌哗

请根据自己实际情况进行选择。

微信小程序 触控事件: 微信小程序的"事件"挺有意思。看了说明文档后发现它的功能很全,事件可以向父节点传递,而且打印这个事件的信息很透明,调试起来应该非常方便。 接下来把文档copy过来 原文地址:https://mp/debug/wxadoc/dev/framework/view/wxml/event.html 》》》什么是事件 事件是视图层到逻辑层的通讯方式。事件可以将用户的行为反馈到逻辑层进行处理。事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数。事件对象可以携带额外信息,如id, dataset, touches。事件的使用方式 在组件中绑定一个事件处理函数。 如bindtap,当用户点击该组件的时候会在该页面对应的Page中找到相应的事件处理函数。 <view id="tapTest" data-hi="MINA" bindtap="tapName">Click me! </view> 在相应的Page定义中写上相应的事件处理函数,参数是event。 Page({tapName: function(event) {console.log(event)} }) 可以看到log出来敏困的信息大致如下: { "type": "tap", "timeStamp": 1252, "target": {"id": "tapTest","offsetLeft": 0,"offsetTop": 0,"dataset": {"hi": "MINA"} }, "currentTarget": {"id": "tapTest","offsetLeft": 0,"offsetTop": 0,"dataset": {"hi": "MINA"} }, "touches": [{"pageX": 30,"pageY": 12,"clientX": 30,"clientY": 12,"screenX": 112,"screenY": 151 }], "detail": {"x": 30,"y": 12 } } 事件详解 事件分类 事件分为冒泡事件和非冒泡事件: 冒泡事件:当一个组件上的事件被触发后,该事件会向父节点传递。 非冒泡事件:当一个组件上的事件被触发后,该事件不会向父节点传递。 》》》事件分类 touchstart 手指触摸touchmove 手指触摸后移动touchcancel 手指触摸动作被打断,如d窗和来电提醒touchend 手指触摸动作结束tap 手指触摸后离开longtap 手指触摸后后,超过350ms离开》》》事件绑定 事件绑定的写法同组件的属性,以 key、value 的形式。 key 以bind或catch开头,羡拿行然后跟上事件的类型,如bindtap, catchtouchstartvalue 是一个字符串,需要在对应的 Page 中定义同名的函数。不然当触发事件的时候会报错。 bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡。上面简单介绍了小程序事件基础,是时候彰显"事件"的威力: 单击(tap)双击(dbtap)长按(longtap)滑兄哗动多点触控1.单击 单击事件由touchstart、touchend组成,touchend后触发tap事件。 <view> <button type="primary" bindtouchstart="mytouchstart" bindtouchend="mytouchend" bindtap="mytap">点我吧</button> </view> mytouchstart: function(e){ console.log(e.timeStamp + '- touch start') },mytouchend: function(e){ console.log(e.timeStamp + '- touch end') },mytap: function(e){ console.log(e.timeStamp + '- tap') } 2.双击 双击事件由两个单击事件组成,两次间隔时间小于300ms认为是双击;微信官方文档没有双击事件,需要开发者自己定义处理。 <view> <button type="primary" bindtap="mytap">点我吧</button> </view> 3.长按 长按事件手指触摸后,超过350ms再离开。 <view> <button type="primary" bindtouchstart="mytouchstart" bindlongtap="mylongtap"bindtouchend="mytouchend" bindtap="mytap">点我吧</button> </view> mytouchstart: function(e){ console.log(e.timeStamp + '- touch start') },//长按事件mylongtap: function(e){ console.log(e.timeStamp + '- long tap') },mytouchend: function(e){ console.log(e.timeStamp + '- touch end') },mytap: function(e){ console.log(e.timeStamp + '- tap') }单击、双击、长按属于点触事件,会触发touchstart、touchend、tap事件,touchcancel事件只能在真机模拟,不多说了。 事件触发顺序单击touchstart → touchend → tap双击touchstart → touchend → tap → touchstart → touchend → tap长按touchstart → longtap → touchend → tap 4.滑动 手指触摸屏幕并移动,为了简化起见,下面以水平滑动和垂直滑动为例。 滑动事件由touchstart、touchmove、touchend组成 坐标图: 以屏幕左上角为原点建立直角坐标系。第四象限为手机屏幕,Y轴越往下坐标值越大(注意跟数学象限的区别)。假设A点为touchstart事件触摸点,坐标为A(ax,ay),然后手指向上滑动到点B(bx,by),就满足条件by <ay 同理,向右滑动到C(cx,cy),满足cx >ax;向下滑动到D(dx,dy),满足dy >ay;向左移动到E(ex,ey)满足ex <ax.计算线段AB在Y轴上投影长度为m,在X轴上的投影长度为n计算r = m/n,如果r >1,视为向上滑动。同理计算线段AC,AD,AE在Y轴投影长度与X轴的投影长度之比,得出向右向下向左的滑动。以上没考虑r为1的情况。 <view> <button type="primary" bindtouchstart="mytouchstart" bindtouchmove="mytouchmove">点我吧</button> </view> 5.多点触控 由于模拟器尚不支持多点触控,内测开放后,继续补充。 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存