gpt4 book ai didi

javascript - 确定 iframe 内的鼠标位置,但包括父级

转载 作者:行者123 更新时间:2023-11-28 21:20:14 33 4
gpt4 key购买 nike

我有一个 iframe,在其中检测到右键单击并将鼠标事件传递给父级中的函数。在这里,(在父函数内部),我有显示自定义上下文菜单的逻辑,并且上下文菜单 html 标记被插入到父 DOM 中。因此需要根据视口(viewport)(或父 DOM)确定鼠标位置,但我收到的位置是相对于 iframe 的。

我尝试使用 offsetTop 和 offsetParent,但这仅迭代到内部页面的 body 标记。

最佳答案

这是我使用的功能:

// Define class that will hold Object coordinates
function CTopLeft(i_nTop, i_nLeft) {
this.nTop = i_nTop;
this.nLeft = i_nLeft;
}

function GetTopLeftFromIframe(i_oElem) {
var cTL = new CTopLeft(0, 0);
var oElem = i_oElem;
var oWindow = window;

do {
cTL.nLeft += oElem.offsetLeft;
cTL.nTop += oElem.offsetTop;
oElem = oElem.offsetParent;

if (oElem == null) { // If we reach top of the ancestor hierarchy
oElem = oWindow.frameElement; // Jump to IFRAME Element hosting the document
oWindow = oWindow.parent; // and switching current window to 1 level up
}
} while (oElem)

return cTL;
}

这是来自 http://codecorner.galanter.net/2012/02/26/absolute-coordinates-of-element-inside-of-iframe/

关于javascript - 确定 iframe 内的鼠标位置,但包括父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572992/

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