本文實(shí)例為大家分享了微信小程序自定義支持圖片的彈窗,供大家參考,具體內(nèi)容如下
為index.wxml添加如下圖代碼: (微信小程序 - canvas層級(jí)最高問題,如何超越canvas的層級(jí),只能使用cover-view標(biāo)簽)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!--index.wxml--> < button class = "show-btn" bindtap = "showDialogBtn" >彈窗</ button > <!--彈窗--> < cover-view class = "modal-mask" bindtap = "hideModal" wx:if = "{{modal.isShow}}" ></ cover-view > < cover-view class = "modal-dialog" wx:if = "{{modal.isShow}}" > < cover-view class = "modal-title" >{{modal.title}}</ cover-view > < cover-view class = "modal-content" > < cover-image src = "{{modal.src}}" class = "img-full" style = "height:auto;" mode = "widthFix" ></ cover-image > </ cover-view > < cover-view class = "modal-footer" wx-if = "{{modal.isFooter}}" > < cover-view class = "btn-cancel" bindtap = "onCancel" data-status = "cancel" >{{modal.cancel}}</ cover-view > < cover-viewew class = "btn-confirm" bindtap = "onConfirm" data-status = "confirm" >{{modal.ok}}</ cover-viewew > </ cover-view > </ cover-view > |
修改樣式文件index.wxss,樣式代碼如下圖所示:
/index.wxss/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
.show-btn { margin-top : 100 rpx; color : #22cc22 ; } .modal-mask { width : 100% ; height : 100% ; position : fixed ; top : 0 ; left : 0 ; background : #000 ; opacity: 0.5 ; overflow : hidden ; z-index : 9000 ; color : #fff ; } .modal-dialog { width : 540 rpx; overflow : hidden ; position : fixed ; top : 50% ; left : 0 ; z-index : 9999 ; background : #f9f9f9 ; margin : -180 rpx 105 rpx; border-radius: 36 rpx; } .modal-title { padding-top : 50 rpx; font-size : 36 rpx; color : #030303 ; text-align : center ; } .modal-content { padding : 50 rpx 32 rpx; } .modal-input { display : flex; background : #fff ; border : 2 rpx solid #ddd ; border-radius: 4 rpx; font-size : 28 rpx; } .input { width : 100% ; height : 82 rpx; font-size : 28 rpx; line-height : 28 rpx; padding : 0 20 rpx; box-sizing: border-box; color : #333 ; } input-holder { color : #666 ; font-size : 28 rpx; } .modal-footer { display : flex; flex- direction : row; height : 86 rpx; border-top : 1px solid #dedede ; font-size : 34 rpx; line-height : 86 rpx; } .btn-cancel { width : 50% ; color : #666 ; text-align : center ; border-right : 1px solid #dedede ; } .btn-confirm { width : 50% ; color : #ec5300 ; text-align : center ; } |
index.js代碼如下圖所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
//index.js //獲取應(yīng)用實(shí)例 var app = getApp() Page({ data: { showModal: false , }, onLoad: function () { }, /** * 彈窗 */ showDialogBtn: function () { this .setData({ showModal: true }) }, /** * 彈出框蒙層截?cái)鄑ouchmove事件 */ preventTouchMove: function () { }, /** * 隱藏模態(tài)對(duì)話框 */ hideModal: function () { this .setData({ showModal: false }); }, /** * 對(duì)話框取消按鈕點(diǎn)擊事件 */ onCancel: function () { this .hideModal(); }, /** * 對(duì)話框確認(rèn)按鈕點(diǎn)擊事件 */ onConfirm: function () { this .hideModal(); } }) |
運(yùn)行,可以看到修改樣式后的效果
這里有個(gè)要特別注意的地方,就是下面這個(gè)方法:
1
|
preventTouchMove: function () { } |
為什么是空方法?因?yàn)橐Y(jié)合界面wxml看,蒙層view里有一個(gè)事件綁定
1
|
catchtouchmove= "preventTouchMove" 。 |
這養(yǎng)寫的原因是阻斷事件向下傳遞,避免在彈窗后還可以點(diǎn)擊或者滑動(dòng)蒙層下的界面。
如果不這樣寫的話,如果主界面是一個(gè)可以滾動(dòng)的界面,想想看,當(dāng)彈窗彈出的時(shí)候用戶還可以操作滾動(dòng)列表。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_43953710/article/details/103923184