gpt4 book ai didi

javascript - 将元素定位在点击区域附近

转载 作者:行者123 更新时间:2023-11-28 01:07:03 26 4
gpt4 key购买 nike

我正在尝试将上下文菜单与 jQuery 完整日历插件集成。我找不到我尝试构建自己的任何示例。它的工作但没有得到正确的位置。我已经尝试了将近 10 天,并在谷歌上搜索了很多,但找不到解决方案。

我根据传递和显示的事件对象手动为菜单项生成 HTML。我用过Positioning Context Menu位置的解决方案。

如何正确定位上下文菜单?

$('#calendar').fullCalendar({
//all fullCalendar options goes here,
eventRender: function (event, element, view) {
element.bind('contextmenu', function (e) {
//generate my own <ul><li> html based on event object
var contextMenuHtml = getContextMenu(event);
//contextMenuContainer is hidden div in at the bottom of <body>
$('#contextMenuContainer').html(contextMenuHtml);

$('#contextMenuContainer').fadeIn();
var position = element.position();

$('#contextMenuContainer').css({
left: e.pageX, //to show the container close to where i click
top: e.pageY // How can i adjust or calculate position if i click near to edge of window
});

$(document).click(function () {
$('#contextMenuContainer').fadeOut();
});

return false;
});
}
});

enter image description here

我试着用这个 jQuery contextMenu插件,但不符合我的要求。我需要根据上面显示的 fullCalander 事件对象动态生成带有链接的菜单项。但不幸的是,这些插件不支持这样的,也没有这样的例子。

最佳答案

将我的评论移至答案,因为在依赖项下 jQuery-contextMenujQuery UI position 列为可选: https://github.com/swisnl/jQuery-contextMenu#dependencies

您可以在此处仅使用此功能构建一个 jQueryUI 包:http://jqueryui.com/download/

或者看起来 contextMenu 在其分发文件中有一个版本:https://github.com/swisnl/jQuery-contextMenu/tree/master/dist

$('#calendar').fullCalendar({
//all fullCalendar options goes here,
eventRender: function (event, element, view) {
element.bind('contextmenu', function (e) {

var contextMenuHtml = getContextMenu(event);

$('#contextMenuContainer')
.html(contextMenuHtml)
.position({
my: 'left-5 top-5',
of: e,
collision: "fit"
})
.fadeIn();


$(document).click(function () {
$('#contextMenuContainer').fadeOut();
});

return false;
});
}
});

关于javascript - 将元素定位在点击区域附近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39300694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com