elementui弹框放大缩小和拖拽 第三节

 //鼠标拉伸弹窗
        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) {
                    dragDom.querySelector('.el-dialog__body').style.height = 'calc(100% - '+dialogHeaderEl.offsetHeight+'px)';
                    hasSetBodyHight = true;
                }
            };
            //拉伸结束
            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
    }
}

posted on 2022-05-09 15:23  Dong1995  阅读(401)  评论(0编辑  收藏  举报

导航