Hi everyone,
I’m new to this forum, and somewhat new to Javascript, so I’d greatly appreciate any help you can provide with my problem.
I’m working on a drop down menu for a website, and it is supposed to be a vertical dropdown menu where the submenu items appear below the menu item and thereby moving the menu items further down the page. Also there should be a timing element implemented to delay the disappearing of submenu items onmouseout (about a second or two).
I’ve implemented a timing element within the JS using setTimeOut which is working fine. However the issue I’ve come across is when you move the mouse from one menu or submenu item to the next menu item, the next menu will trigger showing the submenu items but the previous submenu items will not disappear. However “logically” one would think that it’s because if the submenu items would disappear it would rearrange the location of where the current menu and submenu items would be (moving them up). However I also noticed that if you move off the 2nd triggered menu item the first menu item and submenu items would not disappear. Someone suggested using jquery and using the line $(‘ul.cssdropdown’).css(‘display’, ‘none’); to select and hide all the other ul’s before showing the new ones. I’ve tried it out and it’s not working. Here is the code before using jQuery. It’s easier to see what kind of problem I’m talking about by running this code and playing with the mouseover/out triggers yourselves.
<html>
<head>
<style type=”text/css”>
#cssdropdown li.headlink ul { display: none; }
</style>
<script language=”JavaScript”>
window.onload = function()
{
var lis = document.getElementsByTagName(‘li’);
for(i = 0; i < lis.length; i++)
{
var li = lis[i];
if (li.className == ‘headlink’)
{
var showing = 0;
li.onmouseover = function() { self = this; showing = i; setTimeout(function(){ if (showing == i) self.getElementsByTagName(‘ul’).item(0).style.display = ‘block’; }, 500); }
li.onmouseout = function() { self = this; showing = 0; setTimeout(function(){ if (showing == 0) self.getElementsByTagName(‘ul’).item(0).style.display = ‘none’; }, 1000); }
}
}
}
</script>
</head>
<body>
<ul id=”cssdropdown”>
<li class=”headlink”><a href=”menu.html” class=”headerCssClass”>MENU</a>
<ul>
<li><a href=”submenu1.html” class=”itemCssClass”>SUBMENU1</a></li>
<li><a href=”submenu2.html” class=”itemCssClass”>SUBMENU2</a></li>
<li><a href=”submenu3.html” class=”itemCssClass”>SUBMENU3</a></li>
</ul>
</li>
</ul>
<ul id=”cssdropdown”>
<li class=”headlink”><a href=”menu.html” class=”headerCssClass”>MENU</a>
<ul>
<li><a href=”submenu1.html” class=”itemCssClass”>SUBMENU1</a></li>
<li><a href=”submenu2.html” class=”itemCssClass”>SUBMENU2</a></li>
<li><a href=”submenu3.html” class=”itemCssClass”>SUBMENU3</a></li>
</ul>
</li>
</ul>
<ul id=”cssdropdown”>
<li class=”headlink”><a href=”menu.html” class=”headerCssClass”>MENU</a>
<ul>
<li><a href=”submenu1.html” class=”itemCssClass”>SUBMENU1</a></li>
<li><a href=”submenu2.html” class=”itemCssClass”>SUBMENU2</a></li>
<li><a href=”submenu3.html” class=”itemCssClass”>SUBMENU3</a></li>
</ul>
</li>
</ul>
</body>
</html>