我有一个网页,其中的 iframe 呈现另一个页面(同一域)。我需要获取相对于父文档的鼠标位置。请记住,iframe 可以双向滚动。我尝试过使用偏移量,但没有成功。
$('#iframe').contents().find('html').on('mousemove', function (e) {
//gives me location in terms of the iframe but not the entire page.
var y = e.pageY;
//gives me 0
var y = $(this).offset().top;
//more code here....
})
最佳答案
一种方法是获取 iframe 在父窗口中的位置,并将其添加到相对于 iframe 本身的鼠标位置。扩展下面的代码,
var iframepos = $("#iframe").position();
$('#iframe').contents().find('html').on('mousemove', function (e) {
var x = e.clientX + iframepos.left;
var y = e.clientY + iframepos.top;
console.log(x + " " + y);
})
关于javascript - 从 iframe 内部获取鼠标绝对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28096880/