gpt4 book ai didi

javascript - 定位上下文菜单

转载 作者:可可西里 更新时间:2023-11-01 02:56:00 25 4
gpt4 key购买 nike

我正在尝试使用 jQuery 定位自定义上下文菜单。
它第一次出现在正确的位置(鼠标坐标),但随后当前位置与新位置相加,因此菜单从屏幕上消失。
这是 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>

最佳答案

尝试位置:固定;上下文菜单的位置根据以下条件更改 -

var windowHeight = $(window).height()/2;
var windowWidth = $(window).width()/2;
if(e.clientY > windowHeight && e.clientX <= windowWidth) {
$("#contextMenuContainer").css("left", e.clientX);
$("#contextMenuContainer").css("bottom", $(window).height()-e.clientY);
$("#contextMenuContainer").css("right", "auto");
$("#contextMenuContainer").css("top", "auto");
} else if(e.clientY > windowHeight && e.clientX > windowWidth) {
$("#contextMenuContainer").css("right", $(window).width()-e.clientX);
$("#contextMenuContainer").css("bottom", $(window).height()-e.clientY);
$("#contextMenuContainer").css("left", "auto");
$("#contextMenuContainer").css("top", "auto");
} else if(e.clientY <= windowHeight && e.clientX <= windowWidth) {
$("#contextMenuContainer").css("left", e.clientX);
$("#contextMenuContainer").css("top", e.clientY);
$("#contextMenuContainer").css("right", "auto");
$("#contextMenuContainer").css("bottom", "auto");
} else {
$("#contextMenuContainer").css("right", $(window).width()-e.clientX);
$("#contextMenuContainer").css("top", e.clientY);
$("#contextMenuContainer").css("left", "auto");
$("#contextMenuContainer").css("bottom", "auto");
}

http://jsfiddle.net/AkshayBandivadekar/zakn7Lwb/14/

关于javascript - 定位上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795253/

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