定位上下文菜单 [英] Positioning Context Menu

查看:71
本文介绍了定位上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery定位自定义上下文菜单。

第一次出现在正确位置(鼠标坐标),但是当前位置正在与新位置相加,以便菜单从屏幕上消失。

这是JavaScript:

I'm trying to position a custom context menu with jQuery.
The first time it appears at the right position (mouse coordinates), but then the current position is being summed up with the new position, so that the menu disappears from the screen.
Here is the JavaScript:

<script>
$(function(){
    $('#box').hide();

    $(document).bind("contextmenu", function(e) {
        $("#box").offset({left:e.pageX, top:e.pageY});
        $('#box').show();
        e.preventDefault();
    });

    $(document).bind("click", function(e) {
        $('#box').hide();
    });
    $('#box').bind("click", function(e) {
        $('#box').hide();
    });
});
</script>


推荐答案

不要使用抵消方法,请尝试 css ,绝对定位上下文菜单:

Don't use offset method, try css instead, positioning context menu absolutely:

$("#box").css({left:e.pageX, top:e.pageY});

CSS:

#box {
    ...
    position: absolute;
}

http://jsfiddle.net/smxLk/

这篇关于定位上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆