gpt4 book ai didi

javascript - 从 iframe 内部获取鼠标绝对位置

转载 作者:行者123 更新时间:2023-12-03 22:49:17 28 4
gpt4 key购买 nike

我有一个网页,其中的 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/

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