<template>
<div>
<!-- 触发弹框的按钮 -->
<button @click="showModal = true">打开弹框</button>
<!-- 弹框组件 -->
<modal v-if="showModal" @close="showModal = false">
<!-- 弹框内容 -->
<h3 slot="header">提示</h3>
<p slot="body">这是一个 Vue 弹框示例。</p>
<footer slot="footer">
<button @click="showModal = false">关闭</button>
</footer>
</modal>
</div>
</template>
<script>
export default {
data() {
return {
showModal: false // 控制弹框显示/隐藏的变量
};
}
};
</script>
<!-- 弹框样式 -->
<style scoped>
/* 可以根据需要自定义样式 */
</style>
<!-- 弹框组件定义 -->
<template>
<transition name="modal">
<div class="modal-mask">
<div class="modal-wrapper">
<div class="modal-container">
<slot name="header"></slot>
<slot name="body"></slot>
<slot name="footer"></slot>
</div>
</div>
</div>
</transition>
</template>
<script>
export default {
name: 'Modal',
methods: {
close() {
this.$emit('close');
}
}
};
</script>
<style scoped>
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: table;
transition: opacity 0.3s ease;
}
.modal-wrapper {
display: table-cell;
vertical-align: middle;
}
.modal-container {
width: 300px;
margin: 0px auto;
padding: 20px 30px;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
transition: all 0.3s ease;
font-family: Helvetica, Arial, sans-serif;
}
.modal-header h3 {
margin-top: 0;
color: #42b983;
}
.modal-body {
margin: 20px 0;
}
.modal-default-button {
float: right;
}
/*
* The following styles are auto-applied to elements with
* transition="modal" when their visibility is toggled
* by Vue.js.
*/
.modal-enter {
opacity: 0;
}
.modal-leave-active {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave-active .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
</style>
<template> 标签内定义了 HTML 结构。这里有一个按钮用于触发弹框,以及一个 modal 组件用于显示弹框。<script> 标签内定义了 Vue 组件的逻辑。data 函数返回一个对象,其中包含控制弹框显示与隐藏的 showModal 变量。当点击按钮时,showModal 的值会变为 true,从而显示弹框;点击关闭按钮时,showModal 的值会变为 false,从而隐藏弹框。<style scoped> 标签内定义了弹框的样式,包括背景遮罩、弹框容器的样式等。Modal 组件,用于封装弹框的结构和样式。这个组件可以通过插槽 (slot) 来传递不同的内容(如标题、正文、底部按钮等)。上一篇:vue数组对象去重
下一篇:vue获取元素宽度
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站