elementUI自定义弹窗zIndex问题
更新时间: 2022-12-23 11:06:40
因为公司的大屏项目需要自定义弹窗的样式,我选用了elementUI作为UI框架,所以直接复制了elementUI源码的dialog组件源码然后改了样式,本以为这样就可以大功告成了,结果,实际使用的时候发现,自己封装的弹窗组件的zIndex始终和elementUI自己的其他弹窗zIndex要小。
# 分析原因
通过观察可以发现elementUI的弹窗zIndex是从2000开始往上累加的,但我们自己封装的组件zIndex和elementUI的似乎不是同一套,它们各自从2000开始累加起。
# 查看源码
通过看源码发现dialog组件中引入了Popup组件
// element-ui/packages/dialog/src/component.vue
import Popup from 'element-ui/src/utils/popup';
import Migrating from 'element-ui/src/mixins/migrating';
import emitter from 'element-ui/src/mixins/emitter';
1
2
3
4
2
3
4
然后去popup组件,找到引入了popup-manager
import Vue from 'vue';
import merge from 'element-ui/src/utils/merge';
import PopupManager from 'element-ui/src/utils/popup/popup-manager';
1
2
3
2
3
然后找到popup-manager组件,发现这里存放着关于zIndex修改的组件:
Object.defineProperty(PopupManager, 'zIndex', {
configurable: true,
get() {
if (!hasInitZIndex) {
zIndex = zIndex || (Vue.prototype.$ELEMENT || {}).zIndex || 2000;
hasInitZIndex = true;
}
return zIndex;
},
set(value) {
zIndex = value;
}
});
nextZIndex: function() {
return PopupManager.zIndex++;
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
因为所有组件引用的都是同一个PopupManager,所有会记录每个组件弹窗的zIndex,它们的zIndex是共享的,但是为什么我们自己封装的组件不行呢?
原因是在于我们自己封装的dialog,引用的是src中的,
import Popup from 'element-ui/src/utils/popup';
1
但实际上我们使用的dialog组件并不是引用源码,而应该引用编译出来的依赖库,依赖库的路径为:
import Popup from 'element-ui/lib/utils/popup';
1
到这里,再试一下,问题解决了~
# 完整自定义dialog代码
<template>
<transition
name="dialog-fade"
@after-enter="afterEnter"
@after-leave="afterLeave">
<div
v-show="visible"
class="el-dialog__wrapper"
@click.self="handleWrapperClick">
<div
role="dialog"
:key="key"
aria-modal="true"
:aria-label="title || 'dialog'"
:class="['el-dialog', { 'is-fullscreen': fullscreen, 'el-dialog--center': center }, customClass]"
ref="dialog"
:style="style">
<svg class="dv-border-svg-container" :width="dialogWidth" :height="dialogHeight">
<polygon class="border" :points="`
0,0
${dialogWidth * 0.3},0
${winWidth * 0.018 + dialogWidth * 0.3},${winWidth * 0.018}
${dialogWidth},${winWidth * 0.018}
${dialogWidth},${dialogHeight}
${winWidth * 0.01},${dialogHeight}
0,${dialogHeight - winWidth * 0.01}
`" />
</svg>
<div class="el-dialog__header">
<slot name="title">
<span class="el-dialog__title">{{ title }}</span>
</slot>
<button
type="button"
class="el-dialog__headerbtn"
aria-label="Close"
v-if="showClose"
@click="handleClose">
<i class="el-dialog__close el-icon el-icon-close"></i>
</button>
</div>
<div class="el-dialog__body" v-if="rendered"><slot></slot></div>
<div class="el-dialog__footer" v-if="$slots.footer">
<slot name="footer"></slot>
</div>
</div>
</div>
</transition>
</template>
<script>
import Popup from 'element-ui/lib/utils/popup';
import Migrating from 'element-ui/src/mixins/migrating';
import emitter from 'element-ui/src/mixins/emitter';
export default {
name: 'ElDialog',
mixins: [Popup, emitter, Migrating],
props: {
title: {
type: String,
default: ''
},
modal: {
type: Boolean,
default: true
},
modalAppendToBody: {
type: Boolean,
default: true
},
appendToBody: {
type: Boolean,
default: false
},
lockScroll: {
type: Boolean,
default: true
},
closeOnClickModal: {
type: Boolean,
default: true
},
closeOnPressEscape: {
type: Boolean,
default: true
},
showClose: {
type: Boolean,
default: true
},
width: String,
fullscreen: Boolean,
customClass: {
type: String,
default: ''
},
top: {
type: String,
default: '15vh'
},
beforeClose: Function,
center: {
type: Boolean,
default: false
},
destroyOnClose: Boolean
},
data() {
return {
closed: false,
key: 0,
winWidth:document.documentElement.getBoundingClientRect().width,
winHeight:document.documentElement.getBoundingClientRect().height,
dialogWidth:0,
dialogHeight:0
};
},
watch: {
visible(val) {
if (val) {
this.closed = false;
this.$emit('open');
this.$el.addEventListener('scroll', this.updatePopper);
this.$nextTick(() => {
this.$refs.dialog.scrollTop = 0;
});
if (this.appendToBody) {
document.body.appendChild(this.$el);
}
this.resize()
} else {
this.$el.removeEventListener('scroll', this.updatePopper);
if (!this.closed) this.$emit('close');
if (this.destroyOnClose) {
this.$nextTick(() => {
this.key++;
});
}
}
}
},
computed: {
style() {
let style = {};
if (!this.fullscreen) {
style.marginTop = this.top;
if (this.width) {
style.width = this.width;
}
}
return style;
}
},
methods: {
getMigratingConfig() {
return {
props: {
'size': 'size is removed.'
}
};
},
handleWrapperClick() {
if (!this.closeOnClickModal) return;
this.handleClose();
},
handleClose() {
if (typeof this.beforeClose === 'function') {
this.beforeClose(this.hide);
} else {
this.hide();
}
},
hide(cancel) {
if (cancel !== false) {
this.$emit('update:visible', false);
this.$emit('close');
this.closed = true;
}
},
updatePopper() {
this.broadcast('ElSelectDropdown', 'updatePopper');
this.broadcast('ElDropdownMenu', 'updatePopper');
},
afterEnter() {
this.$emit('opened');
},
afterLeave() {
this.$emit('closed');
},
resize(){
setTimeout(() => {
this.winWidth = document.documentElement.getBoundingClientRect().width
this.winHeight = document.documentElement.getBoundingClientRect().height
const dom = this.$refs['dialog']
this.dialogWidth = dom ? dom.clientWidth : 0
this.dialogHeight = dom ? dom.clientHeight : 0
},300)
}
},
mounted() {
if (this.visible) {
this.rendered = true;
this.open();
if (this.appendToBody) {
document.body.appendChild(this.$el);
}
window.addEventListener('resize',this.resize,false)
this.resize()
}
},
destroyed() {
// if appendToBody is true, remove DOM node after destroy
if (this.appendToBody && this.$el && this.$el.parentNode) {
this.$el.parentNode.removeChild(this.$el);
}
window.removeEventListener('resize',this.resize)
}
};
</script>
<style lang="scss" scoped>
.el-dialog{
background: transparent;
box-shadow:none;
.dv-border-svg-container {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
z-index:-1;
}
.border{
stroke:rgba(27, 196, 205, 1);
fill:rgba(0, 20, 53, 1);
stroke-width: 1;
}
.el-dialog__header{
padding:0.5vw;
text-align: left;
}
.el-dialog__title{
font-size:0.83rem;
color:#ffffff;
line-height: 2vw;
padding-left:0.83rem;
}
.el-dialog__headerbtn{
top:0.9vw;
right:-0.9vw;
width:1.8vw;
height:1.8vw;
border-radius: 50%;
background: #0C286F;
border: 1px solid #31CBFF
}
.el-dialog__headerbtn .el-dialog__close{
color:rgba(255,255,255,0.7);
&:hover{
color:#ffffff;
}
}
}
</style>
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
完成啦,这是一个用svg作为背景,支持自适应的弹窗组件~