上下文菜单的定位

发布于 2024-10-27 18:57:08 字数 1447 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-11-03 18:57:08

我使用以下函数来设置上下文菜单位置,它对我有用。

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;
}

I use the following function to set context menu position, and it works for me.

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;
}
多情癖 2024-11-03 18:57:08

显示上下文菜单时,您应该检查鼠标光标是在屏幕的下半部分还是上半部分。如果是下半部分,您应该将其显示在光标的顶部,反之亦然。这可以应用于屏幕的右半部分和左半部分也是如此,因此您的菜单将根据您的光标所在的屏幕的四分之一而显示。如果您这样做,则无论您的光标在哪里,您都可以确保您的菜单始终可见。

例如:如果鼠标 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文