弹出层设计
Vue代码组织如下:
<template>
<div class="modal-container" v-if="show" @click="hideModal">
<div class="modal-view">
<div v-if="pure">
<span :class="iconClass"></span>
<span class="title global-short-text">{{title}}</span>
<div class="content global-short-text">{{content}}</div>
</div>
<slot></slot>
</div>
<div class="mask"></div>
</div>
</template>
<script>
import { eventBus } from '../utils';
let htmlDom, bodyDom;
export default {
data() {
return {
show: false,
title: "title",
content: "content",
pure: true,
success: true
}
},
mounted() {
eventBus.$on("showModal", this.showModal);
eventBus.$on("hideModal", this.hideModal);
htmlDom = document.getElementsByTagName("html")[0];
bodyDom = document.getElementsByTagName("body")[0];
},
beforeDestroy() {
htmlDom = null;
bodyDom = null;
},
computed: {
iconClass: function (params) {
return this.success ? 'iconfont iconsuccess modal-icon-success' : 'iconfont iconfail modal-icon-fail'
}
},
methods: {
showModal: function ({ title, content, pure = true, success = true }) {
this.show = true;
this.pure = pure;
this.title = title;
this.content = content;
this.success = success;
window.scrollTo(0, 0);
htmlDom.style.overflow = "hidden";
bodyDom.style.overflow = "hidden";
htmlDom.style.position = "fixed";
bodyDom.style.position = "fixed";
eventBus.$emit("banIframeScroll", "no"); //弹框出现的同时禁止滑动
},
hideModal: function () {
this.show = false;
htmlDom.style.overflowX = "hidden";
htmlDom.style.overflowY = "scroll";
bodyDom.style.overflowX = "hidden";
bodyDom.style.overflowY = "scroll";
htmlDom.style.position = "";
bodyDom.style.position = "";
eventBus.$emit("banIframeScroll", "auto"); //弹框出现的同时禁止滑动
},
noscroll: function (e) {
e.preventDefault();
}
}
}
</script>
<style>
.modal-container {
z-index: 999999;
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.8;
overflow: hidden;
}
.modal-view {
z-index: 999999;
width: 80%;
min-height: 200px;
background-color: white;
text-align: center;
position: absolute;
top: 25%;
border-radius: 5px;
padding: 10px;
opacity: 1;
}
.title {
font-size: 28px;
}
.content {
font-size: 24px;
position: relative;
top: 40px;
}
.modal-icon-success {
font-size: 30px;
color: var(--global-color);
}
.modal-icon-fail {
font-size: 30px;
color: red;
}
</style>
基于事件触发弹出和隐藏,这样做的好处就是在整个DOM树上只要挂载一处,挂载一次即可。内不展示蓉蓉可以通过配置参数传递,也支持slot。
滑动问题
一开始,在谷歌浏览器上使用对body的样式修改overflow为”hidden”就可以实现弹出层下滑动禁用。但是在真机上测试的时候发现并没有用。
我怀疑是的overflow的问题,所以对也做了修改。这样android手机基本没有问题了,但是我们的基础页面内使用了
<iframe
v-if="showIframe"
name="report-iframe"
id="report-iframe"
title="report webview"
:src="reportUrl"
:scrolling="iframeScrolling"//重点是这里
></iframe>
核心就是在弹出层show的时候,将iframe的scrolling设置为”no”。但是这样还是未能做到兼容IOS系统,于是在网上又搜到了一篇文章帮到了我:链接
核心就是:将html和body的position也动态的修改为”fixed”(固定)就好了。
这样就做到了移动端弹出层show的时候,底层页面无法滑动,而隐藏弹出层之后又可以滑动的效果。