作者 RuoYi

自定义可拖动弹窗宽度指令

  1 +/**
  2 +* v-dialogDragWidth 可拖动弹窗宽度
  3 +* Copyright (c) 2019 ruoyi
  4 +*/
  5 +
  6 +export default {
  7 + bind(el) {
  8 + const dragDom = el.querySelector('.el-dialog');
  9 + const lineEl = document.createElement('div');
  10 + lineEl.style = 'width: 5px; background: inherit; height: 80%; position: absolute; right: 0; top: 0; bottom: 0; margin: auto; z-index: 1; cursor: w-resize;';
  11 + lineEl.addEventListener('mousedown',
  12 + function (e) {
  13 + // 鼠标按下,计算当前元素距离可视区的距离
  14 + const disX = e.clientX - el.offsetLeft;
  15 + // 当前宽度
  16 + const curWidth = dragDom.offsetWidth;
  17 + document.onmousemove = function (e) {
  18 + e.preventDefault(); // 移动时禁用默认事件
  19 + // 通过事件委托,计算移动的距离
  20 + const l = e.clientX - disX;
  21 + dragDom.style.width = `${curWidth + l}px`;
  22 + };
  23 + document.onmouseup = function (e) {
  24 + document.onmousemove = null;
  25 + document.onmouseup = null;
  26 + };
  27 + },
  28 + );
  29 + dragDom.appendChild(lineEl);
  30 + }
  31 +}
1 import hasRole from './permission/hasRole' 1 import hasRole from './permission/hasRole'
2 import hasPermi from './permission/hasPermi' 2 import hasPermi from './permission/hasPermi'
3 import dialogDrag from './dialog/drag' 3 import dialogDrag from './dialog/drag'
  4 +import dialogDragWidth from './dialog/dragWidth'
4 5
5 const install = function(Vue) { 6 const install = function(Vue) {
6 Vue.directive('hasRole', hasRole) 7 Vue.directive('hasRole', hasRole)
7 Vue.directive('hasPermi', hasPermi) 8 Vue.directive('hasPermi', hasPermi)
8 Vue.directive('dialogDrag', dialogDrag) 9 Vue.directive('dialogDrag', dialogDrag)
  10 + Vue.directive('dialogDragWidth', dialogDragWidth)
9 } 11 }
10 12
11 if (window.Vue) { 13 if (window.Vue) {
12 window['hasRole'] = hasRole 14 window['hasRole'] = hasRole
13 window['hasPermi'] = hasPermi 15 window['hasPermi'] = hasPermi
14 - window['dialogDrag'] = dialogDrag  
15 Vue.use(install); // eslint-disable-line 16 Vue.use(install); // eslint-disable-line
16 } 17 }
17 18