The element UI dialog component adds a draggable position and draggable width and height

Keywords: Vue IE Attribute Firefox

There are several points to pay attention to

  • Each pop-up window must have a unique dom operable instruction
  • To add a draggable block header when dragging
  • Because the element UI dialog component uses a percentage of its width at design time, there are compatibility problems with different browsers
  • Getting edge when dragging width and height
<template>
    <el-dialog
        v-dialogDrag
        ref="dialog__wrapper">
        <div class="dialog-body">
            
            <div 
                class="line"
                v-dialogDragWidth="$refs.dialog__wrapper"></div>
        </div>
    </el-dialog>
</template>

Other properties of dialog component will not be written here. All instructions in the project define centralized management and global registration in directives.js
directives.js:

import Vue from 'vue';

// v-dialogDrag: pop up drag
Vue.directive('dialogDrag', {
    bind(el, binding, vnode, oldVnode) {
        const dialogHeaderEl = el.querySelector('.el-dialog__header');
        const dragDom = el.querySelector('.el-dialog');
        dialogHeaderEl.style.cursor = 'move';

        // Get the original attribute ie dom element.currentStyle Firefox Google window.getComputedStyle(dom element, null);
        const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
        
        dialogHeaderEl.onmousedown = (e) => {
            // Press the mouse to calculate the distance between the current element and the visible area
            const disX = e.clientX - dialogHeaderEl.offsetLeft;
            const disY = e.clientY - dialogHeaderEl.offsetTop;
            
            // Get the value with px regular matching replacement
            let styL, styT;

            // Note that the value obtained for the first time in ie is the value of px after 50% movement of the component
            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) {
                // Calculate the moving distance through event delegation 
                const l = e.clientX - disX;
                const t = e.clientY - disY;

                // Move current element  
                dragDom.style.left = `${l + styL}px`;
                dragDom.style.top = `${t + styT}px`;

                //Pass out the position at this time
                //binding.value({x:e.pageX,y:e.pageY})
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }  
    }
})

// v-dialogDragWidth
Vue.directive('dialogDragWidth', {
    bind(el, binding, vnode, oldVnode) {
        const dragDom = binding.value.$el.querySelector('.el-dialog');

        el.onmousedown = (e) => {
            
            // Press the mouse to calculate the distance between the current element and the visible area
            const disX = e.clientX - el.offsetLeft;
            
            document.onmousemove = function (e) {
                e.preventDefault(); // Disable default events on move

                // Calculate the moving distance through event delegation 
                const l = e.clientX - disX;
                dragDom.style.width = `${l}px`;
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }  
    }
})

main.js:

// Import custom instruction
import './directives.js';

Posted by Lee W on Thu, 02 Apr 2020 15:31:05 -0700