Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue 弹框

作者:泪﹌无痕   发布日期:2026-02-09   浏览:106

<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>

解释说明:

  1. 模板部分<template> 标签内定义了 HTML 结构。这里有一个按钮用于触发弹框,以及一个 modal 组件用于显示弹框。
  2. 脚本部分<script> 标签内定义了 Vue 组件的逻辑。data 函数返回一个对象,其中包含控制弹框显示与隐藏的 showModal 变量。当点击按钮时,showModal 的值会变为 true,从而显示弹框;点击关闭按钮时,showModal 的值会变为 false,从而隐藏弹框。
  3. 样式部分<style scoped> 标签内定义了弹框的样式,包括背景遮罩、弹框容器的样式等。
  4. 弹框组件:在代码中还定义了一个 Modal 组件,用于封装弹框的结构和样式。这个组件可以通过插槽 (slot) 来传递不同的内容(如标题、正文、底部按钮等)。

上一篇:vue数组对象去重

下一篇:vue获取元素宽度

大家都在看

vue.js devtools用法

three.js vue

vue js for循环

vue.min.js 本地引入

highlight.js vue

vue.config.js 配置

vue.config.js 配置代理

vue.config.js configu

node.js vue

vue3 写法

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站