由于官方API提供的顯示模態(tài)彈窗,只能簡(jiǎn)單地顯示文字內(nèi)容,不能對(duì)對(duì)話(huà)框內(nèi)容進(jìn)行自定義,欠缺靈活性,所以自己從模態(tài)彈窗的原理角度來(lái)實(shí)現(xiàn)了自定義的模態(tài)對(duì)話(huà)框。
wx.showModal修改樣式后的效果,如下圖所示:

index.wxml代碼:
<!--index.wxml-->
<button class="show-btn" bindtap="showDialogBtn">彈窗</button>
<!--彈窗-->
<view class="modal-mask" bindtap="hideModal" catchtouchmove="preventTouchMove" wx:if="{{showModal}}"></view>
<view class="modal-dialog" wx:if="{{showModal}}">
<view class="modal-title">添加數(shù)量</view>
<view class="modal-content">
<view class="modal-input">
<input placeholder-class="input-holder" type="number" maxlength="10" bindinput="inputChange" class="input" placeholder="請(qǐng)輸入數(shù)量"></input>
</view>
</view>
<view class="modal-footer">
<view class="btn-cancel" bindtap="onCancel" data-status="cancel">取消</view>
<view class="btn-confirm" bindtap="onConfirm" data-status="confirm">確定</view>
</view>
</view>
index.wxss樣式代碼:
/**index.wxss**/
.show-btn {
margin-top: 100rpx;
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: 540rpx;
overflow: hidden;
position: fixed;
top: 50%;
left: 0;
z-index: 9999;
background: #f9f9f9;
margin: -180rpx 105rpx;
border-radius: 36rpx;
}
.modal-title {
padding-top: 50rpx;
font-size: 36rpx;
color: #030303;
text-align: center;
}
.modal-content {
padding: 50rpx 32rpx;
}
.modal-input {
display: flex;
background: #fff;
border: 2rpx solid #ddd;
border-radius: 4rpx;
font-size: 28rpx;
}
.input {
width: 100%;
height: 82rpx;
font-size: 28rpx;
line-height: 28rpx;
padding: 0 20rpx;
box-sizing: border-box;
color: #333;
}
input-holder {
color: #666;
font-size: 28rpx;
}
.modal-footer {
display: flex;
flex-direction: row;
height: 86rpx;
border-top: 1px solid #dedede;
font-size: 34rpx;
line-height: 86rpx;
}
.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代碼如下:
/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ì)話(huà)框
*/
hideModal: function () {
this.setData({
showModal: false
});
},
/**
* 對(duì)話(huà)框取消按鈕點(diǎn)擊事件
*/
onCancel: function () {
this.hideModal();
},
/**
* 對(duì)話(huà)框確認(rèn)按鈕點(diǎn)擊事件
*/
onConfirm: function () {
this.hideModal();
}
})