2

I want to design a multilevel menu using jQuery. I have already written some code .You can see the demo here . All this works fine. But I want to make multilevel drop down menus dynamically.

Script

$('ul#menu > li').hover(function(){
    //$('#drop' , this).css('display','block');
     $('.drop' , this).delay(20).slideDown(200);
}, function(){
 $('.drop' , this).delay(20).slideUp(200);
});​

HTML

<ul id="menu">
    <li><a href="#">Home</a>
        <ul class="drop">
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
        </ul>
    </li>


    <li><a href="#">about</a>
        <ul class="drop">
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </li>

</ul>​

CSS

ul#menu
{
    margin:0;
    padding:0;
}
ul#menu > li
{
    list-style:none;
    float:left;
    margin:0;
    padding:0;
    position:relative;
}
ul#menu a
{
    text-decoration:none;
    color:#fff;
    background:red;
    display:block;
    padding:10px;
}
ul#menu > li ul.drop
{
    margin:0;
    padding:0;
    width:150px;
    position:absolute;
    display:none;
}
ul#menu > li ul.drop ul
{
    margin:0;
    padding:0;
    width:150px;
    position:absolute;
    display:none;
    left:150px;
    top:0;
}
ul#menu > li ul li
{
    margin:0;
    padding:0;
    list-style:none;
    position:relative;
}​

3 Answers 3

5

You need to change the jQuery script a little to accommodate for the multilevel menus like this:

$('ul#menu li').hover(function(){
     $(this).children('ul').delay(20).slideDown(200);
}, function(){
     $(this).children('ul').delay(20).slideUp(200);
});

and change html like this:

<ul id="menu">
    <li><a href="#">Home</a>
        <ul class="drop">
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li>
                <a href="#">About us</a>
                <ul>
                    <li><a href="#">Sub Item 1</a></li>
                    <li>
                        <a href="#">Sub Item 2</a>
                        <ul>
                            <li><a href="#">Sub item 3</a></li>
                            <li><a href="#">Sub item 4</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>


    <li><a href="#">about</a>
        <ul class="drop">
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </li>
</ul>

Your css is fine. You can check the updated multilevel code at: http://jsfiddle.net/297t6/

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

Comments

0
$(document).ready(function () {
            var data = [{ 'Name': 'Home', 'Url': '#', 'SubMenu': [{ 'Name': 'Home1', 'Url': '#' }, { 'Name': 'Home2', 'Url': '#' }, { 'Name': 'Home3', 'Url': '#' }, { 'Name': 'Home4', 'Url': '#' }, ] }, { 'Name': 'AboutUs', 'Url': '#', 'SubMenu': [{ 'Name': 'AboutUs1', 'Url': '#' }, { 'Name': 'AboutUs2', 'Url': '#' }, { 'Name': 'AboutUs3', 'Url': '#' }, { 'Name': 'AboutUs4', 'Url': '#' }, ]}];

            if (data.length > 0) {
                $('body').prepend('<ul id="menu"><ul>');

                $.each(data, function (i, entity) {
                    var liHtml = [];
                    liHtml.push('<li><a href="' + entity.Url + '">' + entity.Name + '</a>');
                    if (entity.SubMenu.length > 0) {
                        liHtml.push('<ul class="drop">');
                        $.each(entity.SubMenu, function (i, subEntity) {
                            liHtml.push('<li><a href="' + subEntity.Url + '">' + subEntity.Name + '</a>');
                        });
                        liHtml.push('</ul>');
                    }
                    liHtml.push('</li>');

                    $('ul#menu').append(liHtml.join(''));
                });
            }



            $('ul#menu > li').hover(function () {
                //$('#drop' , this).css('display','block');
                $('.drop', this).delay(20).slideDown(200);
            }, function () {
                $('.drop', this).delay(20).slideUp(200);
            });
        });

for live demo see this link: http://jsfiddle.net/nanoquantumtech/Z5NXv/

1 Comment

Thanks, for the above solution. But your solution works only for 2 levels of menuItems. i.e. Parent and Child. Can you say how can I have infinite level menu?
0

To Work your dropdown menu with Mouse click event also, Use this code.

    $('.getdown a.toggle').on('click', function(e) {
        $(this).next('ul').slideToggle(250);
        e.preventDefault(); // to prevent following the link
    });

and add some classes or IDs to your html like this,

<ul id="menu">
    <li class="getdown">
        <a href="#" class="toggle">Home</a>
        <ul>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">About us</a></li>
        </ul>
    </li>


    <li class="getdown">
        <a href="#" class="toggle">about</a>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </li>

</ul>​

To Work with hover try this,

    $(".getdown").hover( function() {
        $(this).children('ul').hide();
        $(this).children('ul').slideDown(250);
    }, function() {
        $('ul', this).slideUp(250);
    });

If you are using bootstrap in your project, don't use bootstrap classes here. User another unique classes because bootstrap can override some functionalities.

Comments

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.