I have a small problem with the jQuery functions slideUp and slideDown.
I have a vertical menu with underneath it a vertical submenu.
When the user enter a menu item which has a submenu it must show it, This is the part which is working.
When the user leaves the menu item and goes to the submenu item it still needs to be visible. This problem i tried to catch with a setTimeout function that will remove (slideUp) the submenu after 1500ms. If the user moves his mouse to the submenu in that time the setTimeout will be cleared (clearTimeout)
But when the user moves his mouse fast over the main menu items all the submenu's will be shown and the content from the orginal page will be pushed down.
Image: http://i43.tinypic.com/5ww8yq.png
This is when i moved my mouse very fast over all the main menu items.
How it should be: When a user moves his mouse to a main menu item, the submenu must be shown. When he moves to a other main menu item the current submenu must be invisible and the other submenu must be shown.
#menu
{
background-color: white;
width: 1000px;
margin-top: 10px;
height: 30px;
position: relative;
}
#menu ul li
{
float: left;
display: inline;
width: 125px;
height: 30px;
line-height: 30px;
text-align: center;
}
#menu ul li.right { float: right; }
#menu ul li a
{
top: 5px !important;
text-decoration: none;
font-size: 20px;
height: 30px;
color: #01224D;
}
.submenu
{
background-color: #01224D;
width: 1000px;
height: 30px;
color: white;
display: none;
}
.submenu ul li
{
display: inline;
width: 100px;
height: 30px;
line-height: 30px;
float: left;
text-align: center;
}
.submenu ul li.right { float: right; }
.submenu ul li a
{
text-decoration: none;
font-size: 20px;
height: 30px;
color: white;
}
HTML:
<div id="menu">
<ul>
<li><a href="index.php">Home</a></li>
<li>
<a class="mainMenuA" id="menu-1" href="#">Lederwaren</a>
</li>
<li><a class="mainMenuA" id="menu-2" href="#">Tassen</a></li>
<li><a class="mainMenuA" id="menu-3" href="#">Koffers</a></li>
<li><a class="mainMenuA" id="menu-4" href="#">Kleding</a></li>
<li><a class="mainMenuA" id="menu-5" href="#">Accessoires</a></li>
<li class="right"><a href="vestigingen.php">Vestigingen</a></li>
</ul>
</div><!-- menu-->
<div class="submenu" id='submenu-1'>
<ul>
<li><a href="#">Submenu</a></li>
</ul>
</div><!-- submenu -->
<div class="submenu" id='submenu-2'>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</div><!-- submenu -->
<div class="submenu" id='submenu-3'>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</div><!-- submenu -->
<div class="submenu" id='submenu-4'>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</div><!-- submenu -->
<div class="submenu" id='submenu-5'>
<ul>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
<li><a href="#">Submenu</a></li>
</ul>
</div><!-- submenu -->
JS:
$(document).ready(function(){
var timer;
var hover;
$('.mainMenuA').hover(
function()
{
var id = $(this).attr('id').split('-')[1];
$('#submenu-'+id).slideDown();
},
function()
{
var id = $(this).attr('id').split('-')[1];
timer = setTimeout(function() { $('#submenu-'+id).slideUp(); },1500);
}
);
$('.submenu').hover(
function()
{
clearTimeout(timer);
},
function()
{
var id = $(this).attr('id');
timer = setTimeout(function() { $('#'+id).slideUp(); },1500);
}
);
});
I hope someone can help me.
Instead of using timer use stop
function to stop the current animtion
e.g:
$('.mainMenuA').hover(function(){
var id = $(this).attr('id').split('-')[1];
$('#submenu-'+id).stop().slideDown();
},function(){
var id = $(this).attr('id').split('-')[1];
$('#submenu-'+id).stop().slideUp();
});
Use the parameters of stop
if needed for best result.