<template>
<div>
<button @click="openDialog">打开对话框</button>
<div v-if="showDialog" class="dialog-wrapper">
<div class="dialog">
<h2 class="dialog-title">对话框标题</h2>
<p class="dialog-content">对话框内容</p>
<button @click="closeDialog" class="dialog-button">关闭</button>
</div>
</div>
</div>
</template>
<style>
.dialog-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(0, 0, 0, 0.5);
}
.dialog {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 80%;
}
.dialog-title {
font-size: 20px;
margin-bottom: 10px;
}
.dialog-content {
margin-bottom: 20px;
}
.dialog-button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
}
.dialog-button:hover {
background-color: #0056b3;
}
</style>
<script>
export default {
data() {
return {
showDialog: false
};
},
methods: {
openDialog() {
this.showDialog = true;
},
closeDialog() {
this.showDialog = false;
}
}
};
</script>
在这个示例中,当点击"打开对话框"按钮时,showDialog属性会被设置为true,从而显示对话框。对话框使用了一个半透明的遮罩层(.dialog-wrapper)来覆盖整个页面,并在中间显示一个白色背景的对话框(.dialog)。点击对话框中的"关闭"按钮时,showDialog属性会被设置为false,对话框将被隐藏。

网友评论