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

编程入门 行业动态 更新时间:2024-10-25 14:23:05
本文介绍了从iframe内部获取绝对鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有iframe的网页呈现另一个页面(同一个域)。我需要获取与父文档相关的鼠标位置。请注意,iframe可以双向滚动。我尝试过使用偏移而没有运气。

I have a webpage with an iframe rendering another page (same domain). I need to get the mouse position in relation to the parent document. Keep in mind the iframe can scroll both ways. I've tried using offset with no luck.

$('#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本身的鼠标位置。在下面扩展您的代码,

One way to do it would be to get the position of the iframe in the parent window and add it to the mouse position relative to the iframe itself. Extending your code below,

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); })

更多推荐

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

本文发布于:2023-11-25 08:54:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   位置   iframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!