Forum Moderators: open

Message Too Old, No Replies

How to position a hover div relative to the hover target

         

snowdzine

7:32 am on Feb 7, 2007 (gmt 0)

10+ Year Member



I am trying to create a hover effect where the div that appears is placed directly below the target in a consistent location. I have tried solutions trapping the mouse location, however, the div then moves around. Inside the div would be links, so once the visibility is set to visible, I would like the location to be fixed.

The main problem that I have discovered is that once the target links are in a table, the relative positioning is not consistent and I feel that the table may be a causing factor.

Any help is appreciated.

Here is some of the code:

<html>
<head>

<script type="text/javascript">
function ShowPopup(hoveritem)
{
hp = document.getElementById("hoverpopup");

// Set popup to visible
hp.style.top = hoveritem.offsetTop + maintable.offsetTop + 10;
hp.style.left = hoveritem.offsetLeft + maintable.offsetTop + -40;

hp.style.visibility = "Visible";
}

function KeepPopup(hoveritem)
{
hp = document.getElementById("hoverpopup");

// Set popup to visible
hp.style.top = hoveritem.offsetTop;
hp.style.left = hoveritem.offsetLeft;

hp.style.visibility = "Visible";
}

function HidePopup()
{
hp = document.getElementById("hoverpopup");
hp.style.visibility = "Hidden";
}
</script>

</head>
<body>

<table id="maintable" width="95%" border="0" align="center" cellpadding="5" cellspacing="1">
<tr>
<td bgcolor="#eeeeee">&nbsp;</td>
<td bgcolor="#eeeeee"><a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">(link)</a><br>some more random text</td>
<td bgcolor="#eeeeee"><a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">(link)</a></td>
</tr>
<tr>
<td bgcolor="#eeeeee">&nbsp;</td>
<td bgcolor="#eeeeee">Another text link <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">(link)</a></td>
<td bgcolor="#eeeeee">&nbsp;</td>
</tr>
<tr>
<td bgcolor="#eeeeee">&nbsp;</td>
<td bgcolor="#eeeeee">Another text link <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">(link)</a></td>
<td bgcolor="#eeeeee">&nbsp;</td>
</tr>
</table>

<p align="center">Another text link <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">(link)</a></p>

<!-- START Popup1 -->
<div id="hoverpopup" style="visibility:hidden; position: absolute; width:200px; border:1px solid #ffffff; background-color:#ffffff;" onMouseOver="KeepPopup(this);" onMouseOut="HidePopup();">
<table width="200px" border="0" cellspacing="0" cellpadding="10" style="border:1px solid #000000;">
<tr>
<td class="tiny" align="left" nowrap="nowrap">
<a href="#">Link 1</a><br />
<a href="#">Link 2</a></td></tr>
</table>
</div>

</body>
</html>

daveVk

12:58 pm on Feb 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change

// Set popup to visible
hp.style.top = hoveritem.offsetTop + maintable.offsetTop + 10;
hp.style.left = hoveritem.offsetLeft + maintable.offsetTop + -40;

hp.style.visibility = "Visible";

TO

// Set popup to visible
if ( hp.style.visibility!= "visible" ) {
hp.style.top = hoveritem.offsetTop + maintable.offsetTop + 10;
hp.style.left = hoveritem.offsetLeft + maintable.offsetTop + -40;

hp.style.visibility = "visible";
}

"Visible" may be ok, "visible" is safer.

snowdzine

6:30 pm on Feb 7, 2007 (gmt 0)

10+ Year Member



Thanks daveVk,
However, the main problem that I have and still cannot get it working is that while the javascript is calling for the hover popup to appear 10px down and 40px to the left of the image location, it does not work within a table.

The bottom link that is not in the table displays properly, however, the other links within the table place the div nowhere near the hover target. Is there something I could call for it to see if it is within a table and then adjust the offset accordingly?

function ShowPopup(hoveritem)
{
hp = document.getElementById("hoverpopup");

// Set popup to visible
if ( hp.style.visibility!= "visible" ) {
hp.style.top = hoveritem.offsetTop + 10;
hp.style.left = hoveritem.offsetLeft - 40;

hp.style.visibility = "visible";
}
}

daveVk

12:15 am on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may help [webmasterworld.com...] you need to get the absolute position, not relative to table cell or whatever.