上下文菜单的定位
我在javascript中为表格开发了一个右键单击上下文菜单。上下文菜单的位置位于表格每一行的光标下方。表格的最后一行位于页面末尾,现在右键单击该行上下文菜单正在下降,但它应该显示在光标上。请提供任何帮助
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(事件);
I have developed a right click context menu in javascript for table .The position of context menu is at the down of cursor for every row of table.The final row of table is at end of the page,Now on right clicking the row the context menu is coming down but it should be shown up the cursor.Any help please
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;
}
Here, the contextShow will display the context menu of right click based on the mouse position using getMousePosition(event); and getScrollPosition(event);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用以下函数来设置上下文菜单位置,它对我有用。
I use the following function to set context menu position, and it works for me.
显示上下文菜单时,您应该检查鼠标光标是在屏幕的下半部分还是上半部分。如果是下半部分,您应该将其显示在光标的顶部,反之亦然。这可以应用于屏幕的右半部分和左半部分也是如此,因此您的菜单将根据您的光标所在的屏幕的四分之一而显示。如果您这样做,则无论您的光标在哪里,您都可以确保您的菜单始终可见。
例如:如果鼠标 x 坐标 >屏幕高度/2 比光标顶部显示菜单的高度...
When displaying the context menu you should check if the mouse cursor is in the bottom half or the top half of the screen.Then if it is the bottom half you should display it at top of the cursor and vice versa.This can be applied for the right and left half of the screen too so then your menu will appear depending in which quarter of the screen your cursor is.if you do this, you are sure that your menu is always visible no mater where you cursor is.
Ex: if mouse x coordinates > screen height/2 than display menu on top of the cursor...