将鼠标运动附加到内部的iframe

发布于 2025-02-13 02:14:55 字数 2260 浏览 1 评论 0原文

在我的JS代码中,我在mousemove& touchMove,这使我的div遵循光标(因此)

css

  <style>
    body {
padding: 0;
margin: 0;
height: 100vh;
background: linear-gradient(135deg, #8fc7f1, #7173f5);
overflow: hidden;
}
#my-div {
width: 300px;
height: 250px;
background-color: #ffffff;
position: absolute;
}
</style>

html:

<div id="my-div">
  <h1>Hello</h1>
</div>

javascript:

let myDiv = document.getElementById("my-div");
//Detect touch device
function isTouchDevice() {
  try {
    //We try to create TouchEvent. It would fail for desktops and throw error
    document.createEvent("TouchEvent");
    return true;
  } catch (e) {
    return false;
  }
}

const move = (e) => {
  //Try, catch to avoid any errors for touch screens (Error thrown when user doesn't move his finger)
  try {
    //PageX and PageY return the position of client's cursor from top left of screen
    var x = !isTouchDevice() ? e.pageX : e.touches[0].pageX;
    var y = !isTouchDevice() ? e.pageY : e.touches[0].pageY;
  } catch (e) {}
  //set left and top of div based on mouse position
  myDiv.style.left = x - 150 + "px";
  myDiv.style.top = y - 120 + "px";
  // myDiv.style.opacity = 0;
  // console.log(myDiv.getBoundingClientRect())
};
//For mouse
document.addEventListener("mousemove", (e) => {
  move(e);
  console.log("Mouse")
});

//For touch
document.addEventListener("touchmove", (e) => {
  move(e);
});

jsfiddle代码

我要做的是添加一个iframe在我的divdiv仍然应遵循Cursor,但是当我添加iframe时div中,然后eventListener停止工作,而我的div在光标之后停止。我不确定问题是什么。 (因此)

<div id="my-div">
    <iframe src="https://example.com/" width="300" height="250">
</div>

带有iframe的JSFIDDLE代码

任何帮助和建议都值得赞赏!

In my JS code, I've added a EventListener on mousemove & touchmove, which makes it so that my div follows the cursor. (as such)

CSS:

  <style>
    body {
padding: 0;
margin: 0;
height: 100vh;
background: linear-gradient(135deg, #8fc7f1, #7173f5);
overflow: hidden;
}
#my-div {
width: 300px;
height: 250px;
background-color: #ffffff;
position: absolute;
}
</style>

HTML:

<div id="my-div">
  <h1>Hello</h1>
</div>

JAVASCRIPT:

let myDiv = document.getElementById("my-div");
//Detect touch device
function isTouchDevice() {
  try {
    //We try to create TouchEvent. It would fail for desktops and throw error
    document.createEvent("TouchEvent");
    return true;
  } catch (e) {
    return false;
  }
}

const move = (e) => {
  //Try, catch to avoid any errors for touch screens (Error thrown when user doesn't move his finger)
  try {
    //PageX and PageY return the position of client's cursor from top left of screen
    var x = !isTouchDevice() ? e.pageX : e.touches[0].pageX;
    var y = !isTouchDevice() ? e.pageY : e.touches[0].pageY;
  } catch (e) {}
  //set left and top of div based on mouse position
  myDiv.style.left = x - 150 + "px";
  myDiv.style.top = y - 120 + "px";
  // myDiv.style.opacity = 0;
  // console.log(myDiv.getBoundingClientRect())
};
//For mouse
document.addEventListener("mousemove", (e) => {
  move(e);
  console.log("Mouse")
});

//For touch
document.addEventListener("touchmove", (e) => {
  move(e);
});

JSFIDDLE CODE

What I'm trying to do is to add a iframe inside my div and the div should still follow the cursor, but when I add the iframe inside the div, then the EventListener stops working and my div stops following the cursor. I'm not sure what the issue is. (as such)

<div id="my-div">
    <iframe src="https://example.com/" width="300" height="250">
</div>

JSFIDDLE CODE WITH IFRAME

Any help and suggestion is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2025-02-20 02:14:55

您忘记了关闭iframe标签

<div id="my-div">
  <iframe src="https://example.com/" width="300" height="250"></iframe>
</div>

You forgot the closing iframe tag

<div id="my-div">
  <iframe src="https://example.com/" width="300" height="250"></iframe>
</div>
小鸟爱天空丶 2025-02-20 02:14:55

我认为您应该将DIV的位置更改为相对和iframe的位置。

I think you should change the position of your div to relative and the position of your iframe to absolute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文