normal;">小程序的"事件"挺有意思。看了说明文档后发现它的功能很全,事件可以向父节点传递,而且打印这个事件的信息很透明,调试起来应该非常方便。
什么是事件
事件是视图层到逻辑层的通讯方式。
事件可以将用户的行为反馈到逻辑层进行处理。
事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数。
事件对象可以携带额外信息,如id, dataset, touches。
事件的使用方式
如bindtap,当用户点击该组件的时候会在该页面对应的Page中找到相应的事件处理函数。
rush:html;toolbar:false">Click me!
在相应的Page定义中写上相应的事件处理函数,参数是event。
rush:html;toolbar:false">Page({ tapName: function(event) { console.log(event) } })
可以看到log出来的信息大致如下:
rush:html;toolbar:false">{ "type":"tap", "timeStamp":895, "target": { "id": "tapTest", "dataset":{ "hi":"WeChat" } }, "currentTarget":{ "id": "tapTest", "dataset": { "hi":"WeChat" } }, "detail": { "x":53, "y":14 }, "touches":[{ "identifier":0, "pageX":53, "pageY":14, "clientX":53, "clientY":14 }], "changedtouches":[{ "identifier":0, "pageX":53, "pageY":14, "clientX":53, "clientY":14 }] }
事件详解
事件分类
事件分为冒泡事件和非冒泡事件:
冒泡事件:当一个组件上的事件被触发后,该事件会向父节点传递。
非冒泡事件:当一个组件上的事件被触发后,该事件不会向父节点传递。
WXML的冒泡事件列表:
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">类型 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">触发条件 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">最低版本 | |||
---|---|---|---|---|---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">touchstart | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸动作开始Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">touchmove | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸后移动Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">touchcancel | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸动作被打断,如来电提醒,弹窗Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">touchend | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸动作结束Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">tap | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸后马上离开Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">longpress | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">手指触摸后,超过350ms再离开,如果指定了事件回调函数并触发了这个事件,tap事件将不被触发Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">
事件绑定
事件绑定的写法同组件的属性,以 key、value 的形式。
key 以bind或catch开头,然后跟上事件的类型,如bindtap, catchtouchstart
value 是一个字符串,需要在对应的 Page 中定义同名的函数。不然当触发事件的时候会报错。
bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡。
如在下边这个例子中,点击 inner view 会先后触发handleTap3和handleTap2(因为tap事件会冒泡到 middle view,而 middle view 阻止了 tap 事件冒泡,不再向父节点传递),点击 middle view 会触发handleTap2,点击 outter view 会触发handleTap1。
outer viewmiddle view inner view
事件对象
如无特殊说明,当组件触发事件时,逻辑层绑定该事件的处理函数会收到一个事件对象。
事件对象的属性列表:
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">StringBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">事件类型Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);"> | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60"> | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">ObjectBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">触发事件的组件的一些属性值集合Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60"> |
---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">ObjectBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">额外的信息 |
---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">属性 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">类型 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">说明 |
---|---|---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60"> | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">ArrayBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">触摸事件,当前变化的触摸点信息的数组
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; direction: ltr; margin: 0px; padding: 0.2em; border: none; color: inherit; background-color: rgb(247, 247, 247);">
中的触摸事件不可冒泡,所以没有 currentTarget。代表事件的类型。
页面打开到触发事件所经过的毫秒数。
触发事件的源组件。
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">属性 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">类型 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">说明 |
---|---|---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">id | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">StringBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">事件源组件的idBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">tagName | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">StringBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">当前组件的类型Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60"> | 开头的自定义属性组成的集合
事件绑定的当前组件。
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">ObjectBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">当前组件上由 |
---|
dataset
在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE。书写方式:以data-开头,多个单词由连字符-链接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.target.dataset 中会将连字符转成驼峰elementType。
示例:
rush:html;toolbar:false">DataSet Test
rush:html;toolbar:false">Page({ bindViewTap:function(event){ event.target.dataset.alphaBeta == 1 // - 会转为驼峰写法 event.target.dataset.alphabeta == 2 // 大写会转为小写 } })
touches
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">属性 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">类型 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">说明 |
---|---|---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">identifier | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">NumberBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">触摸点的标识符Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">pageX, pageY | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">NumberBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">距离文档左上角的距离,文档的左上角为原点 ,横向为X轴,纵向为Y轴Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; break-inside: avoid; border-top: 1px solid rgb(231, 231, 235);">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">clientX, clientY | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">NumberBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial; word-break: break-all;" height="60">距离页面可显示区域(屏幕除去导航条)左上角距离,横向为X轴,纵向为Y轴
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">属性 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">类型 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">说明 | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 6px 20px; border-width: initial; border-style: none; border-color: initial; font-weight: 400; color: rgb(141, 141, 141); text-align: left; height: 40px;">特殊说明 |
---|---|---|---|
Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">identifier | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">NumberBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">触摸点的标识符Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">x, y | Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">NumberBox-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">距离 Canvas 左上角的距离,Canvas 的左上角为原点 ,横向为X轴,纵向为Y轴Box-sizing: border-Box; -webkit-tap-highlight-color: transparent; text-size-adjust: none; -webkit-font-smoothing: antialiased; padding: 12px 20px; border-width: initial; border-style: none; border-color: initial;" height="60">
changedtouches 数据格式同 touches。 表示有变化的触摸点,如从无变有(touchstart),位置变化(touchmove),从有变无(touchend、touchcancel)。
自定义事件所携带的数据,如表单组件的提交事件会携带用户的输入,媒体的错误事件会携带错误信息,详见组件定义中各个事件的定义。
点击事件的detail 带有的 x, y 同 pageX, pageY 代表距离文档左上角的距离。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。