vue2 - element彈框自定義指令 實現拖動、縮放

seekHelp發表於2024-08-12

directive/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;
            };
        }
    }
})

main.js

import dialogDrag from './directive/dialogDrag'

Vue.use(dialogDrag)

相關文章