From 2ac24349a82283b996df81c6cad00b93a9133ac8 Mon Sep 17 00:00:00 2001 From: xudan Date: Wed, 15 Oct 2025 17:03:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E4=BD=8D=E7=BD=AE=E8=AE=A1=E7=AE=97=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E8=8F=9C=E5=8D=95=E5=9C=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=86=85=E6=98=BE=E7=A4=BA=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E7=94=A8=E6=88=B7=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/context-menu/context-menu.vue | 37 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/components/context-menu/context-menu.vue b/src/components/context-menu/context-menu.vue index d0afb09..6be586d 100644 --- a/src/components/context-menu/context-menu.vue +++ b/src/components/context-menu/context-menu.vue @@ -80,11 +80,42 @@ const { watch( () => props.visible, (isVisible) => { - if (isVisible) { - positionX.value = props.x; - positionY.value = props.y; + if (isVisible && menuRef.value) { + // 获取浏览器窗口尺寸 + const windowWidth = window.innerWidth; + const windowHeight = window.innerHeight; + + // 获取菜单的尺寸 + const menuWidth = menuRef.value.offsetWidth; + const menuHeight = menuRef.value.offsetHeight; + + // 初始位置 + let newX = props.x; + let newY = props.y; + + // 边界检查与调整 + if (newX + menuWidth > windowWidth) { + newX = windowWidth - menuWidth - 5; // 预留5px边距 + } + if (newY + menuHeight > windowHeight) { + newY = windowHeight - menuHeight - 5; // 预留5px边距 + } + + // 确保菜单不会出现在左侧或顶部屏幕外 + if (newX < 0) { + newX = 5; + } + if (newY < 0) { + newY = 5; + } + + positionX.value = newX; + positionY.value = newY; } }, + { + flush: 'post', // 确保在DOM更新后执行 + }, ); // 定义组件名称