从鼠标点(及更多)查询位置元素

jQuery position element from the mouse point (and more)

本文关键字:查询 位置 元素 鼠标      更新时间:2023-09-26

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

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

谢谢。

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

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()之后

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