组件代码
<template>
<!-- 遮罩层 -->
<div v-show="isMask" class="mask" @touchmove.stop @mousewheel.stop>
<!-- 内容 -->
<slot></slot>
</div>
</template>
<script>
export default {
props: {
// 是否显示加载动画
isMask: {
type: Boolean,
default: true
}
},
methods: {}
}
</script>
<style lang="less" scoped>
.mask {
width: 100vw;
height: 100vh;
position: fixed;
z-index: 1000;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
</style>
使用
<template>
<div class="box">
<eject :isMask="isMask">
<div class="teacherDetails">
</div>
</eject>
</div>
</template>
<script>
import eject from '@/components/mask'
export default {
name: '',
components: {
eject
},
data() {
return {
isMask: true //是否显示模态框
}
},
computed: {},
watch: {
isMask(newisMask, oldisMask) {
// 当模态框打开时,阻止当前页面滚动,当模态框关闭时恢复滚动(不影响模态框中的元素滚动)
if (newisMask) {
this.stopMove()
} else {
this.StartMove()
}
}
},
created() {
this.stopMove()
},
methods: {
/**
* @function 停止页面滚动(组件必须要这个)
*/
stopMove() {
let m = function(e) {
e.preventDefault()
}
document.body.style.overflow = 'hidden'
document.addEventListener('touchmove', m, { passive: false }) //禁止页面滑动
},
/**
* @function 开启页面滚动(组件必须要这个)
*/
StartMove() {
let m = function(e) {
e.preventDefault()
}
document.body.style.overflow = '' //出现滚动条
document.removeEventListener('touchmove', m, { passive: true })
},
},
}
</script>
<style scoped lang="less">
.teacherDetails {
position: relative;
box-sizing: border-box;
width: 960px;
height: 530px;
background: #fff;
border-radius: 10px;
}
</style>