【问题标题】:Determine current mouse position in IFRAME?确定 IFRAME 中的当前鼠标位置?
【发布时间】:2011-08-01 14:10:35
【问题描述】:

我在 IFRAME 中有一个 IFRAME,而我的 getMousePosition 方法无法检索 iframe 中的当前鼠标位置。它在第一个 Iframe 中工作,但是当我从父文档中的函数调用 getMousePosition 时,它返回后备值 600 和 350。仅供参考:我无法控制生成 IFrame 的内容,但它不是跨域访问。 IFRAMES 和父文档都托管在同一台服务器上。我只是为 Internet Explorer 8 编程。所以浏览器兼容性不是问题。

  function getMousePosition(){
  if(!inframe)
      $(document).mousemove(function(e){
           mouseX = e.pageX 
           mouseY = e.pageY 
      });
  else
  {
    mouseX = 600;
    mouseY = 350;
  }

  //This is where I get the Iframe Document (I then parse through the document, picking up the specific links and storing them in the array verifiedlinks)
  var src = window.frames[iframeindex].document.getElementsByTagName("a");
  // This is where I call my function which uses the values returned by getMousePosition (verifiedlinks is an array of links inside the iframe):

    verifiedlinks[i].onmouseover = function() 
    {
        showPeopleDetails(this.getAttribute('username'));
    }
  // This should display User Details at the current Mousecoordinates
function showPeopleDetails(UserId){
var vpd = document.getElementById("PeopleDetails");
    if ( vpd != null ) {
        getMousePosition();
        vpd.style.left=mouseX+10; //mouseX and mouseY are defined globally
        vpd.style.top=mouseY+10;
        vpd.style.display="block";
    }
}

我已阅读此问题:SOLVED QUESTION 但答案并没有解决我的问题。 我找到了这个question,但似乎没有一个答案对我有用。 我新编辑的代码:

function showPeopleDetails(UserId, x, y){
var vpd = document.getElementById("PeopleDetails");
try
{
    if ( vpd != null ) {
        //getMousePosition();
        //alert("MouseX: " +mouseX+" MouseY: "+mouseY);
        //vpd.style.left=mouseX+10;
        //vpd.style.top=mouseY+10;
        vpd.style.left = x +10 - window.frames[2].document.body.scrollLeft;
        vpd.style.top = y +10 - window.frames[2].document.body.scrollTop;
     }
}

【问题讨论】:

    标签: javascript jquery iframe mousemove mouse-position


    【解决方案1】:

    如果您从父窗口调用 getMousePosition,则 document 将指向父窗口文档。您应该在 iframe 的上下文中调用此方法。此外,inframe 是在哪里定义的,您是否会在任何事件上更新其值?

    您可以使用 jquery 将鼠标悬停事件附加到链接。使用它,您将获得事件对象,该对象提供链接相对于文档的鼠标 x/y 坐标。希望对你有帮助。

    $(verifiedlinks[i]).mouseover(function(e){
            showPeopleDetails($(this).attr('username'), e.pageX, e.pageY);
    }
    
    function showPeopleDetailsNow(UserId, x, y){
    var vpd = document.getElementById("PeopleDetails");
        if ( vpd != null ) {
            getMousePosition();
            //vpd.style.left=mouseX+10; //mouseX and mouseY are defined globally
            //vpd.style.top=mouseY+10;
            vpd.style.left= x +10 + $(document).scrollTop(); //mouseX and mouseY are defined globally
            vpd.style.top= y +10;
            vpd.style.display="block";
        }
    }
    

    【讨论】:

    • 是的,我每次解析 iframe 时都会将 INFRAME 更新为 true。当用户将鼠标悬停在特定链接上时,我想弹出一个气球工具提示。
    • 您应该直接将鼠标悬停事件附加到 iframe 内的链接,而不是通过父窗口进行。你试图做的方式是行不通的。
    • 对不起,我应该澄清一下:我正在更改 IFrame 中的链接并且链接正在工作,只是气球工具提示显示在错误的坐标处:当我将鼠标悬停在 iframe 内的链接上时,气球是始终以 600px、350px 显示。所以我假设我的 getMousePosition 没有更新 IFRAME 中的值。
    • 谢谢,您的解决方案部分为我工作。对于前 6 个链接,气球显示正确。但是一旦我滚动到下一个链接,气球就会向下显示。我只看到滚动条非常扩大,当我向下滚动到页面末尾时,气球会显示在那里..
    • 您还需要添加滚动顶部,检查我编辑的答案。它现在应该适用于所有链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多