# 本案例使用到 关闭冒泡 和v-show 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="vue.js"></script>
<style>
#overlay {
/* 透明度 */
background: rgba(0, 0, 0, 0.6);
width: 100%;
margin: auto;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#center {
background: #ffff;
border-radius: 5px;
/* 边框圆角 */
padding-top: 15px;
padding-left: 30px;
padding-bottom: 15px;
width: 290px;
height: 160px;
position: fixed;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
</style>
</head>
<body>
<div id="box">
<!-- 点击显示模态框 -->
<button @click="isShow=!isShow">模态框</button>
<!-- 方案一: stop-->
<!-- 页面加载时,不显示模态框 ; 点击页面关闭模态框-->
<!-- <div id="overlay" v-show="isShow" @click="isShow=false"> -->
<!-- 页面冒泡会导致,点击输入框也会关闭模态框,所以需要添加关闭冒泡事件 -->
<!-- <div id="center" @click.stop> -->
<!-- 用户名:<input> -->
<!-- <button @click="isShow=false">登录</button> -->
<!-- </div> -->
<!-- </div> -->
<!-- 方案二: self-->
<!-- 页面冒泡会导致,点击输入框也会关闭模态框,所以需要添加关闭冒泡事件 -->
<div id="overlay" v-show="isShow" @click.self="isShow=false">
<div id="center">
用户名:<input>
<button @click="isShow=false">登录</button>
</div>
</div>
</div>
<script>
var obj = {
data() {
return {
isShow:false
}
}
}
var app = Vue.createApp(obj).mount("#box")
</script>
</body>
</html>