• vue el-dialog弹出框自定义指令实现拖拽改变位置-宽度-高度


    前言

    • 在实际开发中我们经常使用el-dialog弹出框做表单,一般情况都是居中。遮挡到了一部分数据

    • 当我们想要查看弹出框下面的数据时,就只能先把弹出框关闭,查看完数据之后在打开弹框

    • 我们通过动态样式,和鼠标事件就可以实现。但自己写的在适配性和全面性上还是有所欠缺的

    • 这种我们可以直接复制使用,写成全局自定义指令。在很多的地方使用,并且只做加法

    代码实现-没有自定义指令情况下

    1.来到src/创建directive文件夹

    2.在src/directive/创建dialog文件夹专门用来放关于dialog的代码

    3.在src/directive/dialog创建drag.js文件-弹出框的拖拽-代码如下

    1. /**
    2. * v-dialogDrag 弹窗拖拽
    3. */
    4. export default {
    5. bind(el, binding, vnode, oldVnode) {
    6.   const value = binding.value
    7.   if (value === false) return
    8.   // 获取拖拽内容头部
    9.   const dialogHeaderEl = el.querySelector('.el-dialog__header');
    10.   const dragDom = el.querySelector('.el-dialog');
    11.   dialogHeaderEl.style.cursor = 'move';
    12.   // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    13.   const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
    14.   dragDom.style.position = 'absolute';
    15.   dragDom.style.marginTop = 0;
    16.   let width = dragDom.style.width;
    17.   if (width.includes('%')) {
    18.     width = +document.body.clientWidth * (+width.replace(/%/g, '') / 100);
    19.   } else {
    20.     width = +width.replace(/\px/g, '');
    21.   }
    22.   dragDom.style.left = `${(document.body.clientWidth - width) / 2}px`;
    23.   // 鼠标按下事件
    24.   dialogHeaderEl.onmousedown = (e) => {
    25.     // 鼠标按下,计算当前元素距离可视区的距离 (鼠标点击位置距离可视窗口的距离)
    26.     const disX = e.clientX - dialogHeaderEl.offsetLeft;
    27.     const disY = e.clientY - dialogHeaderEl.offsetTop;
    28.     // 获取到的值带px 正则匹配替换
    29.     let styL, styT;
    30.     // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    31.     if (sty.left.includes('%')) {
    32.       styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);
    33.       styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);
    34.     } else {
    35.       styL = +sty.left.replace(/\px/g, '');
    36.       styT = +sty.top.replace(/\px/g, '');
    37.     }
    38.     // 鼠标拖拽事件
    39.     document.onmousemove = function (e) {
    40.       // 通过事件委托,计算移动的距离 (开始拖拽至结束拖拽的距离)
    41.       const l = e.clientX - disX;
    42.       const t = e.clientY - disY;
    43.       let finallyL = l + styL
    44.       let finallyT = t + styT
    45.       // 移动当前元素
    46.       dragDom.style.left = `${finallyL}px`;
    47.       dragDom.style.top = `${finallyT}px`;
    48.     };
    49.     document.onmouseup = function (e) {
    50.       document.onmousemove = null;
    51.       document.onmouseup = null;
    52.     };
    53.   }
    54. }
    55. }

    4.在src/directive/dialog创建dragWidth.js文件-弹出框的宽度改变-代码如下

    1. /**
    2. * 可拖动弹窗宽度(右侧边)
    3. */
    4. export default {
    5. bind(el) {
    6.   const dragDom = el.querySelector('.el-dialog');
    7.   const lineEl = document.createElement('div');
    8.   lineEl.style = 'width: 5px; background: inherit; height: 80%; position: absolute; right: 0; top: 0; bottom: 0; margin: auto; z-index: 1; cursor: w-resize;';
    9.   lineEl.addEventListener('mousedown',
    10.     function (e) {
    11.       // 鼠标按下,计算当前元素距离可视区的距离
    12.       const disX = e.clientX - el.offsetLeft;
    13.       // 当前宽度
    14.       const curWidth = dragDom.offsetWidth;
    15.       document.onmousemove = function (e) {
    16.         e.preventDefault(); // 移动时禁用默认事件
    17.         // 通过事件委托,计算移动的距离
    18.         const l = e.clientX - disX;
    19.         dragDom.style.width = `${curWidth + l}px`;
    20.       };
    21.       document.onmouseup = function (e) {
    22.         document.onmousemove = null;
    23.         document.onmouseup = null;
    24.       };
    25.     }, false);
    26.   dragDom.appendChild(lineEl);
    27. }
    28. }

    5.在src/directive/dialog创建dragHeight.js文件-弹出框的宽度和高度改变-代码如下

    1. /**
    2. * 可拖动弹窗高度(右下角)- 也可以改变高度和宽度
    3. */
    4. export default {
    5. bind(el) {
    6.   const dragDom = el.querySelector('.el-dialog');
    7.   const lineEl = document.createElement('div');
    8.   lineEl.style = 'width: 6px; background: inherit; height: 10px; position: absolute; right: 0; bottom: 0; margin: auto; z-index: 1; cursor: nwse-resize;';
    9.   lineEl.addEventListener('mousedown',
    10.     function(e) {
    11.       // 鼠标按下,计算当前元素距离可视区的距离
    12.       const disX = e.clientX - el.offsetLeft;
    13.       const disY = e.clientY - el.offsetTop;
    14.       // 当前宽度 高度
    15.       const curWidth = dragDom.offsetWidth;
    16.       const curHeight = dragDom.offsetHeight;
    17.       document.onmousemove = function(e) {
    18.         e.preventDefault(); // 移动时禁用默认事件
    19.         // 通过事件委托,计算移动的距离
    20.         const xl = e.clientX - disX;
    21.         const yl = e.clientY - disY
    22.         dragDom.style.width = `${curWidth + xl}px`;
    23.         dragDom.style.height = `${curHeight + yl}px`;
    24.       };
    25.       document.onmouseup = function(e) {
    26.         document.onmousemove = null;
    27.         document.onmouseup = null;
    28.       };
    29.     }, false);
    30.   dragDom.appendChild(lineEl);
    31. }
    32. }

    6.在src/directive/创建index.js文件-对自定义指令统一注册-代码如下

    1. // dialog弹出框-可拖动
    2. import dialogDrag from './dialog/drag'
    3. // dialog弹出框-宽度可拖动
    4. import dialogDragWidth from './dialog/dragWidth'
    5. // dialog弹出框-高度可拖动(也可拖动宽度)
    6. import dialogDragHeight from './dialog/dragHeight'
    7. const install = function (Vue) {
    8. // dialog弹出框-可拖动-使用v-dialogDrag
    9. Vue.directive('dialogDrag', dialogDrag)
    10. // dialog弹出框-宽度可拖动-使用v-dialogDragWidth
    11. Vue.directive('dialogDragWidth', dialogDragWidth)
    12. // dialog弹出框-高度可拖动(也可拖动宽度)- 使用v-dialogDragHeight
    13. Vue.directive('dialogDragHeight', dialogDragHeight)
    14. }
    15. export default install

    7.来到main.js引入注册

    1. // 自定义指令
    2. import directive from './directive'
    3. // 挂载
    4. Vue.use(directive)

    8.来到页面使用


    总结:

    经过这一趟流程下来相信你也对 vue el-dialog弹出框自定义指令实现拖拽改变位置-宽度-高度 有了初步的深刻印象,但在实际开发中我 们遇到的情况肯定是不一样的,所以我们要理解它的原理,万变不离其宗。加油,打工人!

    有什么不足的地方请大家指出谢谢 -- 風过无痕

  • 相关阅读:
    vsCode快捷键
    3_springboot_shiro_jwt_多端认证鉴权_Redis缓存管理器.md
    1053 Path of Equal Weight
    Java main方法
    python读取execel表格(xls等格式)转换为csv,并且加载到hive表中
    第一个 flet 应用
    关于在 Notion 中使用 Markdown 语法
    新型支持苹果 Find My 防丢神器-Chipolo CARD Spot
    利用Linked SQL Server提权
    Linux驱动开发——块设备驱动
  • 原文地址:https://blog.csdn.net/weixin_53579656/article/details/133953507