javascript – 上下文菜单的定位
作者:互联网
我在javascript for table中开发了一个右键单击上下文菜单.上下文菜单的位置在每行表格的光标下方.表格的最后一行是在页面的末尾,现在右键单击该行的上下文菜单正在下降但它应该显示在光标上.任何帮助请
function ContextShow(event) {
event = event || window.event;
var m = getMousePosition(event);
var s = getScrollPosition(event);
var client_height = document.body.clientHeight;
var display_context = document.getElementById('context_menu');
if(replaceContext){
display_context.style.display = "block";
display_context.style.left = m.x + s.x + "px";
display_context.style.top = m.y + s.y + "px";
replaceContext = false;
}}
function getMousePosition (e){
e = e || window.event;
var position = {
'x' : e.clientX,
'y' : e.clientY
}
return position;}
function getScrollPosition(){
var x = 0;
var y = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
x = window.pageXOffset;
y = window.pageYOffset;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
x = document.body.scrollLeft;
y = document.body.scrollTop;
}
var position = {
'x' : x,
'y' : y
}
return position;
}
这里,contextShow将使用getMousePosition(event)显示基于鼠标位置的右键单击上下文菜单;和getScrollPosition(事件);
解决方法:
我使用以下函数来设置上下文菜单位置,它适用于我.
function setContextMenuPostion(event, contextMenu) {
var mousePosition = {};
var menuPostion = {};
var menuDimension = {};
menuDimension.x = contextMenu.outerWidth();
menuDimension.y = contextMenu.outerHeight();
mousePosition.x = event.pageX;
mousePosition.y = event.pageY;
if (mousePosition.x + menuDimension.x > $(window).width() + $(window).scrollLeft()) {
menuPostion.x = mousePosition.x - menuDimension.x;
} else {
menuPostion.x = mousePosition.x;
}
if (mousePosition.y + menuDimension.y > $(window).height() + $(window).scrollTop()) {
menuPostion.y = mousePosition.y - menuDimension.y;
} else {
menuPostion.y = mousePosition.y;
}
return menuPostion;
}
标签:javascript,contextmenu,right-click 来源: https://codeday.me/bug/20190621/1255407.html