3、Teleport传送门
给整个屏幕添加蒙层
水平垂直居中的 CSS 样式是非常值得借鉴的!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="https://unpkg.com/vue@next"></script>
<!-- 样式 -->
<style>
.area{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
width: 200px;
height: 300px;
background-color: burlywood;
}
/* 写一个蒙层 */
.mask{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: black;
opacity: .5;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return {
show: false
}
},
methods:{
handleClick(){
this.show = !this.show;
}
},
template: `
<div class="area">
<button @click="handleClick">点我</button>
<div class="mask" v-show="show"></div>
</div>
`
});
const vm = app.mount('#root');
</script>
</html>
复制代码
运行结果
使用teleport传送门
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="https://unpkg.com/vue@next"></script>
<!-- 样式 -->
<style>
.area{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
width: 200px;
height: 300px;
background-color: burlywood;
}
/* 写一个蒙层 */
.mask{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: black;
opacity: .5;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return {
show: false
}
},
methods:{
handleClick(){
this.show = !this.show;
}
},
// body标签使用to="body"
// hello id使用to="#hello"
template: `
<div class="area">
<button @click="handleClick">点我</button>
<teleport to="body">
<div class="mask" v-show="show"></div>
</teleport>
</div>
`
});
const vm = app.mount('#root');
</script>
</html>
复制代码
运行结果
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END