Solved

relative positioning when creating a mouseover dynamic drop down table

Posted on 2004-08-30
9
695 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now