2

I tryd to made a Jquery dropdown menu when i slide the menu item it slides perfecly down and when i leave the menu item it slides perfecly up.

The only problem is when i move my mouse down into the submenu it slides up and what i want is that is stays open untill i leave the menu item or the submenu.

HTML

<ul>
    <a href="dashboard.php"><li class="dashboard icon-home">Dashboard</li></a>
    <a href="#"><li class="icon-pages">Paginas</li></a>
    <a class="trigger" href="#"><li class="afbeeldingen icon-image">Afbeeldingen</li></a>
    <div class="adminsubmenu">
        <a href="#">Submenu item</a>
        <a href="#">Submenu item</a>
        <a href="#">Submenu item</a>
    </div>
    <a href="#"><li class="icon-users">Gebruikers</li></a>
    <a href="#"><li class="icon-settings">Instellingen</li></a>
</ul>

CSS

div#adminmenu ul li {
    font-size:13px;
    font-weight:600;
    padding:7px 0 7px 28px;
    background-color:#fff;
    width:175px;
    border-bottom:1px solid #c2c2c2;
    border-right:1px solid #c2c2c2;
}

div.adminsubmenu {
    height:100px;
    width:175px;
    background:url(../images/adminmenu_bg.png) repeat-y top right #e6e6e6;
}

Jquery

$(document).ready(function(){

            $(".adminsubmenu").hide();

            $("a.trigger").mouseover(function(){
                $(".adminsubmenu").slideDown();
            }).mouseleave(function(){
                $(".adminsubmenu").slideUp();
            });

        });

And here is the Fiddle

3 Answers 3

3

You ought to put the <a> inside of the <li>, not the other way around, in order to make the HTML valid.

That said, just change your JavaScript to do exactly what you want:

$("a.trigger").mouseover(function(){
            $(".adminsubmenu").slideDown();
        });
$('.adminsubmenu').mouseleave(function(){
            $(".adminsubmenu").slideUp();
        });

Better yet, place the submenu inside the trigger element. That way, you'll still be hovering over the trigger as long as your mouse is over the submenu:

<li class="afbeeldingen icon-image trigger">
    <a class="" href="#">Afbeeldingen</a>
    <ul class="adminsubmenu">
        <li><a href="#">Submenu item</a></li>
        <li><a href="#">Submenu item</a></li>
        <li><a href="#">Submenu item</a></li>
    </ul>
</li>

new JS:

$(".trigger").mouseover(function () {
    $(".adminsubmenu").stop().slideDown();
}).mouseleave(function () {
    $(".adminsubmenu").stop().slideUp();
});

http://jsfiddle.net/mblase75/B3GB6/

Sign up to request clarification or add additional context in comments.

Comments

1
<li><a ... </a></li>

INSTEAD OF

<a><li> </li></a>

It's actually the other way round

Comments

0

It was doing exactly what you said. When the mouse leaves a.trigger the menu slides up. So you just have to change it to $(".adminsubmenu").mouseleave Here is what you want:

http://jsfiddle.net/2Ajuz/1/

$(document).ready(function(){

    $(".adminsubmenu").hide();

    $("a.trigger").mouseover(function(){
        $(".adminsubmenu").slideDown();
    })
    $(".adminsubmenu").mouseleave(function(){
        $(".adminsubmenu").slideUp();
    });

});

1 Comment

Yes that is almost good but it doesn't work if you mouseleave the class trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.