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

vue 弹出框

作者:放电的牛   发布日期:2025-07-23   浏览:50

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

    <!-- 弹出框组件 -->
    <modal v-if="showModal" @close="showModal = false">
      <!-- 弹出框的内容 -->
      <h3>这是一个弹出框</h3>
      <p>你可以在这里添加任何内容。</p>
    </modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showModal: false // 控制弹出框的显示与隐藏
    };
  }
};
</script>

<!-- 弹出框组件定义 -->
<template>
  <transition name="modal">
    <div 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>默认内容</slot>
          </div>

          <div class="modal-footer">
            <button @click="$emit('close')">关闭</button>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

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

.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="showModal" 控制弹出框的显示与隐藏。
    • 使用了自定义的 <modal> 组件来展示弹出框的内容。
  2. 脚本部分 (<script>)

    • 定义了一个 data 属性 showModal,用于控制弹出框的显示状态。
    • 点击按钮时,将 showModal 设置为 true,从而显示弹出框。
    • 弹出框关闭时,通过 $emit('close')showModal 设置为 false,隐藏弹出框。
  3. 样式部分 (<style>)

    • 定义了弹出框的样式,包括背景遮罩、弹出框容器的样式以及动画效果。
    • 使用了 transition 来实现弹出框的淡入淡出效果。

上一篇:vue watch handler

下一篇:vue query传参

大家都在看

vue.config.js configu

node.js vue

vue 图表组件

vue3watch监听多个变量

vue查看版本

vue3 reactive对象重新赋值

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

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

Laravel 中文站