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

前言

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

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

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

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

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

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

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

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

javascript 复制代码
/**
 * v-dialogDrag 弹窗拖拽
 */
export default {
  bind(el, binding, vnode, oldVnode) {
    const value = binding.value
    if (value === false) return
    // 获取拖拽内容头部
    const dialogHeaderEl = el.querySelector('.el-dialog__header');
    const dragDom = el.querySelector('.el-dialog');
    dialogHeaderEl.style.cursor = 'move';
    // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
    dragDom.style.position = 'absolute';
    dragDom.style.marginTop = 0;
    let width = dragDom.style.width;
    if (width.includes('%')) {
      width = +document.body.clientWidth * (+width.replace(/%/g, '') / 100);
    } else {
      width = +width.replace(/\px/g, '');
    }
    dragDom.style.left = `${(document.body.clientWidth - width) / 2}px`;
    // 鼠标按下事件
    dialogHeaderEl.onmousedown = (e) => {
      // 鼠标按下,计算当前元素距离可视区的距离 (鼠标点击位置距离可视窗口的距离)
      const disX = e.clientX - dialogHeaderEl.offsetLeft;
      const disY = e.clientY - dialogHeaderEl.offsetTop;
​
      // 获取到的值带px 正则匹配替换
      let styL, styT;
​
      // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
      if (sty.left.includes('%')) {
        styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);
        styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);
      } else {
        styL = +sty.left.replace(/\px/g, '');
        styT = +sty.top.replace(/\px/g, '');
      }
​
      // 鼠标拖拽事件
      document.onmousemove = function (e) {
        // 通过事件委托,计算移动的距离 (开始拖拽至结束拖拽的距离)
        const l = e.clientX - disX;
        const t = e.clientY - disY;
​
        let finallyL = l + styL
        let finallyT = t + styT
​
        // 移动当前元素
        dragDom.style.left = `${finallyL}px`;
        dragDom.style.top = `${finallyT}px`;
​
      };
​
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
  }
}
复制代码

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

javascript 复制代码
/**
 * 可拖动弹窗宽度(右侧边)
 */
​
export default {
  bind(el) {
    const dragDom = el.querySelector('.el-dialog');
    const lineEl = document.createElement('div');
    lineEl.style = 'width: 5px; background: inherit; height: 80%; position: absolute; right: 0; top: 0; bottom: 0; margin: auto; z-index: 1; cursor: w-resize;';
    lineEl.addEventListener('mousedown',
      function (e) {
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX - el.offsetLeft;
        // 当前宽度
        const curWidth = dragDom.offsetWidth;
        document.onmousemove = function (e) {
          e.preventDefault(); // 移动时禁用默认事件
          // 通过事件委托,计算移动的距离
          const l = e.clientX - disX;
          dragDom.style.width = `${curWidth + l}px`;
        };
        document.onmouseup = function (e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      }, false);
    dragDom.appendChild(lineEl);
  }
}
复制代码

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

javascript 复制代码
/**
 *  可拖动弹窗高度(右下角)- 也可以改变高度和宽度
 */
​
export default {
  bind(el) {
    const dragDom = el.querySelector('.el-dialog');
    const lineEl = document.createElement('div');
    lineEl.style = 'width: 6px; background: inherit; height: 10px; position: absolute; right: 0; bottom: 0; margin: auto; z-index: 1; cursor: nwse-resize;';
    lineEl.addEventListener('mousedown',
      function(e) {
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX - el.offsetLeft;
        const disY = e.clientY - el.offsetTop;
        // 当前宽度 高度
        const curWidth = dragDom.offsetWidth;
        const curHeight = dragDom.offsetHeight;
        document.onmousemove = function(e) {
          e.preventDefault(); // 移动时禁用默认事件
          // 通过事件委托,计算移动的距离
          const xl = e.clientX - disX;
          const yl = e.clientY - disY
          dragDom.style.width = `${curWidth + xl}px`;
          dragDom.style.height = `${curHeight + yl}px`;
        };
        document.onmouseup = function(e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      }, false);
    dragDom.appendChild(lineEl);
  }
}
复制代码

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

javascript 复制代码
// dialog弹出框-可拖动
import dialogDrag from './dialog/drag'
// dialog弹出框-宽度可拖动
import dialogDragWidth from './dialog/dragWidth'
// dialog弹出框-高度可拖动(也可拖动宽度)
import dialogDragHeight from './dialog/dragHeight'
​
const install = function (Vue) {
  // dialog弹出框-可拖动-使用v-dialogDrag
  Vue.directive('dialogDrag', dialogDrag)
  // dialog弹出框-宽度可拖动-使用v-dialogDragWidth
  Vue.directive('dialogDragWidth', dialogDragWidth)
  // dialog弹出框-高度可拖动(也可拖动宽度)- 使用v-dialogDragHeight
  Vue.directive('dialogDragHeight', dialogDragHeight)
}
​
​
export default install
复制代码

7.来到main.js引入注册

javascript 复制代码
// 自定义指令
import directive from './directive'
​
// 挂载
Vue.use(directive)

8.来到页面使用


总结:

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

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

相关推荐
下雪天的夏风15 分钟前
React@16.x(55)Redux@4.x(4)- store
前端·javascript·react.js
玖玥西1 小时前
html5——CSS3_文本样式属性
前端·css·html·css3·html5·web
卓卓没头发1 小时前
Vue Router 4:构建高效单页面应用的路由管理
前端·javascript·vue.js
SuGarSJL4 小时前
npm ERR! code ENOTEMPTY npm ERR! syscall rename npm ERR!
前端·npm·node.js
tonylua5 小时前
Python: 从 2.7 升级到 3,我比 vue 慢了一点点
开发语言·前端·javascript·vue.js·python
测开小林5 小时前
Django+vue自动化测试平台(25)-- 自动化测试之封装APscheduler定时任务框架
vue.js·python·django·自动化
测开小林5 小时前
Django+vue自动化测试平台(26)-- 接口自动化测试之连接数据库执行sql
数据库·vue.js·django·自动化
daban20085 小时前
【技术支持】npm镜像设置
前端·npm·node.js
周bro5 小时前
vue2实现复制,粘贴功能,使用vue-clipboard2插件
前端·javascript·vue.js
牛奶味的团子6 小时前
添加点击跳转页面,优化登录和注册页路由
前端·javascript·vue.js