3

I just developed a single level dropdown menu using jquery... How to refactor the same code to make it a multilevel dropdown menu...

Here is my working sample....

The jquery what i used,

$(document).ready(function() {
$("#Programming ul.child").removeClass("child");
$("#Programming li").has("ul").hover(function() {
   $(this).addClass("current").children("ul").fadeIn();
     }, function() {
    $(this).removeClass("current").children("ul").stop(true, true).css("display",
                                                                     "none");
                              });
                            });
​

EDIT:

Menu1       Menu2
 SubMenu1    SubMenu1
 Submenu2    SubMenu2
    SubMenu21       
    SubMenu22

The second submenu level can be anywere...

1
  • Where do you want the multi-level ness to appear? Commented Jul 28, 2010 at 1:57

3 Answers 3

3
+50

As your code is very generic about hiding/showing the child of the list, all I did was nested another UL inside an LI element, and then positioned it according to it's parent:

http://jsbin.com/oteze/5

(No JS changes, just a new line of CSS targeting Third Level Menu items)

#Programming li ul li ul { left:100px;top:0px; }
Sign up to request clarification or add additional context in comments.

Comments

1

Here is my solution: http://jsbin.com/oteze/9 I tested it on Firefox 3.6.8. ADD: Now it works in IE7 too.

You can nest any number of submenus anywhere. Just like that:

<li><a href="#">Child 1.2</a>
   <ul class="child">
       <li><a href="#">Child 1.2.1</a></li>
       <li><a href="#">Child 1.2.2</a></li>
   </ul>                                                      
</li>

I modified your code a little, so there is difference between first level submenu and other levels. I also moved all show/hide logic to JS.

   $(document).ready(function() {

        $("#Programming ul.child-first-level").hide();
        $("#Programming ul.child").hide();

        $("#Programming ul.child").each(function(index) {
            $(this).css("margin-left", $(this).parent().css("width"));
        });


        $("#Programming li ul.child-first-level").parent().hover(
            function() {
                $(this).children("ul").fadeIn();
            },
            function() {
                $(this).children("ul").stop(true, true).css("display", "none");
            }
        );

        $("#Programming li ul.child").parent().hover(
            function() {
                var offset = $(this).children("ul").offset();
                offset.top = 0;
                $(this).children("ul").offset(offset);
                $(this).children("ul").css("margin-left", $(this).parent().css("width"));
                $(this).children("ul").fadeIn();
            },
            function() {
                $(this).children("ul").stop(true, true).css("display", "none");
            }
        );

   });

1 Comment

Yep, IE doesn't handle offsets well. So I modified code a little. Now it works in IE 7: jsbin.com/oteze/9
1

Is there any reason why you wouldn't want to use the existing jquery plugin dedicated to this?

A good one is: http://users.tpg.com.au/j_birch/plugins/superfish/

Suckerfish has been around for years as the pure JS version that's about as solid as you can get.

Even if you don't use this I'm sure it'd be helpful to check out his source.

1 Comment

i just tried a menu without any plugin... So i did it manually but i couldn't convert it to a multilevel menu...

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.