Solved

relative positioning when creating a mouseover dynamic drop down table

Posted on 2004-08-30
9
696 Views
Last Modified: 2008-03-03
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
Comment
Question by:robjay
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

by:seanpowell
ID: 11934213
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 11934527
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
 

Author Comment

by:robjay
ID: 11934777
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 31

Expert Comment

by:seanpowell
ID: 11934928
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 11935500
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
 

Author Comment

by:robjay
ID: 11936758
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
 
LVL 31

Expert Comment

by:seanpowell
ID: 11938493
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
 
LVL 31

Accepted Solution

by:
seanpowell earned 300 total points
ID: 11941970
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
 

Author Comment

by:robjay
ID: 11942438
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unordered lists (HTML ul tags) are very handy to create menus of all types, shapes and sizes.  All it takes is a little knowledge of CSS and the possibilities are endless. First off, you should know the general format for menus created using the …
When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question