Vue之弹出框

博客介绍了在vue项目中实现动态插入式弹窗的方法。指出不同类型弹窗有不同控制方式,提示框适合使用时插入、结束时删除元素。介绍了通过方法将组件动态插入DOM并清理实例、删除元素的思路,还提及编写提示弹窗组件及相关调用代码。

弹出框有很多种,有的适合使用v-if加上一个控制变量来控制弹窗的显示或否。但是有的框,如提示框,适合使用时插入,结束时删除元素。如下图所示:
在这里插入图片描述
且看vue如何实现这种方式的弹窗,这里使用的是vue-cli建立的项目。

思路

我所知,组件实例化有三种方式,见vue学习笔记3.1小节。通过$mount方法可以将组件以编程时的方式动态插入DOM中,$destroy方法可以清理组件实例,然后再删除DOM元素。

代码

  1. 首先编写一个提示弹窗组件(TipLayer.vue):
    <template>
        <div class="msg">
            {{msg}}
        </div>
    </template>
    
    
    <script>
    export default {
        name:"TipLayer",
        data(){
            return {
                msg:null
            }
        }
    }
    </script>
    
    <style scoped>
        .msg{
            position:fixed;
            left:50%;
            top:50%;
            transform:translate(-50%,-50%);
            background-color: rgba(0,0,0,0.5);
            color: white;
            padding:0.5rem 1rem;
            border-radius: 10px;
            animation: TipLayer 2s;
        }
        @keyframes TipLayer {
            from{
                transform:translate(-50%,-50%) scale(0);
                opacity: 0;
            }
            25%{
                transform:translate(-50%,-50%);
                opacity: 1;
            }
            75%{
                transform:translate(-50%,-50%);
                opacity: 1;
            }
            to{
                transform:translate(-50%,-50%) scale(0);
                opacity: 0;
            }
        }
    </style>
    

    提示弹窗组件就是这么简单

  2. 编写layer.js,它负责实例化组件,插入DOM,删除组件和DOM:
    import TipLayer from "./TipLayer"
    import Vue from "vue"
    import $ from "jquery"
    import uuid from "uuid/v4"
    
    let TipLayerClass=Vue.extend(TipLayer);//创建vue子类
    
    export default {
        /**
         * 提示弹窗
         * @param msg 提示消息
         * @param callback 弹窗结束后的回调
         */
        tip(msg,callback){
            //实例对象,并初始化
            let tipLayer=new TipLayerClass();
            tipLayer.msg=msg;
            //创建挂载点
            let id="id-"+uuid();
            $("body").append(`<div id="${id}"></div>`);
            //挂载
            tipLayer.$mount("#"+id);
            //2s后删除
            setTimeout(function () {
                //删除
                tipLayer.$destroy();
                $(tipLayer.$el).remove();
                //回调
                callback?callback():null;
            },2000)
        }
    }
    
  3. 调用,很简单,不给出全部代码了:
    layer.msg("我是快乐的弹窗");
    
Vue 3 中实现弹出框(Dialog)或对话组件功能,可以通过组合式 API(`<script setup>`)结合状态管理和事件处理来完成。以下是实现弹出框功能的详细方式,包括核心组件结构、数据绑定和事件交互。 ### 弹出框组件的核心结构 弹出框通常包括以下部分: - 控制显示与隐藏的状态变量 - 弹出框标题 - 内容区域(支持文本、表单等) - 确定与取消按钮 - 对话的遮罩及点击关闭逻辑 ### 基本实现示例 以下是一个基于 Vue 3 的可复用弹出框组件示例: ```vue <template> <div> <!-- 触发弹出框的按钮 --> <button @click="openDialog">打开弹出框</button> <!-- 弹出框 --> <div v-if="visible" class="dialog-overlay" @click.self="closeDialog"> <div class="dialog-box"> <div class="dialog-header"> <span>弹出框标题</span> <button class="close-btn" @click="closeDialog">✖</button> </div> <div class="dialog-body"> <p>这是一个简单的弹出框内容区域。</p> </div> <div class="dialog-footer"> <button @click="onConfirm">确定</button> <button @click="closeDialog">取消</button> </div> </div> </div> </div> </template> <script setup> import { ref } from 'vue'; const visible = ref(false); const openDialog = () => { visible.value = true; }; const closeDialog = () => { visible.value = false; }; const onConfirm = () => { alert('确认操作'); closeDialog(); }; </script> <style scoped> .dialog-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .dialog-box { background-color: white; padding: 20px; border-radius: 8px; width: 400px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); position: relative; } .dialog-header { display: flex; justify-content: space-between; align-items: center; } .close-btn { background: none; border: none; font-size: 18px; cursor: pointer; } .dialog-footer { margin-top: 20px; display: flex; justify-content: flex-end; gap: 10px; } </style> ``` ### 可扩展功能 - **表单输入**:可在 `.dialog-body` 中加入 `<input>`、`<textarea>` 或使用 UI 架(如 Element Plus、Vant)的表单组件[^2]。 - **遮罩点击关闭**:通过 `@click.self` 实现点击遮罩关闭窗。 - **动画过渡**:使用 Vue 的 `<Transition>` 组件添加淡入淡出效果。 - **可配置化**:将标题、内容、按钮文本等设为 props,提升组件复用性。 - **异步操作支持**:在点击“确定”时执行异步请求,并通过 `Promise` 或 `async/await` 控制关闭逻辑。 ### 与 UI 架集成(如 Element Plus) 如果使用 Element Plus,可以使用其 `<el-dialog>` 组件实现更复杂的弹出框: ```vue <template> <el-button @click="dialogVisible = true">打开弹出框</el-button> <el-dialog v-model="dialogVisible" title="弹出框标题" width="30%"> <span>这是一个使用 Element Plus 的弹出框。</span> <template #footer> <el-button @click="dialogVisible = false">取消</el-button> <el-button type="primary" @click="handleConfirm">确定</el-button> </template> </el-dialog> </template> <script setup> import { ref } from 'vue'; const dialogVisible = ref(false); const handleConfirm = () => { console.log('用户点击了确定'); dialogVisible.value = false; }; </script> ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值