elementUI彈框增加拖拽及最大最小化功能


import Vue from 'vue'
// v-dialogDrag: 彈窗拖拽自定義指令,已在main.js引入,使用即可,每個 el-dialog 加指令
Vue.directive('dialogDrag', {
    bind(el, binding, vnode, oldVnode) {
        /**
         * 關閉彈窗可移動
         * @type {CustomEvent<string>}
         */
        // return;

        /**
         * 打開彈窗可移動
         * @type {CustomEvent<string>}
         */
        let resizeEvent = new CustomEvent('drag-resize',{detail:'尺寸變化',bubbles:false});
        //初始化不最大化
        el.fullscreen = false;
        //彈框可拉伸最小寬高
        let minWidth = 400;
        let minHeight = 300;
        //當前寬高
        let nowWidth = minWidth;
        let nowHight = minHeight;
        //當前頂部高度
        let nowMarginTop = 0;
        //獲取彈框頭部(這部分可雙擊全屏)
        const dialogHeaderEl = el.querySelector('.el-dialog__header');
        let hasSetBodyHight = false;
        //彈窗
        const dragDom = el.querySelector('.el-dialog');
        dragDom.className += ' el-drag-dialog';
        //清除選擇頭部文字效果
        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 maxMin = document.createElement("button");
        maxMin.className += ' el-dialog__headerbtn el-dialog__minmax';
        maxMin.style.right = '40px';
        maxMin.style.color = '#909399';
        maxMin.title = el.fullscreen ? '還原' : '最大化';
        maxMin.innerHTML = '<i class=' + (el.fullscreen ? '"el-icon-crop"' : '"el-icon-full-screen"') + ' onMouseOver="this.style.color=\'#409EFF\'" onMouseOut="this.style.color=\'inherit\'"></i>';
        dialogHeaderEl.insertBefore(maxMin, dialogHeaderEl.childNodes[1]);
        let moveDown = (e) => {
            // 鼠標按下,計算當前元素距離可視區的距離
            const disX = e.clientX - dialogHeaderEl.offsetLeft;
            const disY = e.clientY - dialogHeaderEl.offsetTop;

            // 獲取到的值帶px 正則匹配替換
            let styL, styT;

            const screenWidth = document.documentElement.clientWidth || document.body.clientWidth // 頁面可視區寬度,兼容寫法
            const screenHeight = document.documentElement.clientHeight || document.body.clientHeight // 頁面可視區高度,兼容寫法

            const dragDomWidth = dragDom.offsetWidth // 對話框寬度
            const dragDomheight = dragDom.offsetHeight // 對話框高度
            // 下面四行的是 x軸和y軸最小和最大的移動范圍  可基於上面兩行設置
            const minDragDomLeft = (dragDom.offsetLeft+dragDomWidth/2) // 對話框邊界最小left值
            const maxDragDomLeft = (screenWidth - dragDom.offsetLeft - dragDomWidth+dragDomWidth/2) // 對話框邊界最大left值

            const minDragDomTop = dragDom.offsetTop // 對話框邊界最小Top值
            const maxDragDomTop = (screenHeight - dragDom.offsetTop - dragDomheight+dragDomheight/2) // 對話框邊界最大Top值

            // 注意在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) {

                // 通過事件委托,計算移動的距離
                let l = e.clientX - disX;
                let t = e.clientY - disY;

                // 邊界處理
                if (-(l) > minDragDomLeft) {
                    l = -(minDragDomLeft)
                } else if (l > maxDragDomLeft) {
                    l = maxDragDomLeft
                }

                if (-(t) > minDragDomTop) {
                    t = -(minDragDomTop)
                } else if (t > maxDragDomTop) {
                    t = maxDragDomTop
                }
                // 移動當前元素
                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;
        let bodyHeight = 'auto';
        // 最大/最小化
        function setMaxMin() {
            if (el.fullscreen) {
                let i = maxMin.querySelector('.el-icon-crop');
                i.classList.remove('el-icon-crop');
                i.classList.add('el-icon-full-screen');
                maxMin.innerHTML = '<i class="el-icon-full-screen"></i>';
                maxMin.title = '最大化';
                dragDom.style.height = "auto";
                dragDom.style.width = nowWidth + 'px';
                dragDom.style.marginTop = nowMarginTop;
                el.fullscreen = false;
                dialogHeaderEl.style.cursor = 'move';
                dialogHeaderEl.onmousedown = moveDown;
                dragDom.querySelector('.el-dialog__body').style.height = bodyHeight;
                hasSetBodyHight = false;
            } else {
                let i = maxMin.querySelector('.el-icon-full-screen');
                i.classList.remove('el-icon-full-screen');
                i.classList.add('el-icon-crop');
                maxMin.title = '還原';
                bodyHeight = dragDom.querySelector('.el-dialog__body').offsetHeight + 'px';
                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;
                el.fullscreen = true;
                dialogHeaderEl.style.cursor = 'initial';
                dialogHeaderEl.onmousedown = null;
                if (!hasSetBodyHight) {
                    let footerHeight = dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight;
                    dragDom.querySelector('.el-dialog__body').style.height = 'calc(100% - ' + (dialogHeaderEl.offsetHeight + footerHeight) + 'px)';
                    hasSetBodyHight = true;
                }
            }
            el.dispatchEvent(resizeEvent);
        }

        //點擊放大縮小效果
        maxMin.onclick = setMaxMin;
        //雙擊頭部效果
        dialogHeaderEl.ondblclick = setMaxMin;
        //獲取彈框底部(這部分可雙擊全屏)
        const dialogFooter = el.querySelector('.el-dialog__footer');
        if (dialogFooter) {
            dialogFooter.ondblclick = setMaxMin;
        }
        //拉伸
        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.onmousedown = (e) => {
            // 記錄初始x位置
            const clientX = e.clientX;
            // 鼠標按下,計算當前元素距離可視區的距離
            const disX = e.clientX - resizeEl.offsetLeft;
            const disY = e.clientY - resizeEl.offsetTop;
            document.onmousemove = function (e) {
                // 通過事件委托,計算移動的距離 (開始拖拽至結束拖拽的距離)
                e.preventDefault(); // 移動時禁用默認事件
                // 通過事件委托,計算移動的距離
                const x = e.clientX - disX + (e.clientX - clientX);//這里 由於elementUI的dialog控制居中的,所以水平拉伸效果是雙倍
                const y = e.clientY - disY;
                //比較是否小於最小寬高
                dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';
                dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';
                if (!hasSetBodyHight) {
                    let footerHeight = dragDom.querySelector('.el-dialog__footer') && dragDom.querySelector('.el-dialog__footer').offsetHeight;
                    dragDom.querySelector('.el-dialog__body').style.height = 'calc(100% - ' + (dialogHeaderEl.offsetHeight + footerHeight) + 'px)';
                    hasSetBodyHight = true;
                }
            };
            //拉伸結束
            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
                el.dispatchEvent(resizeEvent);
            };
        }
    },
})

在main.js中引入 import '@/utils/dialog'; 

其中加了頭部和底部雙擊最大化最小化功能,因為拖動到最頂部時拖動不了,此時雙擊底部最大化,再縮小即可;


免責聲明!

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



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