微信小程序--鼠标事件 & 点击事件返回值的target分析
来源:互联网 发布:手机mac过滤怎么设置 编辑:程序博客网 时间:2024/06/16 12:43
小程序鼠标事件" style="margin: 0.8em 0px; padding: 0px; box-sizing: border-box; font-weight: 100; line-height: 1.3em; font-size: 2.6em; color: rgb(63, 63, 63); font-family: 'microsoft yahei'; background-color: rgb(255, 255, 255);">微信小程序鼠标事件
事件分类
事件分为冒泡事件和非冒泡事件:
1. 冒泡事件(bind):当一个组件上的事件被触发后,该事件会向父节点传递。
2. 非冒泡事件(catch):当一个组件上的事件被触发后,该事件不会向父节点传递。
bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡。
WXML的冒泡事件列表
冒泡讲解
<view id="outter" bindtap="handleTap1"> outer view <view id="middle" catchtap="handleTap2"> middle view <view id="inner" bindtap="handleTap3"> inner view view> view>view>
点击inner view后只触发handleTap3,然后再触发handleTap2.不触发handleTap1。
因为handleTap2中的绑定类型是catch,阻止了冒泡事件。
返回对象
BaseEvent 基础事件对象属性列表:
type
代表事件的类型。
timeStamp
页面打开到触发事件所经过的毫秒数。
target
触发事件的源组件。
dataset
在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE。 书写方式: 以data-开头,多个单词由连字符-链接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.currentTarget.dataset 中会将连字符转成驼峰elementType。
示例: DataSet Test
Page({ bindViewTap:function(event){ event.currentTarget.dataset.alphaBeta === 1 // - 会转为驼峰写法 event.currentTarget.dataset.alphabeta === 2 // 大写会转为小写 }})
CustomEvent 自定义事件对象属性列表(继承 BaseEvent):
detail
自定义事件所携带的数据,如表单组件的提交事件会携带用户的输入,媒体的错误事件会携带错误信息,详见组件定义中各个事件的定义。
点击事件的detail 带有的 x, y 同 pageX, pageY 代表距离文档左上角的距离。
TouchEvent 触摸事件对象属性列表(继承 BaseEvent):
touches
touches 是一个数组,每个元素为一个 Touch 对象(canvas 触摸事件中携带的 touches 是 CanvasTouch 数组)。 表示当前停留在屏幕上的触摸点。
Touch 对象
changedTouches
changedTouches 数据格式同 touches。 表示有变化的触摸点,如从无变有(touchstart),位置变化(touchmove),从有变无(touchend、touchcancel)。
特殊事件:
bindtap
程序代码
Click me!
对应的js
Page({ tapName: function(event) { console.log(event) }})
输出结果
{"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}]}
可以看到,返回的type是tap
同时在target.id节点中也可以看到 对应的id
在a.target.dataset.hi中也可以找到对应的data-id的值(data-hi → hi)
实际内容以文档为准
微信小程序点击事件返回值的target分析
测试过程
在微信小程序中创建以下图片
然后在调试中点击下面第5个。
console返回两个e
第一个e
是第5块小块的e
第二个e
是下面全部9小块组成的大块的e
可以看到,currentTarget
节点是不一样的。
分析
在HTML或者WXML这些基于XML的树形结构的界面布局方式中,元素与元素之间是有层级关系的,子级元素上触发的事件,可以向父级元素逐层向上传递,所以,父级元素上也可以捕获子级元素上的事件并进行逻辑处理。
1. 使用 bind 开头的事件绑定,这种绑定不会阻止冒泡事件向上冒泡
2. 使用 catch 开头的事件绑定,这种绑定可以阻止冒泡事件向上冒泡
结论
event对象中
- target是事件产生的源头组件
- currentTarget则是当前捕获这个事件的组件。
(current - adj. 现在的; 最近的; 流行的; 流传的; n. 电流; 趋势; 水流; 涌流; )
target.id/currentTarget.id 为 目标事件的id
测试使用的代码
<view class="section"> <movable-area style="height: 300px;width: 300px; background: red;"> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">1movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">2movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">3movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">4movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">5movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">6movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">7movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">8movable-view> <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">9movable-view> movable-area><view style="height: 300px;width: 300px; background: red;" class="main" bindtap="viewmove"> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">1view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">2view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">3view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">4view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view" bindtap="viewmove">5view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">6view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">7view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">8view> <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">9view>view> <view class="btn-area"> <button size="mini" bindtap="tap">click me to move to (30px, 30px)button> view> 。 。 。 。 。。 。。 。view>
.k{ background: green; height: 100px; width: 100px; position:absolute;}movable-view{ height: 98px; width: 98px; background: blue; position:relative; border:1px dashed #fff;}.view{ height: 98px; width: 98px; background: blue; position:relative; border:1px dashed #fff; display: inline-block;}.main{ margin-top:10px;}
//index.js//获取应用实例var app = getApp()Page({ data: { motto: 'Hello World', userInfo: {}, x:0, y:0 }, onLoad: function () { console.log('onLoad') var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function(userInfo){ //更新数据 that.setData({ userInfo:userInfo }) }) }, tap: function(e) { this.setData({ x: 30, y: 30 });}, scroll:function(){ console.log("haha") }, move:function(e){ this.setData({ left:e.touches[0].clientX-60, top:e.touches[0].clientY-60 }) console.log(e) }, b1:function (e) { //console.log("e") console.log(e) //console.log(this.data.x) }, viewmove:function(e){ viewmove(e,this) }})function viewmove(e,that){ console.log(e)}
- 微信小程序--鼠标事件 & 点击事件返回值的target分析
- [微信小程序]微信小程序点击事件返回值的target分析
- 鼠标的点击事件
- div的鼠标点击事件
- [微信小程序]鼠标事件
- Android控件点击事件的返回值,
- 模拟鼠标点击事件
- 关于鼠标点击事件
- 鼠标点击事件
- watir鼠标点击事件
- js鼠标点击事件
- 鼠标点击事件
- 鼠标移动/点击事件
- 鼠标点击事件总结
- JavaScript鼠标点击事件
- 获取鼠标点击事件
- highcharts鼠标点击事件
- 触发鼠标点击事件
- 2015多校第7场 HDU 5372 Segment Game 树状数组,思维
- sudo 找不到 (-bash sudo : command not found)
- 微服务之服务发现的可行方案以及实践案例(四)
- 【SoapUI】接口测试之post上传文件方法思路
- CC2640R2F BLE5.0 BLE Device Monitor用户指南
- 微信小程序--鼠标事件 & 点击事件返回值的target分析
- ora-01034和ora-27101 续
- 转义字符在字符串中的运用
- jar包修改并重新打包
- redis集群--转
- 操作系统面试笔记
- logback去掉DubboMonitor烦人的INFO日志
- Docker学习总结(25)——阿里巴巴基于Java容器的多应用部署技术实践
- wireshark如何抓取本机包