<template>
|
<view v-if="visible" class="modal-container" @touchmove.stop.prevent>
|
<!-- 遮罩层 -->
|
<view class="modal-mask" @click="handleMaskClick"></view>
|
|
<!-- 弹窗主体 -->
|
<view class="modal-content" :class="['modal-animation-' + animationType, customClass]"
|
:style="{ width: width }">
|
<!-- 标题区 -->
|
<view class="modal-header" v-if="title || $slots.header">
|
<slot name="header">
|
<text class="modal-title">{{ title }}</text>
|
</slot>
|
<view v-if="showClose" class="close-btn" @click="close">
|
<text class="iconfont icon-close">×</text>
|
</view>
|
</view>
|
|
<!-- 内容区 -->
|
<view class="modal-body">
|
<slot></slot>
|
</view>
|
|
<!-- 底部按钮区 -->
|
<view v-if="showFooter || $slots.footer" class="modal-footer">
|
<slot name="footer">
|
<button class="modal-btn cancel-btn" @click="handleCancel">{{ cancelText }}</button>
|
<button class="modal-btn confirm-btn" @click="handleConfirm">{{ confirmText }}</button>
|
</slot>
|
</view>
|
</view>
|
</view>
|
</template>
|
|
<script>
|
export default {
|
name: 'BaseModal',
|
props: {
|
value: { // Vue2的v-model传值
|
type: Boolean,
|
default: false
|
},
|
title: String,
|
width: {
|
type: String,
|
default: '600rpx'
|
},
|
cancelText: {
|
type: String,
|
default: '取消'
|
},
|
confirmText: {
|
type: String,
|
default: '确定'
|
},
|
showClose: {
|
type: Boolean,
|
default: true
|
},
|
showFooter: {
|
type: Boolean,
|
default: true
|
},
|
maskClosable: {
|
type: Boolean,
|
default: true
|
},
|
animationType: {
|
type: String,
|
default: 'fade', // fade/scale/slide-up
|
validator: (val) => ['fade', 'scale', 'slide-up'].includes(val)
|
},
|
customClass: String
|
},
|
data() {
|
return {
|
visible: this.value
|
}
|
},
|
watch: {
|
value(newVal) {
|
this.visible = newVal
|
},
|
visible(newVal) {
|
this.$emit('input', newVal) // Vue2的v-model事件
|
}
|
},
|
methods: {
|
close() {
|
this.visible = false
|
this.$emit('close')
|
},
|
handleMaskClick() {
|
if (this.maskClosable) this.close()
|
},
|
handleCancel() {
|
this.$emit('cancel')
|
this.close()
|
},
|
handleConfirm() {
|
this.$emit('confirm')
|
}
|
}
|
}
|
</script>
|
|
<style scoped>
|
/* 基础布局 */
|
.modal-container {
|
position: fixed;
|
top: 0;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
z-index: 9999;
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
}
|
|
.modal-mask {
|
position: absolute;
|
top: 0;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
background-color: rgba(0, 0, 0, 0.5);
|
}
|
|
.modal-content {
|
position: relative;
|
background-color: #fff;
|
border-radius: 16rpx;
|
box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
|
overflow: hidden;
|
z-index: 1;
|
}
|
|
/* 动画效果 */
|
.modal-animation-fade {
|
opacity: 0;
|
transition: opacity 0.3s;
|
}
|
|
.modal-animation-scale {
|
opacity: 0;
|
transform: scale(0.8);
|
transition: all 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
|
}
|
|
.modal-animation-slide-up {
|
opacity: 0;
|
transform: translateY(100px);
|
transition: all 0.3s ease-out;
|
}
|
|
.modal-container[v-if] .modal-animation-fade,
|
.modal-container[v-if] .modal-animation-scale,
|
.modal-container[v-if] .modal-animation-slide-up {
|
opacity: 1;
|
transform: scale(1) translateY(0);
|
}
|
|
/* 头部样式 */
|
.modal-header {
|
padding: 30rpx;
|
display: flex;
|
justify-content: space-between;
|
align-items: center;
|
border-bottom: 1rpx solid #f5f5f5;
|
}
|
|
.modal-title {
|
font-size: 32rpx;
|
font-weight: bold;
|
color: #333;
|
}
|
|
.close-btn {
|
width: 40rpx;
|
height: 40rpx;
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
color: #999;
|
font-size: 36rpx;
|
}
|
|
/* 内容区样式 */
|
.modal-body {
|
padding: 30rpx;
|
max-height: 60vh;
|
overflow-y: auto;
|
}
|
|
/* 底部按钮区 */
|
.modal-footer {
|
display: flex;
|
justify-content: flex-end;
|
/* 按钮右对齐 */
|
align-items: center;
|
padding: 30rpx;
|
border-top: 1rpx solid #f5f5f5;
|
}
|
|
.modal-btn {
|
height: 70rpx;
|
min-width: 160rpx;
|
margin: 0;
|
margin-left: 20rpx;
|
padding: 0 30rpx;
|
border-radius: 35rpx;
|
font-size: 28rpx;
|
line-height: 70rpx;
|
}
|
|
.cancel-btn {
|
background-color: #f5f5f5;
|
color: #666;
|
}
|
|
.confirm-btn {
|
height: 70rpx;
|
min-width: 160rpx;
|
margin: 0;
|
margin-left: 20rpx;
|
padding: 0 30rpx;
|
border-radius: 35rpx;
|
font-size: 28rpx;
|
line-height: 70rpx;
|
background-color: #007AFF;
|
color: #fff;
|
}
|
|
.modal-content {
|
/* 强制显示 */
|
opacity: 1 !important;
|
transform: none !important;
|
}
|
</style>
|