Skip to content

Commit 0d8a4f8

Browse files
committed
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
1 parent 2ae293c commit 0d8a4f8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/jquery.contextMenu.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1579,18 +1579,32 @@
15791579
var $menu = opt.$menu;
15801580
var $menuOffset = $menu.offset();
15811581
var winHeight = $(window).height();
1582+
var winWidth = $(window).width();
15821583
var winScrollTop = $(window).scrollTop();
1584+
var winScrollLeft = $(window).scrollLeft();
15831585
var menuHeight = $menu.height();
1586+
var outerHeight = $menu.outerHeight();
1587+
var outerWidth = $menu.outerWidth();
1588+
15841589
if(menuHeight > winHeight){
15851590
$menu.css({
15861591
'height' : winHeight + 'px',
15871592
'overflow-x': 'hidden',
15881593
'overflow-y': 'auto',
15891594
'top': winScrollTop + 'px'
15901595
});
1591-
} else if(($menuOffset.top < winScrollTop) || ($menuOffset.top + menuHeight > winScrollTop + winHeight)){
1596+
} else if($menuOffset.top < winScrollTop){
15921597
$menu.css({
1593-
'top': winScrollTop + 'px'
1598+
'top': winScrollTop + 'px'
1599+
});
1600+
} else if($menuOffset.top + outerHeight > winScrollTop + winHeight){
1601+
$menu.css({
1602+
'top': $menuOffset.top - (($menuOffset.top + outerHeight) - (winScrollTop + winHeight)) + "px"
1603+
});
1604+
}
1605+
if($menuOffset.left + outerWidth > winScrollLeft + winWidth){
1606+
$menu.css({
1607+
'left': $menuOffset.left - (($menuOffset.left + outerWidth) - (winScrollLeft + winWidth)) + "px"
15941608
});
15951609
}
15961610
}

0 commit comments

Comments
 (0)