Link to home
Start Free TrialLog in
Avatar of robjay
robjay

asked on

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>

Avatar of seanpowell
seanpowell
Flag of Canada image

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

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&
Avatar of robjay
robjay

ASKER

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>
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
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&
Avatar of robjay

ASKER

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>
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
ASKER CERTIFIED SOLUTION
Avatar of seanpowell
seanpowell
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robjay

ASKER

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?