代码
页面效果
主页面页面test1
<template>
<div class="test1">
<div class="mapView" @click="print('map')">map</div>
<test2></test2>
</div>
</template>
<script>
import test2 from "./test2.vue";
export default {
components: {
test2
},
data () {
return {
}
},
methods: {
print(code){
alert(code)
}
}
}
</script>
<style lang="less" scoped>
.mapView {
position: absolute;
z-index: 0;
width: 48%;
height: calc(90% );
left: 26%;
top: 9%;
background: rgba(255, 0, 0, .1);
}
</style>
子组件test2
<template>
<div class="test2">
<div class="left" @click="print('left')">left</div>
<div class="right" @click="print('right')">right</div>
</div>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
print(code) {
alert(code)
}
}
}
</script>
<style lang="less" scoped>
.left,
.right {
width: 23%;
height: calc(90% - 20px);
position: absolute;
z-index: 1000;
background: rgba(0, 0, 0, .1);
}
.left {
left: 2%;
}
.right {
right: 2%;
}
</style>
核心点:
两侧来自子组件,且使用pistion定位控制位置,脱离文档流,父盒子with100%,height:0;故主页面map盒子,不会被遮挡。即使子组件的父盒子高度100%,此时也不会遮盖map.
map盒子,本身是position定位,所以它只关注覆盖其上的div是否也是定位,且zindex层级是否高于自己。
便利处:主页面头部和map中间部位可以公用,两侧组件化,随条件不同,切换组件。
扩展内容
CSS的五种定位方式【哪一种脱离文档流】