From 7fa67909ef0f1e7a276d078437b36eb7d2a4552a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=BD=B3=E5=AE=87?= <17601616548@163.com> Date: Tue, 8 Oct 2024 15:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=96=E6=8B=BD=E8=BF=94=E5=9B=9E=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E6=8C=89=E9=92=AE=E6=97=B6=EF=BC=8C=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E4=B8=8D=E5=8A=A8=EF=BC=8C=E4=B8=94=E5=9C=A8=E5=8F=AF=E8=A7=86?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E5=86=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- h5/pages/components/goHome.vue | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/h5/pages/components/goHome.vue b/h5/pages/components/goHome.vue index 768fba4..1dd54ed 100644 --- a/h5/pages/components/goHome.vue +++ b/h5/pages/components/goHome.vue @@ -30,9 +30,19 @@ const dragButton = (event) => { // 添加鼠标移动监听 mouseMoveTracker = (event) => { + document.body.style.overflow = "hidden"; posX = event.touches[0]?.clientX - startX; posY = event.touches[0]?.clientY - startY; - button.value.style.position = "absolute"; + if (posX < 0) { + posX = 0; + } else if (posX > document.body.clientWidth - button.value.clientWidth) { + posX = document.body.clientWidth - button.value.clientWidth; + } + if (posY < 0) { + posY = 0; + } else if (posY > document.body.clientHeight - button.value.clientHeight) { + posY = document.body.clientHeight - button.value.clientHeight; + } button.value.style.top = `${posY}px`; button.value.style.left = `${posX}px`; }; @@ -40,6 +50,7 @@ const dragButton = (event) => { // 添加鼠标释放监听 mouseUpTracker = () => { + document.body.style.overflow = "auto"; document.removeEventListener("touchmove", mouseMoveTracker); document.removeEventListener("touchend", mouseUpTracker); }; @@ -48,13 +59,21 @@ const dragButton = (event) => { onMounted(() => { if (!button.value) { - document.body.appendChild(button.value); + var child = document.getElementById("draggable-button"); + if (!document.body.contains(child)) { + document.body.appendChild(button.value); + } } }); onBeforeUnmount(() => { if (button.value) { - document.body.removeChild(button.value); + var child = document.getElementById("draggable-button"); + if (document.body.contains(child)) { + document.body.removeChild(child); + } else { + console.log("节点不是父节点的子节点"); + } } });