Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery mouseover and mouseleave drop down menus

The following displays a menu:

<a href="#" id="cityclick">ONZE WAARDEN</a>

<div id="citydrop">
    <div class="dropbottom">
        <div class="dropmid">
            <ul>
                <li><a href="#">FAQ</a>
                </li>
                <li><a href="#">ITC</a>
                </li>
                <li><a href="#">CLUB</a>
                </li>
                <li><a href="#">CULTUUR</a>
                </li>
                <li><a href="#">ROBITICA</a>
                </li>
            </ul>
        </div>
    </div>
</div>

The jQuery for is as follows :

$("#citydrop").hide();

$("#cityclick").mouseover(function () {
    $("#citydrop").slideDown('slow');
});

$("#citydrop").mouseleave(function () {
    $("#citydrop").slideUp('slow');
});

PROBLEM: I'm not able to figure out how to toggle the submenu "citydrop" when the mouse leaves "cityclick" but does not enter "citydrop" yet.

I tried out here jsfiddle

like image 499
dora Avatar asked May 13 '13 09:05

dora


2 Answers

Use some wrapper on your html, and then call mouseleave event on it, like there: http://jsfiddle.net/9yEHV/11/

$("#wrapper").mouseleave(function () {
    $("#citydrop").slideUp('slow');
});

And wrapper:

<div id="wrapper">
    <a href="#" id="cityclick" >ONZE WAARDEN</a>
    <!-- rest of your code -->
</div>
like image 110
Kasyx Avatar answered Sep 27 '22 18:09

Kasyx


Is this what you are trying to do?

HTML

<ul>
<li class="main-menu">
  ONZE WAARDEN
  <ul class="sub-menu" style="display: none;">
    <li><a href="#">FAQ</a></li>
    <li><a href="#">ITC</a></li>
    <li><a href="#">CLUB</a></li>
    <li><a href="#">CULTUUR</a></li>
    <li><a href="#">ROBITICA</a></li>
  </ul>
</li>
</ul>

Javascript

$('.main-menu').mouseenter(function(){
  $(this).find('.sub-menu').slideDown();
});

$('.main-menu').mouseleave(function(){
  $(this).find('.sub-menu').slideUp();
});
like image 32
Tan Nguyen Avatar answered Sep 27 '22 20:09

Tan Nguyen