微信小程序 自制模态框

wxml

1
2
3
4
<view class='modal-mask' bindtap='hideModal' catchtouchmove="true"></view>
<view class='modal-dialog' catchtouchmove="true" wx:if='{{showModal}}'>
// 这里填充模态框内要展示的内容
<view>

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Page({
/**
* 页面的初始数据
*/
data: {
showModal: true
},

onLoad: function () {

},

hideModal() {
this.setData({
showModal: false
});
}
})

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
.modal-mask {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background: #000;
opacity: 0.5;
overflow: hidden;
z-index: 9998;
}

.modal-dialog {
width: 600rpx; //模态框的宽
height: 800rpx; //模态框的高
position: fixed;
top: 13%;
left: 10%; //topleft控制模态框的位置
//如要居中
//top: 50%;
//left: 50%;
//transform: translate(-50%,-50%);
z-index: 9999;
background: #fff;
border-radius: 20rpx;
display: flex;
flex-flow: column nowrap; //子元素纵向排列,不换行
justify-content: flex-start;
align-items: center;
}

这只是一个简单的例子,主要是wxmlwxss部分,至于显示和隐藏,根据自己需求来写。

作者

DullSword

发布于

2019-07-24

更新于

2024-07-02

许可协议

评论