From 04a22ee760dfcbfe753231e4cfb89c1f1d0e991e Mon Sep 17 00:00:00 2001 From: dong <58670809@qq.com> Date: Fri, 8 Jul 2022 16:34:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=96=E6=8B=BD=E8=B6=85=E5=87=BA=E6=97=B6?= =?UTF-8?q?=E9=99=90=E5=88=B6=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/directive/dialogDrag/dialogDrag.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/directive/dialogDrag/dialogDrag.js b/src/directive/dialogDrag/dialogDrag.js index 36af40898..473b2e953 100644 --- a/src/directive/dialogDrag/dialogDrag.js +++ b/src/directive/dialogDrag/dialogDrag.js @@ -10,10 +10,12 @@ export default { const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); dialogHeaderEl.onmousedown = (e) => { - e.stopPropagation(); + e.stopPropagation(); /** 鼠标按下,计算当前元素距离可视区的距离*/ const disX = e.clientX - dialogHeaderEl.offsetLeft; const disY = e.clientY - dialogHeaderEl.offsetTop; + const oY = e.offsetY; + const bY = dialogHeaderEl.offsetHeight - e.offsetY; /** 获取到的值带px 正则匹配替换*/ let styL, styT; @@ -31,10 +33,17 @@ export default { } document.onmousemove = function (e) { - e.stopPropagation(); + e.stopPropagation(); + let cY = e.clientY; + if (cY < oY) { + cY = oY; + } + if (cY > document.body.clientHeight - bY) { + cY = document.body.clientHeight - bY; + } /** 通过事件委托,计算移动的距离*/ const l = e.clientX - disX; - const t = e.clientY - disY; + const t = cY - disY; /** 移动当前元素*/ dragDom.style.left = `${l + styL}px`; @@ -48,7 +57,7 @@ export default { }; document.onmouseup = function () { - e.stopPropagation(); + e.stopPropagation(); document.onmousemove = null; document.onmouseup = null; };