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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<template>
<div class="modal-layout-root">
<transition name="modal-fade">
<div class="modal-container" v-show="visible">
<!-- 头部标题 -->
<div class="modal-header">
<div class="modal-title">{{ title }}</div>
<i class="iconfont close-icon" @click="close"></i>
</div>
<!-- 内容区域 -->
<div class="modal-content">
<slot name="content"></slot>
</div>
<!-- 底部按钮 -->
<div class="modal-footer">
<div class="modal-btn">
<button class="cancel" ref="cancelBtn" @click="close" @mousedown="cancelMouseDown" @mouseup="cancelMouseUp">取消</button>
<button class="ok" @click="ok">确认</button>
</div>
</div>
</div>
</transition>
<!-- 遮罩层 -->
<div class="mask" v-show="visible" @click="close"></div>
</div>
</template>
<script>
export default {
name: "modal",
data () {
return {}
},
props: {
// 模态框标题
title: {
type: String,
default: () => {
return '模态框标题'
},
},
// 显示隐藏控件
visible: {
type: Boolean,
default: () => {
return false
},
},
},
mounted () {
this.$nextTick(() => {
const body = document.querySelector("body");
if (body.append) {
body.append(this.$el);
} else {
body.appendChild(this.$el);
}
});
},
beforeDestroy () {
document.body.removeChild(this.$el)
},
methods: {
// 取消
close () {
this.$emit('cancel')
this.$emit('update:visible', false)
},
// 确认
ok () {
this.$emit('submit')
this.$emit('update:visible', false)
},
// 取消按钮 鼠标按下事件
cancelMouseDown () {
this.$refs.cancelBtn.style.color = '#096dd9'
this.$refs.cancelBtn.style.border = '1px solid #096dd9'
},
// 取消按钮 鼠标松开事件
cancelMouseUp () {
this.$refs.cancelBtn.style.color = '#595959'
this.$refs.cancelBtn.style.border = '1px solid #d9d9d9'
},
},
watch: {
// 操作遮罩层的展示/隐藏
visible () {
if (this.visible == true) {
document.querySelector('body').setAttribute('style', 'overflow:hidden !important;')
} else {
document.querySelector('body').removeAttribute('style')
}
},
},
}
</script>