Javascript:open&关闭图像上的新窗口onMouseOver& onMouseOut,但仅当新窗口onMouseOver = true时

时间:2011-07-21 03:49:25

标签: javascript window onmouseover onmouseout

谢谢大家以前用我的Javascripting问题来帮助我。我目前的问题是我需要打开&关闭图像上的新窗口onMouseOver& onMouseOut,但如果新窗口onMouseOver == true,那么我不希望新窗口关闭。

我确信有一个简单的解决方案,但我似乎无法找到取消图像的方法onMouseOut =“closeDetails();”如果用户将鼠标悬停在新窗口上以下是我正在处理的大部分代码。在此先感谢您的帮助。

<body>
   <img  name="img1" id="img1" onMouseOver="windowDelay(this);"
           onMouseOut="closeDetails();" src="images/127.jpg" height="240" width="166"/>
</body>

<script language="JavaScript" type="text/javascript">

// This opens the movie details pop-up after an
// half second interval.
function windowDelay(thatImg)
{
    winOpenTimer = window.setTimeout(function() {openDetails(thatImg);}, 2000);
}


// This is the function that will open the
// new window when the mouse is moved over the image
function openDetails(thatImg) 
{
    // This creates a new window and uses the hovered image name as the window 
    // name so that it can be used in the that window's javascript 
    newWindow = open("", thatImg.name,"width=400,height=500,left=410,top=210");

    // open new document 
    newWindow.document.open();


    // Text of the new document
    // Replace your " with ' or \" or your document.write statements will fail
    newWindow.document.write("<html><head><title>Movies</title>");
    newWindow.document.write("<script src='myDetails.js' type='text/javascript'>");
    newWindow.document.write("</script></head>");
    newWindow.document.write("<body bgcolor='white' onload='popUpDetails();'>");
    newWindow.document.write("... SOME OTHER HTML....");
    newWindow.document.write("</body></html>");


    // close the document
    newWindow.document.close(); 
}



// This is the function that will call the
// closeWindow() after 2 seconds
// when the mouse is moved off the image.
function closeDetails() 
{
    winCloseTimer = window.setTimeout("closeWindow();", 2000);
}

// This function closes the pop-up window
// and turns off the Window Timers
function closeWindow()
{
    // If popUpHover == true then I do not want
    // the window to close
    if(popUpHover == false)
    {
        clearInterval(winOpenTimer); 
        clearInterval(winCloseTimer);
        newWindow.close();
    }
}

function popUpDetails()
{
    // This will be used to prevent the Details Window from closing
    popUpHover = true;

    // Below is some other javascript code...
}
</script> 

1 个答案:

答案 0 :(得分:0)

我建议不要使用新的浏览器窗口来执行此任务。尝试这样的事情:

var popup = {
  open = function () {
    if (this.element == null) {
      // create new div element to be our popup and store it in the popup object 
      this.element = document.createElement('div');
      this.element.id = "myPopup";
      // you don't need a full html document here. Just the stuff you were putting in the <body> tag before
      this.element.innerHTML = "<your>html</here>";
      // Some bare minimum styles to make this work as a popup. Would be better in a stylesheet
      this.element.style = "position: absolute; top: 50px; right: 50px; width: 300px; height: 300px; background-color: #fff;";
    }
    // Add it to your <body> tag
    document.body.appendChild(this.element);
    // call whatever setup functions you were calling before
    popUpDetails();
  },
  close = function () {
    // get rid of the popup
    document.body.removeChild(this.element);
    // any other code you want
  }
};

// The element you want to trigger the popup
var hoverOverMe = document.getElementById("hoverOverMe");
// set our popup open and close methods to the proper events
hoverOverMe.onmouseover = popup.open;
hoverOverMe.onmouseout = popup.close;

应该这样做。它比新的浏览器窗口更容易控制。您需要自己调整CSS。

编辑:

以下是使用实际窗口执行此操作的说明。重申一下,使用实际窗口不是完成此任务的最佳方法。风格化的div标记看起来像一个窗口更好,因为它提供了更多的控制,以及跨浏览器的标准化功能。但是,如果你必须使用一个窗口,那么它是:

// You can use many principles from the example above, but I'll give the quick version
var popup;
var hoverOverMe = document.getElementById("hoverOverMe");

hoverOverMe.onmouseover = function () {
  popup = window.open("path_to_content", "popup");
};
hoverOverMe.onmouseout = function () {
  popup.close();
};

它有效,但不是很好(恕我直言)。如果用户的设置使新窗口中的新窗口打开(就像我一样),则会打开一个选项卡。 Javascript无法控制它。在Firefox中,新选项卡将打开并获得焦点,此时它会立即关闭,因为hoverOverMe已触发onmouseout事件(显然会关闭窗口)。我想你在实际窗口也会遇到同样的问题。