• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

relative positioning when creating a mouseover dynamic drop down table

This code was dropped in a post some time back.  I find it useful, but I'm unable to make the drop down relative when I change the location of the mouseover text (I've added some <BR>s to move it down some).  The drop down box still appears at the top of the HTML page - I have changed some of the position values but then the mouseout doesn't appear to work:


<HTML>
<HEAD>
<TITLE>Drop Down Menu</TITLE>

<style type="text/css">
     td {font-family:verdana, san-serif, arial;font-size:10pt;font-weight:bold;}
     .spcls {background:#EEEEEE;font-family:verdana, san-serif, arial;font-size:10pt;font-weight:bold;}
     #list{position:absolute;z-Index:2}
</style>
<script language="javascript">


showlist = false
timeoutID = ''
list = new Array()
    list[0] = new Array('Option1_1','Option1_2','Option1_3'); // Submenu for menu one
    list[1] = new Array('Option2_1.','Option2_2','Option2_3'); // Submenu for menu two
    list[2] = new Array('Option3_1.','Option3_2','Option3_3');// Submenu for menu three
    list[3] = new Array('Option4_1.','Option4_2','Option4_3');// Submenu for menu four


listUrl = new Array()
    listUrl[0] = new Array('http://microsoft.com','http://microsoft.com','http://microsoft.com'); // URL for menu list one
    listUrl[1] = new Array('http://microsoft.com','http://microsoft.com','http://microsoft.com'); // URL for menu list two
    listUrl[2] = new Array('http://microsoft.com','http://microsoft.com','http://microsoft.com'); // URL for menu list three
     listUrl[3] = new Array('http://microsoft.com','http://microsoft.com','http://microsoft.com'); // URL for menu list four

function listme(val,divposition)
{
     window.clearTimeout(timeoutID)
     bodystart = '<table border=1 >';
     bodypart = '';
     for(i=0; i< list[val].length; i++)
     {
          bodypart = bodypart + '<tr valign="top" align="center"><td><a href="'+listUrl[val][i]+'" onmouseout="returnback()" >'+ list[val][i] +'</a></td></tr>';
     }
     bodyend = '</table>';
     
     if(document.all)
     {
          document.all.list.innerHTML = bodystart + bodypart + bodyend;
          document.all.list.style.top = 40;
          document.all.list.style.left = divposition
          if(document.all.list.style.pixelLeft+100 >= screen.width)
          {
               document.all.list.style.left = screen.width-125
          }
     }
     if(document.layers)
     {
          mylayer = eval(document.list);
          mylayer.document.writeln(bodystart + bodypart + bodyend);
          mylayer.moveTo(divposition,40)
          if(mylayer.pageX+mylayer.clip.right >= window.outerWidth)
          {
               mylayer.moveTo(window.outerWidth-125,40)    
          }
          mylayer.document.close();
     }
     timeoutID = window.setTimeout("deleteme();", 1700);
}

function deleteme()
{
     if(!showlist)
     {
          if(document.all)
          {
               document.all.list.innerHTML = ''
               document.all.list.style.top = 40;
               document.all.list.style.left = 0;
          }
          if(document.layers)
          {
               mylayer = eval(document.list);
               mylayer.document.write('');
               mylayer.moveTo(0,40);
               mylayer.document.close();
          }
     }
     else
     {
          window.setTimeout("deleteme();", 800);
     }
}
function returnback()
{
     return;
}

function setshow()
{
     showlist = true
}
function sethide()
{
     showlist = false;
}
</script>

</HEAD>
<BODY >
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<table id="tbl" align="left" border="1" >
     <tr valign="top" align="left">
         
               <td><a href="javascript:listme(0,25)" onmouseover="javascript:listme(0,25)">Menu 1</a></td>
               <td><a href="javascript:listme(1,90)" onmouseover="javascript:listme(1,90)">Menu 2</a></td>
               <td><a href="javascript:listme(2,150)" onmouseover="javascript:listme(2,150)">Menu 3</a></td>
               <td><a href="javascript:listme(3,225)" onmouseover="javascript:listme(3,225)">Menu 4</a></td>

     </tr>
</table><br>
<div id="list" style="position:absolute;width:150" onmouseover="setshow()" onmouseout="sethide()"></div>

<script language="javascript">
if(document.layers)
{
     mylayer = eval(document.list);
     mylayer.captureEvents(Event.MOUSEOUT | Event.MOUSEOVER)
     mylayer.onmouseout = sethide;
     mylayer.onmouseover = setshow;
}
</script>
</body>
</html>

0
robjay
Asked:
robjay
  • 4
  • 3
  • 2
1 Solution
 
seanpowellCommented:
Well, the easy fix is just to set the position on the div to relative instead of absolute, however:

1. Any content "below" the div will jump on the screen as the div becomes visible, because relative does take up document space.

2. "width:150"   Be careful with these - all values need a measurement attached (150px) in browsers other than IE...

So, depending on you feel about # 1 will determine the outcome of the as yet unposted #3 :-)

Thanks,
Sean

0
 
COBOLdinosaurCommented:
That is really ancient code.  Do you realize that it will work only with IE and Netscrap4.  Mozilla based browsers do not recognize document.all or document.layers.  Your script is at least 5 years behind the browser technologes in use today.

Cd&
0
 
robjayAuthor Commented:
OK COBOLdino, I'm not in this full time - give me a direction to go in to get some better code.  I did have this accomplished using a Fireworks created drop down only to find out the .js file was 20 pages long.  I would like a drop down menu that appears on mouseover without 1000 lines of cryptic code doing magic for me in the background.

. . . and sean
When I change this part to relative- my drop down appears about 240 pixels away to the right  - not right below:
<div id="list" style="position:relative;width:150" onmouseover="setshow()" onmouseout="sethide()"></div>
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
seanpowellCommented:
The 240 pixels is because your javascript was telling it to do that...

No matter, if you want state of the art code for creating a menu, I've posted an example for you.

http://www.pdgmedia.com/code/menus/cssmenu_full.html

Thanks,
Sean
0
 
COBOLdinosaurCommented:
Its not complex for rudimentary modernization.  you just need to use the document.getElementById() method for modern browsers.

If you duplicate the functions using document.all.list and use document.getElementById('list) the rest of the object references should work.  The only other change would be to add a unit of measure to the style settings.  Example:

          if(document.document.getElementById)
          {
               document.getElementById('list').innerHTML = ''
               document.getElementById('list').style.top = 40+'px';
               document.getElementById('list').style.left = 0+'px';
          }

Probably not perfect, but just adding a couple of additional functions to use the DOM, will increase the cross-browser support.

If it was part of a larger migration to modern support, I would probably get rid of the NS4 stuff unless the server logs show visitors are actually using it. As long as it is doing the job you need done, there is no reason to fix what is not broken.

Cd&
0
 
robjayAuthor Commented:
Sean,
Looks like some code that will work for me.  I'm a little uncertain how to position the drop down always to the right.  If I add a longer text line, it defaults to a drop down directly below - what I would like is to the right always.


<ul id="nav">
      <li><a href="#"><nobr>Menu Link Menu Link </nobr></a>
0
 
seanpowellCommented:
Well, if I understand correctly, just add the following code to have the drop-down's display horizontally:

#nav li ul
{
      position: absolute;
      left: -999em;

      /* set to the longest width */
      width:600px;
}

#nav li ul li
{
      display:inline;
}

#nav li ul li a
{
      float:left;
}

Thanks,
Sean
0
 
seanpowellCommented:
If you need the "entire" menu to sit to the right, then you need something along these lines:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CSS/JS Driven Menu System</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css" media="screen" />
body
{
      margin:50px;
      background-color:#ffffff;
      font:12px verdana;
}

#nav, #nav ul
{
      float: left;
      padding: 0;
      margin: 0;
      list-style: none;
      position:relative;
}

/* now lets get rid of position:relative for IE */

* #nav, * #nav ul
{
      float: left;
      padding: 0;
      margin: 0;
      list-style: none;
}


#nav a
{
      width: 105px;
      display: block;
      color: #333333;
      text-decoration: none;
      background-color: #ffcc66;
      border: 1px solid #ffffff;
      padding: 4px;
      font:12px verdana; /* N6 needs this to cascasde the font style */
}

/* BMH for IE 5 */

* html #nav a
{
      width: 115px;
      w\idth: 105px;
}
      
#nav li
{
      width: 115px;
      position: relative;
}
      
#nav li ul
{
      position: absolute;
      left: -999em;
}

#nav li a:hover
{
      color: #000000;
      background-color: #CCCC99;
}

#nav li:hover ul, #nav li.sfhover ul
{
      top: 0px;
      left: 115px;
}
</style>
<script type="text/javascript">
<!--
function sfHover()
{
      var sfEls = document.getElementById("nav").getElementsByTagName("li");
      for (var i=0; i<sfEls.length; i++)
      {
            sfEls[i].onmouseover=function()
            {
                  this.className+=" sfhover";
            }
            sfEls[i].onmouseout=function()
            {
                  this.className=this.className.replace(new RegExp("sfhover\\b"), "");
            }
      }
}
//-->
</script>
</head>

<body onload="sfHover();">

<ul id="nav">
      <li><a href="#">Menu Link</a>
      <ul>
                <li><a href="#">Sub Menu Link</a></li>
                <li><a href="#">Sub Menu Link</a></li>
                <li><a href="#">Sub Menu Link</a></li>
      </ul></li>
</ul>

</body>
</html>
0
 
robjayAuthor Commented:
Excellent Answer - Please tell me
1.  Good reference that covers css positioning, i.e. this example
2.  Have you embraced CSS totally and are now shunning tables in web page design?
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now