1

感谢大家之前帮助我解决我的 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> 
4

1 回答 1

1

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

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 事件已触发(这显然会关闭窗口)。我想您在实际窗口中也会遇到同样的问题。

于 2011-07-21T08:56:06.327 回答