JQuery problem of a MENU with two <ul> ’s

Asked

Viewed 143 times

2

I’m having a problem with a menu.

It turns out that when the person hovers the mouse on the link Attending, happens a dropdown, the menu appears because it is with display:none and when it passes the mouse gets display:block.

So far so good, only I’m having a problem, I need it to happen with more than one <ul>. Only at 1° <ul> is receiving this effect.

I want that when the person hovers the mouse over the link Attending, such an effect occurs in 2 or plus <ul> 'Then I’ll be able to do it like a Mega Menu.

Code of Menu | HTML:

                    <li>
                        <a href="blog.html">Atendimento</a>
                        <ul><!-- Primeiro | No caso o .first() -->
                            <li><a href="base.html">Base de Conhecimento</a></li>
                            <li><a href="blog.html">Nosso Blog</a></li>
                            <li><a href="contato.php">Fale conosco</a></li>
                            <li><a href="suporte.html">Suporte</a></li>
                        </ul>
                        <ul><!-- Segundo | Não funciona -->
                            <li><a href="base.html">Base de Conhecimento</a></li>
                            <li><a href="blog.html">Nosso Blog</a></li>
                            <li><a href="contato.php">Fale conosco</a></li>
                            <li><a href="suporte.html">Suporte</a></li>
                        </ul>
                    </li>

Code of Menu | jQuery:

jQuery("#menu > li").hover(function() {
        jQuery(this).find("ul").first().slideDown(600);
    }, function() {
        jQuery(this).find("ul").first().slideUp(200);

    });

    jQuery("#menu li ul li").hover(function() {
        jQuery(this).find("ul").first().toggle(0);

    });

    jQuery("#menu li ul li").each(function() {
        if(jQuery(this).has("ul").length > 0) {
            jQuery(this).addClass("menu-arrow")
        }
    });

I believe the problem lies in .first() = first (in Portuguese), in case only works with the first <ul>, but I need it to work with both <ul>.

Note: I tried to remove the .first(), but he left the whole menu bugged. (I’m still a beginner)

2 answers

1


According to the template you passed me by chat, your second UL is A’s child, so jquery had to be adapted:

HTML:

<ul id="menu">
    <li> <a href="blog.html">Atendimento</a>

        <ul>
            <li><a href="base.html">Base de Conhecimento</a></li>
            <li><a href="blog.html">Nosso Blog</a></li>
            <li><a href="contato.php">Fale conosco</a></li>
            <li> <a href="suporte.html">Suporte
                    <ul>
                        <li><a href="base.html">Base de Conhecimento</a></li>
                        <li><a href="blog.html">Nosso Blog</a></li>
                        <li><a href="contato.php">Fale conosco</a></li>
                        <li> <a href="suporte.html">Suporte</a></li>
                    </ul>
                </a>
            </li>
        </ul>
    </li>
</ul>

jquery:

$("#menu").find('li').hover(function () {
    $(this).children('ul').clearQueue().slideDown(600);
    $(this).children('a').children('ul').clearQueue().fadeIn(600);
}, function () {
    $(this).children('ul').slideUp(600);
    $(this).children('a').children('ul').fadeOut(600);
});

$("#menu").find('a').has( "ul" ).each(function() {
    $(this).addClass("menu-arrow");
});

That is, any LI that has a son UL and also any LI that has a son A that has a son UL, will trigger the events, showing the UL and then hiding again.

jsfiddle http://jsfiddle.net/jaderw/p83s8zL6/7/

  • Yeah, it’s just I didn’t put the whole code, just the 2 part <ul>. This code of yours has bugged my menu. It doesn’t work anymore. You also haven’t put the ex IDS: #menu li ul li.

  • Now it worked, only an error happened, the dropdown menu also has a submenu, now it does not open more.

  • @Alexandrelopes Examine the code carefully, note that the entire block of <ul> should stay inside the <li> Dad, in this scheme that I passed works on "infinite" levels.

  • I found the problem, is that after the <li> has to stay the <ul> and what I had was a <a>, you could put the IDS? To work only in the menu. The way it works anywhere on the site.

  • what element is with id #menu?

  • It’s not working right, because even the submenu is cascading.

  • There would be no way only to add the effect in more than 1 <ul> without having to touch the HTML?

  • Like the effect would also work on: #menu > li:nth-child(4) > ul:nth-child(3)

  • sorry I didn’t understand how is this your menu, if you can pass a fiddle with the full and original code would make it much easier

  • your jQuery seems to have a small bug, when I hover over the menu and then take out the mouse it gets a bug in the look of the site. Watch when you hover: http://s2.postimg.org/c5bj4m1nd/Screenshot.png. and when you hover: http://s2.postimg.org/ilkhupa6x/Screenshot2.png

  • Note: Using Google Chrome

  • Will be if the problem is only in the Browser?

  • 1

    Ta with way to be some bug in the rendering of the browser, because the script only makes show and hide with Hover, and if it was the script the whole object would be visible... try to change the effects of fadein to show and fadeout to Hide see if it changes anything.

  • Yes, you did! @Jader edit your answer. It was 100% here. I wasn’t even using the fade anyway, I even put (0) in the fade to not show up. : D

  • So maybe that was the problem (zero in the fade)...

  • Hihihihi, I don’t know, I just know that now: .show(0) and .hide(0)

  • Do I have to remove? or leave with (0) same ?

Show 13 more comments

1

My suggestion is to do this only with CSS.

ul  ul {
    display: none;
}

ul > li:hover ul {
    display: block;
}

Example: http://jsfiddle.net/n6ty5jut/

I could even do with CSS animation:

ul  ul {
    opacity: 0;
    transition: opacity 1s;
}

ul > li:hover ul {
    opacity: 1;
}

Example: http://jsfiddle.net/n6ty5jut/1/

  • Wow! It got interesting...

  • Not only does it solve with CSS, it has some effects...

  • @Alexandrelopes which ones? what you lack?

  • It’s just that there was a submenu that wasn’t working. And I needed jQuery to add a class menu-arrow, just that it worked with @Jader, but thank you so much @Sergio for always helping me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.