微信小程序自定义弹窗,默认同意活动规则
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">同意活动协议</view>
<view class="modal-content">
<checkbox-group bindchange="bindAcceptRule">
<label class="checkbox" >
<checkbox value="同意" class="checkbox-size" checked="checked" color="#FFA200" />
</label>
</checkbox-group>
<view class="size-24">我已经阅读并接受 <navigator url="" class="rule-text">《活动协议》</navigator>
</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
.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;
}
.size-24 {
font-size: 24rpx;
display: flex;
margin-left: 50rpx;
margin-top: -34rpx;
}
.rule-text {
color: rgb(87, 107, 149);
}
checkbox-group {
margin-top: 20rpx;
}
/* 默认状态 */
.checkbox .wx-checkbox-input {
border-radius: 32rpx; /* 圆角 */
width: 32rpx; /* 背景的宽 */
height: 32rpx; /* 背景的高 */
border: 2px solid rgba(255, 129, 92, 1);
}
/* 不可点击 */
.checkbox .wx-checkbox-input-disabled {
border-radius: 32rpx; /* 圆角 */
width: 32rpx; /* 背景的宽 */
height: 32rpx; /* 背景的高 */
background: #f3f3f3;
border: 1px solid rgba(201, 201, 201, 1);
}
/* 选择后 */
.checkbox .wx-checkbox-input.wx-checkbox-input-checked {
background: rgba(255, 129, 92, 1);
}
/* 打勾样式 */
.checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
width: 32rpx;
height: 32rpx;
line-height: 32rpx;
text-align: center;
font-size: 24rpx;
color: #fff;
background: transparent;
transform: translate(-50%, -50%) scale(1);
-webkit-transform: translate(-50%, -50%) scale(1);
}
index.js
var app = getApp()
Page({
data: {
showModal: false,
acceptRule: true
},
onLoad: function () {
},
// 同意规则
bindAcceptRule: function(e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
if (e.detail.value.length !== 0) {
this.data.acceptRule = true
}else{
this.data.acceptRule = false
}
},
/**
* 弹窗
*/
showDialogBtn: function () {
this.setData({
showModal: true
})
},
/**
* 弹出框蒙层截断touchmove事件
*/
preventTouchMove: function () {
},
/**
* 隐藏模态对话框
*/
hideModal: function () {
this.setData({
showModal: false
});
},
/**
* 对话框取消按钮点击事件
*/
onCancel: function () {
this.hideModal();
},
/**
* 对话框确认按钮点击事件
*/
onConfirm: function () {
if(this.data.acceptRule){
this.hideModal();
}else{
wx.showToast({
title: '请同意活动协议',
icon: 'none',
})
}
}
})