小程序鼠标事件" 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);">微信小程序鼠标事件javascript 事件分类事件分为冒泡事件和非冒泡事件: 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。 返回对象BaseEvent 基础事件对象属性列表:
type表明事件的类型。canvas timeStamp页面打开到触发事件所通过的毫秒数。小程序 target触发事件的源组件。微信小程序
dataset数组 在组件中能够定义数据,这些数据将会经过事件传递给 SERVICE。 书写方式: 以data-开头,多个单词由连字符-连接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.currentTarget.dataset 中会将连字符转成驼峰elementType。微信 示例: Page({ bindViewTap:function(event){ event.currentTarget.dataset.alphaBeta === 1 // - 会转为驼峰写法 event.currentTarget.dataset.alphabeta === 2 // 大写会转为小写 } }) CustomEvent 自定义事件对象属性列表(继承 BaseEvent):
detail自定义事件所携带的数据,如表单组件的提交事件会携带用户的输入,媒体的错误事件会携带错误信息,详见组件定义中各个事件的定义。ide 点击事件的detail 带有的 x, y 同 pageX, pageY 表明距离文档左上角的距离。 TouchEvent 触摸事件对象属性列表(继承 BaseEvent):
touchestouches 是一个数组,每一个元素为一个 Touch 对象(canvas 触摸事件中携带的 touches 是 CanvasTouch 数组)。 表示当前停留在屏幕上的触摸点。 Touch 对象
changedToucheschangedTouches 数据格式同 touches。 表示有变化的触摸点,如从无变有(touchstart),位置变化(touchmove),从有变无(touchend、touchcancel)。
bindtap程序代码
对应的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分析测试过程在微信小程序中建立如下图片 而后在调试中点击下面第5个。 第二个 能够看到, 分析在HTML或者WXML这些基于XML的树形结构的界面布局方式中,元素与元素之间是有层级关系的,子级元素上触发的事件,能够向父级元素逐层向上传递,因此,父级元素上也能够捕获子级元素上的事件并进行逻辑处理。 结论event对象中
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) } |