2

我正在编写一个用于向元素添加上下文菜单的 jQuery 插件。现在,我需要修复上下文菜单的位置。如果没有足够的空间从鼠标底部显示整个上下文菜单,那么它应该从鼠标顶部显示。我尝试了一些东西,但没有像我想要的那样工作。

这是 jsFinddle 上的代码:http: //jsfiddle.net/evandroprogram/pRPQq/

谢谢你。

4

1 回答 1

1

给你,伙计,我在这里分叉并更新了你的小提琴

http://jsfiddle.net/joevallender/j5Vy9/

代码更改是这样的

var screenBottom = $(window).scrollTop() + $(window).height();
var menuHeight = _contextMenu.height();
var menuBottom = menuHeight + options.positionY;
if(menuBottom > screenBottom) {
    _contextMenu.css({
        top: "-=" + menuHeight
    })
}

在您设置 _contextMenu.css() 之后放置

编辑刚刚再次测试,这不是像素完美的,但它确实有效,如果你想调整它应该给你一个不错的线索:)

于 2012-08-22T10:52:50.740 回答