Jelajahi Sumber

处理右键菜单,处理有些电脑出现主菜单与子菜单距离过远的问题

zeweizhong 6 tahun lalu
induk
melakukan
ca2f752b94
1 mengubah file dengan 16 tambahan dan 0 penghapusan
  1. 16 0
      lib/jquery-contextmenu/jquery.contextMenu.js

+ 16 - 0
lib/jquery-contextmenu/jquery.contextMenu.js

@@ -199,6 +199,21 @@
 
                 opt.$menu.css(offset);
             },
+            // 有的电脑会出现主菜单和子菜单距离比较远的情况,这里检查一下。
+            checkSubmenuPosition: function ($menu) {
+                if (!$menu) {
+                    return;
+                }
+                const parentOffsetWidth = $menu.parentElement.offsetWidth;
+                const curLeft = parseFloat(window.getComputedStyle($menu).left) || 0;
+                const maxDiff = 8;
+                const minDiff = 4;
+                const diff = parentOffsetWidth - curLeft;
+                if (diff > maxDiff || diff < minDiff) {
+                    const fixedLeft = parentOffsetWidth - 5;
+                    $menu.style.left = `${fixedLeft}px`;
+                }
+            },
             // position the sub-menu
             positionSubmenu: function ($menu) {
                 if (typeof $menu === 'undefined') {
@@ -224,6 +239,7 @@
                     };
                     $menu.css(offset);
                 }
+                defaults.checkSubmenuPosition($menu[0]);
             },
             // offset to add to zIndex
             zIndex: 1,