简体   繁体   English

如何使用jQuery在鼠标悬停时移动iframe

[英]how to move iframe on mouse over using jquery

I am attempting to move an iframe on mouseover in jquery. 我正在尝试在jquery中的鼠标悬停时移动iframe。 I have had some success with this by capturing the mouse position within the iframe, but the flicker off the movement is extreme. 通过在iframe中捕获鼠标位置,我在此方面取得了一些成功,但是动作的闪烁非常极端。

Here is a jsfiddle example 是一个jsfiddle示例

As far as I can see, the reason for the flicker is that every time the iframe moves the x position inside the iframe jumps in the opposite direction, but I can not figure a way of fixing it. 据我所知,闪烁的原因是,每当iframe移动iframe中的x位置时,它都会朝相反的方向跳跃,但是我找不到解决它的方法。

@charlietf, @charlietf,

Thanks for your efforts, it did indeed lead me in the right direction and I have given a vote to your comment. 感谢您的努力,它的确为我指明了正确的方向,我对您的评论投了赞成票。

The only problem with your example is that as the iframe x position changed the e.pageX changes with it. 您的示例的唯一问题是,随着iframe x位置的更改,e.pageX也随之更改。 This means it always equals 0 (or close to). 这意味着它始终等于0(或接近)。

To get past this I had to add a variable that captures the mouse position within the iframe before any movement has taken place and then remove this number from the x variable when moving the frame. 为了克服这个问题,我必须添加一个变量,该变量在发生任何移动之前捕获iframe中鼠标的位置,然后在移动帧时从x变量中删除此数字。

For anyone out there looking for something similar, here is an update version of charlietf's script with the changes written in . 对于那些正在寻找类似内容的人, 这里是charlietf脚本的更新版本,其中写入了更改 I have also changed from mouseover mouseout events to mousedown mouseup in order to make it easier to play with. 我还从mouseover mouseout事件更改为mousedown mouseup事件,以使其更易于使用。

Now I can drag an iframe around from any non active part of my iframe; 现在,我可以从iframe的任何非活动部分拖动iframe;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM