Skip to content

Commit 15e5cff

Browse files
committed
事件处理详解
1 parent 51f8b15 commit 15e5cff

File tree

3 files changed

+141
-55
lines changed

3 files changed

+141
-55
lines changed

pages/home/home.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,64 @@
11
// pages/home/home.js
22
Page({
3-
4-
/**
5-
* 页面的初始数据
6-
*/
73
data: {
8-
4+
titles: ['衣服', '裤子', '鞋子']
95
},
10-
11-
/**
12-
* 生命周期函数--监听页面加载
13-
*/
14-
onLoad: function (options) {
15-
6+
handleBtnClick() {
7+
console.log('按钮发生点击')
168
},
17-
18-
/**
19-
* 生命周期函数--监听页面初次渲染完成
20-
*/
21-
onReady: function () {
22-
9+
handleTouchStart() {
10+
console.log('handleTouchStart')
2311
},
24-
25-
/**
26-
* 生命周期函数--监听页面显示
27-
*/
28-
onShow: function () {
29-
12+
handleTouchMove() {
13+
console.log('handleTouchMove')
3014
},
31-
32-
/**
33-
* 生命周期函数--监听页面隐藏
34-
*/
35-
onHide: function () {
36-
15+
handleTouchEnd() {
16+
console.log('handleTouchEnd')
3717
},
38-
39-
/**
40-
* 生命周期函数--监听页面卸载
41-
*/
42-
onUnload: function () {
43-
18+
handleTap() {
19+
console.log('handleTap')
4420
},
45-
46-
/**
47-
* 页面相关事件处理函数--监听用户下拉动作
48-
*/
49-
onPullDownRefresh: function () {
50-
21+
handleLongpress() {
22+
console.log('handleLongpress')
5123
},
52-
53-
/**
54-
* 页面上拉触底事件的处理函数
55-
*/
56-
onReachBottom: function () {
57-
24+
handleEventClick(event) {
25+
console.log('-------', event)
26+
},
27+
handleEventEnd(event) {
28+
console.log('+++++++', event)
29+
},
30+
handleInner(event) {
31+
console.log(event)
32+
},
33+
handleOuter(event) {
34+
console.log(event)
35+
},
36+
handleItemClick(event) {
37+
console.log(event)
38+
// title - index
39+
const dataset = event.currentTarget.dataset;
40+
const title = dataset.item;
41+
const index = dataset.index;
42+
console.log(title, index)
5843
},
5944

60-
/**
61-
* 用户点击右上角分享
62-
*/
63-
onShareAppMessage: function () {
64-
45+
// ---------- 事件冒泡和事件捕获
46+
handleCaptureView1() {
47+
console.log('handleCaptureView1')
48+
},
49+
handleBindView1() {
50+
console.log('handleBindView1')
51+
},
52+
handleCaptureView2() {
53+
console.log('handleCaptureView2')
54+
},
55+
handleBindView2() {
56+
console.log('handleBindView2')
57+
},
58+
handleCaptureView3() {
59+
console.log('handleCaptureView3')
60+
},
61+
handleBindView3() {
62+
console.log('handleBindView3')
6563
}
66-
})
64+
})

pages/home/home.wxml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
11
<!--pages/home/home.wxml-->
2-
<text class='title'>Hello World</text>
3-
<button size='mini'>按钮</button>
2+
<!-- 1.事件处理的回顾 -->
3+
<button bindtap='handleBtnClick' size='mini'>按钮</button>
4+
<button bind:tap='handleBtnClick' size='mini'>按钮</button>
5+
<button catch:tap='handleBtnClick' size='mini'>按钮</button>
6+
7+
<!-- 2.常见的一些事件 -->
8+
<view class='box'
9+
bind:touchstart="handleTouchStart"
10+
bind:touchmove="handleTouchMove"
11+
bind:touchend="handleTouchEnd"
12+
bind:tap="handleTap"
13+
bind:longpress="handleLongpress"></view>
14+
15+
<!-- 3.事件对象的分析 -->
16+
<button id='btn' size='mini'
17+
bindtap='handleEventClick'
18+
bindtouchend='handleEventEnd'>事件对象</button>
19+
20+
<view class='outer' id='outer' bindtap='handleOuter'>
21+
外层的view
22+
<view class='inner' id='inner' bindtap='handleInner'>内存的view</view>
23+
</view>
24+
25+
<!-- 4.事件的传递参数 -->
26+
<view class='container'>
27+
<block wx:for="{{titles}}" wx:key="{{index}}">
28+
<view class='item'
29+
bindtap='handleItemClick'
30+
data-index="{{index}}"
31+
data-item="{{item}}">
32+
{{item}}
33+
</view>
34+
</block>
35+
</view>
36+
37+
<!-- 5.事件冒泡和事件捕获 - catch和bind区别 -->
38+
<!-- bind: 一层层传递 -->
39+
<!-- catch: 阻止事件的进一步传递 -->
40+
<view class='view1' capture-bind:tap="handleCaptureView1" bindtap='handleBindView1'>
41+
<view class='view2' capture-bind:tap="handleCaptureView2" catchtap='handleBindView2'>
42+
<view class='view3' capture-bind:tap="handleCaptureView3" bindtap='handleBindView3'></view>
43+
</view>
44+
</view>

pages/home/home.wxss

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,50 @@
33
color: red;
44
font-size: 30px;
55
}
6+
7+
.box {
8+
width: 300rpx;
9+
height: 300rpx;
10+
background: orange;
11+
}
12+
13+
.outer {
14+
width: 400rpx;
15+
height: 400rpx;
16+
background: red;
17+
color: white;
18+
}
19+
20+
.inner {
21+
width: 200rpx;
22+
height: 200rpx;
23+
background: blue;
24+
color: white;
25+
}
26+
27+
.container {
28+
display: flex;
29+
}
30+
31+
.item {
32+
flex: 1;
33+
text-align: center;
34+
}
35+
36+
.view1 {
37+
width: 600rpx;
38+
height: 600rpx;
39+
background: red;
40+
}
41+
42+
.view2 {
43+
width: 400rpx;
44+
height: 400rpx;
45+
background: blue;
46+
}
47+
48+
.view3 {
49+
width: 200rpx;
50+
height: 200rpx;
51+
background: green;
52+
}

0 commit comments

Comments
 (0)