vue使用自定義指令v-dialogDrag來控制element ui中el-dialog的拖動縮放,拉伸問題


1 在vue的utils中新建一個dialogDrag.js

import Vue from 'vue'
Vue.directive('dialogDrag', {
  bind(el, binding, vnode, oldVnode) {
    //彈框可拉伸最小寬高
    let minWidth = 400;
    let minHeight = 300;
    //初始非全屏
    let isFullScreen = false;
    //當前寬高
    let nowWidth = 0;
    let nowHight = 0;
    //當前頂部高度
    let nowMarginTop = 0;
    //獲取彈框頭部(這部分可雙擊全屏)
    const dialogHeaderEl = el.querySelector('.el-dialog__header');
    //彈窗
    const dragDom = el.querySelector('.el-dialog');
    //給彈窗加上overflow auto;不然縮小時框內的標簽可能超出dialog;
    dragDom.style.overflow = "auto";
    //清除選擇頭部文字效果
    dialogHeaderEl.onselectstart = new Function("return false");  
    //頭部加上可拖動cursor
    dialogHeaderEl.style.cursor = 'move';

    // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);

    let moveDown = (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;

        // 移動當前元素  
        dragDom.style.left = `${l + styL}px`;
        dragDom.style.top = `${t + styT}px`;

        //將此時的位置傳出去
        //binding.value({x:e.pageX,y:e.pageY})
      };

      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
    dialogHeaderEl.onmousedown = moveDown;
    //雙擊頭部效果
    dialogHeaderEl.ondblclick = (e) => {
      if (isFullScreen == false) {
        nowHight = dragDom.clientHeight;
        nowWidth = dragDom.clientWidth;
        nowMarginTop = dragDom.style.marginTop;
        dragDom.style.left = 0;
        dragDom.style.top = 0;
        dragDom.style.height = "100VH";
        dragDom.style.width = "100VW";
        dragDom.style.marginTop = 0;
        isFullScreen = true;
        dialogHeaderEl.style.cursor = 'initial';
        dialogHeaderEl.onmousedown = null;
      } else {
        dragDom.style.height = "auto";
        dragDom.style.width = nowWidth + 'px';
        dragDom.style.marginTop = nowMarginTop;
        isFullScreen = false;
        dialogHeaderEl.style.cursor = 'move';
        dialogHeaderEl.onmousedown = moveDown;
      }
    }
    
    //拉伸(右下方)
    let resizeEl=document.createElement("div");
    dragDom.appendChild(resizeEl); 
    //在彈窗右下角加上一個10-10px的控制塊
    resizeEl.style.cursor = 'se-resize';
    resizeEl.style.position = 'absolute';
    resizeEl.style.height = '10px';
    resizeEl.style.width = '10px';
    resizeEl.style.right = '0px';
    resizeEl.style.bottom = '0px';
    resizeEl.style.zIndex = '99';
    //鼠標拉伸彈窗
    resizeEl.onmousedown = (e) => {
      // 記錄初始x位置
      let clientX = e.clientX;
      // 鼠標按下,計算當前元素距離可視區的距離
      let disX = e.clientX - resizeEl.offsetLeft;
      let disY = e.clientY - resizeEl.offsetTop;

      document.onmousemove = function (e) {
        e.preventDefault(); // 移動時禁用默認事件
        
        // 通過事件委托,計算移動的距離
        let x = e.clientX - disX + (e.clientX - clientX);//這里 由於elementUI的dialog控制居中的,所以水平拉伸效果是雙倍
        let y = e.clientY - disY;
        //比較是否小於最小寬高
        dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';
        dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';
        };
      //拉伸結束
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
    
    //拉伸(右邊)
    let resizeElR=document.createElement("div");
    dragDom.appendChild(resizeElR); 
    //在彈窗右下角加上一個10-10px的控制塊
    resizeElR.style.cursor = 'w-resize';
    resizeElR.style.position = 'absolute';
    resizeElR.style.height = '100%';
    resizeElR.style.width = '10px';
    resizeElR.style.right = '0px';
    resizeElR.style.top = '0px';
    //鼠標拉伸彈窗
    resizeElR.onmousedown = (e) => {
      let elW = dragDom.clientWidth;
      let EloffsetLeft = dragDom.offsetLeft;
      // 記錄初始x位置
      let clientX = e.clientX;
      document.onmousemove = function (e) {
        e.preventDefault(); // 移動時禁用默認事件
        //右側鼠標拖拽位置
        if (clientX > EloffsetLeft + elW - 10 && clientX < EloffsetLeft + elW) {
            //往左拖拽
            if (clientX > e.clientX) {
                if (dragDom.clientWidth < minWidth) {
                } else {
                    dragDom.style.width = elW - (clientX - e.clientX) * 2 + 'px';
                }
            }
            //往右拖拽
            if (clientX < e.clientX) {
                dragDom.style.width = elW + (e.clientX - clientX) * 2 + 'px';
            }
        }
       
      };
      //拉伸結束
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
    
    //拉伸(左邊)
    let resizeElL=document.createElement("div");
    dragDom.appendChild(resizeElL); 
    //在彈窗右下角加上一個10-10px的控制塊
    resizeElL.style.cursor = 'w-resize';
    resizeElL.style.position = 'absolute';
    resizeElL.style.height = '100%';
    resizeElL.style.width = '10px';
    resizeElL.style.left = '0px';
    resizeElL.style.top = '0px';
    //鼠標拉伸彈窗
    resizeElL.onmousedown = (e) => {
      let elW = dragDom.clientWidth;
      let EloffsetLeft = dragDom.offsetLeft;
      // 記錄初始x位置
      let clientX = e.clientX;
      document.onmousemove = function (e) {
        e.preventDefault(); // 移動時禁用默認事件
        //左側鼠標拖拽位置
        if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) {
            //往左拖拽
            if (clientX > e.clientX) {
                dragDom.style.width = elW + (clientX - e.clientX) * 2 + 'px';
            }
            //往右拖拽
            if (clientX < e.clientX) {
                if (dragDom.clientWidth < minWidth) {
                } else {
                    dragDom.style.width = elW - (e.clientX - clientX) * 2 + 'px';
        
                }
            }
        }
       
      };
      //拉伸結束
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
    
    // 拉伸(下邊)
    let resizeElB=document.createElement("div");
    dragDom.appendChild(resizeElB); 
    //在彈窗右下角加上一個10-10px的控制塊
    resizeElB.style.cursor = 'n-resize';
    resizeElB.style.position = 'absolute';
    resizeElB.style.height = '10px';
    resizeElB.style.width = '100%';
    resizeElB.style.left = '0px';
    resizeElB.style.bottom = '0px';
    //鼠標拉伸彈窗
    resizeElB.onmousedown = (e) => {
     let EloffsetTop = dragDom.offsetTop;
     let ELscrollTop = el.scrollTop;
      let clientY = e.clientY;
      let elH = dragDom.clientHeight;
      document.onmousemove = function (e) {
        e.preventDefault(); // 移動時禁用默認事件
        //底部鼠標拖拽位置
        if (ELscrollTop + clientY > EloffsetTop + elH - 20 && ELscrollTop + clientY < EloffsetTop + elH) {
            //往上拖拽
            if (clientY > e.clientY) {
                if (dragDom.clientHeight < minHeight) {
                } else {
                    dragDom.style.height = elH - (clientY - e.clientY) * 2 + 'px';
                }
            }
            //往下拖拽
            if (clientY < e.clientY) {
                dragDom.style.height = elH + (e.clientY - clientY) * 2 + 'px';
            }
        }
      };
      //拉伸結束
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }
  }
})
/**
 *  注意:如果是el-dialog彈出的表單el-form,最好不要把提交按鈕當做el-form-item(elementUI文檔是這么用的),放在el-dialog里。
 *  不然按鈕會同el-dialog中標簽一樣被overflow;
*/

2.在main.js中引用

import '@/utils/dialogDrag.js'

3。使用v-dialogDrag指令

<el-dialog v-dialogDrag :title="dialog.title" center :visible.sync="dialog.show" custom-class="lz-el-dialog" width="80%" top="1vh"
                :close-on-click-modal="false">
                <addCustomer ref="addCustomer" :type="dialog.title" @success="getCustomerList"></addCustomer>
                <span slot="footer" class="dialog-footer" >
                    <el-button @click="dialog.show=false" :size="size">取 消</el-button>
                    <el-button type="primary" @click="determine" :size="size" style="margin-left: 30px;">確 定</el-button>
                </span>
            </el-dialog>

4.你可能遇到這樣的問題,我只要在彈出層中滾動后,就無法縮放,或者我想讓確定和取消按鈕始終在底部顯示

那么你需要設置css

.el-dialog {
  position: relative;
  margin-bottom: 0!important;
  height: 94vh;overflow: auto;
}
// 彈出對話框 解決縮放問題
.el-dialog__body{padding: 10px;height: calc(100% - 112px);overflow: auto;}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM