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

vue弹窗组件

作者:寒江映月   发布日期:2025-10-09   浏览:59

<template>
  <div>
    <!-- 触发弹窗的按钮 -->
    <button @click="showModal = true">打开弹窗</button>

    <!-- 弹窗组件 -->
    <transition name="modal">
      <div v-if="showModal" class="modal-mask">
        <div class="modal-wrapper">
          <div class="modal-container">
            <!-- 弹窗标题 -->
            <div class="modal-header">
              <slot name="header">
                默认标题
              </slot>
            </div>

            <!-- 弹窗内容 -->
            <div class="modal-body">
              <slot name="body">
                默认内容
              </slot>
            </div>

            <!-- 弹窗底部操作 -->
            <div class="modal-footer">
              <slot name="footer">
                <button @click="showModal = false">关闭</button>
              </slot>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showModal: false, // 控制弹窗显示/隐藏的状态
    };
  },
};
</script>

<style>
/* 弹窗样式 */
.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.
 *
 * You can easily play with the modal transition by editing
 * these styles.
 */

.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>):

    • 使用一个按钮触发弹窗的显示和隐藏,通过 v-if 指令控制弹窗的显示状态。
    • 弹窗分为三个部分:标题、内容和底部操作。这些部分可以通过插槽 (<slot>) 进行自定义。
  2. 脚本部分 (<script>):

    • 定义了一个 data 属性 showModal 来控制弹窗的显示和隐藏,默认为 false(即不显示)。
    • 当点击按钮时,将 showModal 设置为 true,弹窗就会显示出来;当点击关闭按钮时,将其设置为 false,弹窗消失。
  3. 样式部分 (<style>):

    • 定义了弹窗的基本样式,包括背景遮罩、弹窗容器的样式等。
    • 使用了 CSS 过渡效果,使得弹窗在显示和隐藏时有平滑的动画效果。

上一篇:vue pathrewrite

下一篇:vue get请求传递参数

大家都在看

vue js for循环

vue.config.js 配置

vue.config.js configu

node.js vue

vue3组件传值的方式

vue 图表组件

vue3+vite+ts

vue3watch监听多个变量

vue查看版本

vue3 reactive对象重新赋值

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

Laravel 中文站