小程序官方提供了 wx.showModal
方法,但样式比较固定,不能知足多元化需求,自定义势在必行~css
老规矩先上图json
点击某个按钮,弹出 modal
框,里面的内容能够自定义,能够是简单的文字提示,也能够输入框等复杂布局。操做完点击取消或肯定关闭 modal
.小程序
将下面的 modal.wxml
、modal.wxss
、modal.js
、modal.json
四个文件复制到对应位置便可。bash
封装完以后调用起来也很简单,看看调用的代码吧app
<modal show="{{showModal}}" height='60%' bindcancel="modalCancel" bindconfirm='modalConfirm'>
<view class='modal-content'>
<formrow wx:for='{{goodsList}}' wx:key='{{index}}' title="{{item.name}}" placeholder='库存{{item.store}}' mode='input' rowpadding='10rpx' currentId="{{index}}" bindinput='goodsInput'></formrow>
</view>
</modal>
复制代码
在modal
中定义了 slot
,因此能够将须要展现的任何布局包裹在 modal
中。xss
上面的代码简化一下就是函数
<modal show="{{showModal}}" height='60%' bindcancel="modalCancel" bindconfirm='modalConfirm'>
<view class='modal-content'>你本身的布局</view>
</modal>
复制代码
须要传四个属性布局
show
:用来控制 modal
的显示隐藏。flex
height
: 定义 modal
的高度,能够是百分比,也能够是具体单位如 600rpx
。ui
bindcancel
:点击取消按钮的回调。
bindconfirm
:点击肯定按钮的回调。
本身的布局用一个 view
包起来放到 modal
标签里便可。
首先在你存放自定义组件的文件夹里新建个 modal
文件夹,我的习惯将全部组件都放在 components
下面。
而后右键新建 component
,注意是 component
不是 page
,由于要做为组件引入到页面中。
先看布局吧
<view class='mask' wx:if='{{show}}' bindtap='clickMask'>
<view class='modal-content' style='height:{{height}}'>
<scroll-view scroll-y class='main-content'>
<slot></slot>
</scroll-view>
<view class='modal-btn-wrapper'>
<view class='cancel-btn' style='color:rgba(7,17,27,0.6)' bindtap='cancel'>取消</view>
<view class='confirm-btn' style='color:#13b5f5' bindtap='confirm'>肯定</view>
</view>
</view>
</view>
复制代码
布局讲解
最外层是半透明的 mask
蒙版,覆盖了整个页面。里面是包裹内容的 view
,内容区有两层,上面是放置传入布局的主内容区,下面是取消和肯定两个按钮。
这里把 slot
用 scroll-view
包裹了起来,处理了传入的布局高度超出内容区域的问题,若是超出将会滚动。因此没必要担忧传入的布局高度,大胆用就行。
内容区的高度经过属性传入的 height
肯定,默认是 80%
.mask{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0,0,0,0.4);
z-index: 9999;
}
.modal-content{
display: flex;
flex-direction: column;
width: 90%;
/* height: 80%; */
background-color: #fff;
border-radius: 10rpx;
}
.modal-btn-wrapper{
display: flex;
flex-direction: row;
height: 100rpx;
line-height: 100rpx;
border-top: 2rpx solid rgba(7,17,27,0.1);
}
.cancel-btn, .confirm-btn{
flex: 1;
height: 100rpx;
line-height: 100rpx;
text-align: center;
font-size: 32rpx;
}
.cancel-btn{
border-right: 2rpx solid rgba(7,17,27,0.1);
}
.main-content{
flex: 1;
height: 100%;
overflow-y: hidden;
}
复制代码
css讲解
css没啥讲的,直接复制过去就行。
注意几个点:
将 .mask
的 z-index
设置的高一些,确保能在全部布局的最上层。
/**
* 自定义modal浮层
* 使用方法:
* <modal show="{{showModal}}" height='60%' bindcancel="modalCancel" bindconfirm='modalConfirm'>
<view>你本身须要展现的内容</view>
</modal>
属性说明:
show: 控制modal显示与隐藏
height:modal的高度
bindcancel:点击取消按钮的回调函数
bindconfirm:点击肯定按钮的回调函数
使用模块:
场馆 -> 发布 -> 选择使用物品
*/
Component({
/**
* 组件的属性列表
*/
properties: {
//是否显示modal
show: {
type: Boolean,
value: false
},
//modal的高度
height: {
type: String,
value: '80%'
}
},
/**
* 组件的初始数据
*/
data: {
},
/**
* 组件的方法列表
*/
methods: {
clickMask() {
// this.setData({show: false})
},
cancel() {
this.setData({ show: false })
this.triggerEvent('cancel')
},
confirm() {
this.setData({ show: false })
this.triggerEvent('confirm')
}
}
})
复制代码
Js 代码也很简单,在 properties
中定义两个需传入的属性 show
和 height
,并指定默认值。
在 methods
中写点击取消和肯定按钮的回调,点击按钮后先经过 this.setData({ show: false })
将 modal
隐藏掉,再经过 this.triggerEvent('confirm')
将点击事件传递出去。
{
"component": true,
"usingComponents": {}
}
复制代码
json 主要是声明 modal
为组件
以上就是简单的 modal
弹窗封装。若是不想要下面的肯定取消两个按钮,内容区的全部内容都要外部传入,能够这样写
<view class='mask' wx:if='{{show}}' bindtap='clickMask'>
<slot></slot>
</view>
复制代码
若是须要多个 slot
也能够,小程序都支持。
具体怎么实现看具体的业务需求吧,自定义的组件就是灵活性很是高,能够根据业务需求进行调整。