From 0d8a4f8ba583ee816809648e0de14706fbc83a22 Mon Sep 17 00:00:00 2001 From: Speedphoenix Date: Mon, 19 Jul 2021 11:41:31 +0200 Subject: [PATCH] Fix menu jumping to top of screen This commit adds in changes proposed by @lukefoley in issue #749, and fixes the contextmenu going out of the window when created at the right end of the window --- src/jquery.contextMenu.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/jquery.contextMenu.js b/src/jquery.contextMenu.js index fc4e90f9..ace46e63 100644 --- a/src/jquery.contextMenu.js +++ b/src/jquery.contextMenu.js @@ -1579,8 +1579,13 @@ var $menu = opt.$menu; var $menuOffset = $menu.offset(); var winHeight = $(window).height(); + var winWidth = $(window).width(); var winScrollTop = $(window).scrollTop(); + var winScrollLeft = $(window).scrollLeft(); var menuHeight = $menu.height(); + var outerHeight = $menu.outerHeight(); + var outerWidth = $menu.outerWidth(); + if(menuHeight > winHeight){ $menu.css({ 'height' : winHeight + 'px', @@ -1588,9 +1593,18 @@ 'overflow-y': 'auto', 'top': winScrollTop + 'px' }); - } else if(($menuOffset.top < winScrollTop) || ($menuOffset.top + menuHeight > winScrollTop + winHeight)){ + } else if($menuOffset.top < winScrollTop){ $menu.css({ - 'top': winScrollTop + 'px' + 'top': winScrollTop + 'px' + }); + } else if($menuOffset.top + outerHeight > winScrollTop + winHeight){ + $menu.css({ + 'top': $menuOffset.top - (($menuOffset.top + outerHeight) - (winScrollTop + winHeight)) + "px" + }); + } + if($menuOffset.left + outerWidth > winScrollLeft + winWidth){ + $menu.css({ + 'left': $menuOffset.left - (($menuOffset.left + outerWidth) - (winScrollLeft + winWidth)) + "px" }); } }