Просмотр исходного кода

【修复】调整右键菜单隐藏时的位置处理

- 移除 center 函数,改为实现 inScreen 函数
- 将菜单隐藏时的位置调整为屏幕左上角附近 (2px, 2px)
- 修改相关事件处理中菜单隐藏逻辑,统一使用 inScreen 函数
PJ568 1 год назад
Родитель
Сommit
2213122c8c
1 измененных файлов с 8 добавлено и 15 удалено
  1. 8 15
      index.html

+ 8 - 15
index.html

@@ -319,17 +319,10 @@
 
       // 自定义右键菜单功能
       function initOptionMenu(doc) {
-        // 移动到中心
-        function center(contextMenu) {
-          // 隐藏菜单后,将菜单移动到屏幕中心
-          const windowWidth = window.innerWidth;
-          const windowHeight = window.innerHeight;
-          const menuWidth = contextMenu.offsetWidth;
-          const menuHeight = contextMenu.offsetHeight;
-          const centerX = (windowWidth - menuWidth) / 2;
-          const centerY = (windowHeight - menuHeight) / 2;
-          contextMenu.style.left = centerX + "px";
-          contextMenu.style.top = centerY + "px";
+        // 隐藏菜单后,将菜单移动到屏幕内
+        function inScreen(contextMenu) {
+          contextMenu.style.left = "2px";
+          contextMenu.style.top = "2px";
         }
 
         // 初始化样式
@@ -450,7 +443,7 @@
                   doc.execCommand("selectAll");
                 }
                 contextMenu.classList.remove("show");
-                center(contextMenu);
+                inScreen(contextMenu);
               };
               contextMenu.appendChild(li);
             });
@@ -505,7 +498,7 @@
                   }
                   // 隐藏菜单
                   contextMenu.classList.remove("show");
-                  center(contextMenu);
+                  inScreen(contextMenu);
                 };
                 contextMenu.appendChild(li);
               });
@@ -540,14 +533,14 @@
           } else {
             // 如果没有定义菜单选项,则隐藏菜单
             contextMenu.classList.remove("show");
-            center(contextMenu);
+            inScreen(contextMenu);
           }
         });
 
         // 点击其他地方隐藏菜单
         doc.addEventListener("click", function () {
           contextMenu.classList.remove("show");
-          center(contextMenu);
+          inScreen(contextMenu);
         });
 
         // 阻止菜单内部的点击事件冒泡