-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modal.vue
82 lines (82 loc) · 1.8 KB
/
Modal.vue
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<template>
<div class="overlay" v-if="visible"></div> <!-- 遮罩层 -->
<div class="container" ref="container" @click="visible=true">
<slot></slot>
<div class="modal" v-if="visible">
<div class="title">{{ title }}</div>
<div class="content">{{ content }}</div>
<div class="footer">
<button @click.stop="visible=false">确定!</button>
</div>
</div>
</div>
</template>
<script>
import {ref,watch,watchEffect} from 'vue';
export default{
props:{
title:{
type:String,
default:''
},
content:{
type:String,
default:''
},
duration:{ //持续时间可传可不传
type:Number,
}
},
setup(props){
// const container = ref(null)
const visible=ref(false)
console.log(props.duration)
watchEffect(()=>{
if(visible.value && props.duration!==undefined){
console.log("enter");
setTimeout(() => {
visible.value=false
}, props.duration);
}
})
return {
visible
}
}
}
</script>
<style scoped>
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
z-index: 999; /* 确保遮罩层在模态框上方 */
}
.modal{
position:fixed;
width: 250px;
height: auto;
/* border: 1px solid #000; */
background-color: aqua;
border-radius: 5px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1000; /* 确保模态框位于遮罩层之上 */
}
.title{
text-align: center;
}
.content{
padding: 5px;
}
.footer{
display: flex;
justify-content: flex-end;
padding: 5px;
border-top: 1px solid #000;
}
</style>