这篇文章主要为大家详细介绍了Vue dialog模态框的封装方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
前言
这个是基于vue2的模态框封装,仿照elementUI而写的组件。
效果如图
首先我们需要一个遮罩层
<template> <div class="myDialog"> <div v-if="visable" class="dialog_mask" @click="close"></div> </div> </template> <style> .dialog_mask { width: 100%; height: 100vh; background-color: rgba(0, 0, 0, 0.418); position: fixed; top: 0; left: 0; z-index: 122; } </style>
然后是主体部分
<!-- 模态框内容部分 --> <div v-if="visable" class="dialog_window" @mousedown="moveDialog"> <header> <!-- 传入的标题 --> <h5>{{ title }}</h5> <!-- x号关闭 --> <span @click="close">x</span> </header> <!-- 插槽插入中间的内容 --> <div class="ctx"> <slot></slot> </div> <!-- 插槽插入底部按钮 --> <div class="footer"> <slot name="footer"></slot> </div> </div>
props传入的值
props: { visable: { // 数据显示隐藏 type: Boolean, default: false, }, title: { // 标题 type: String, }, move: { // 是否可拖动 type: Boolean, default: false, } },
对应的事件
methods: { close() { // 关闭功能 this.$emit("update:visable", false); // .sync修饰符 父子组件同步更新 this.callBack(this.visable); }, moveDialog(e) { // 拖动 if (!this.move) return false; let odiv = e.target; let disX = e.clientX - odiv.offsetLeft; let disY = e.clientY - odiv.offsetTop; document.onmousemove = (e) => { let left = e.clientX - disX; let top = e.clientY - disY; odiv.style.left = left + "px"; odiv.style.top = top + "px"; }; document.onmouseup = (e) => { document.onmousemove = null; document.onmousedown = null; }; }, },
以上就是dialog的封装。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。
您可能感兴趣的文章:
- Vue.js弹出模态框组件开发的示例代码
- vue+element 模态框表格形式的可编辑表单实现
- 利用vue实现模态框组件
- vue+element模态框中新增模态框和删除功能
- vue实现模态框的通用写法推荐
- vue.extend实现alert模态框弹窗组件
- 详解如何用VUE写一个多用模态框组件模版
- vue移动端模态框(可传参)的实现
- 详解vue父子组件关于模态框状态的绑定方案
- Vue.extend 登录注册模态框的实现