HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery

17. November 2010

Today I was playing around with using jQuery to create a drop down menu. I thought I’d share an example of adding a drop down menu to the new ASP.NET MVC HTML5 Template that I created on Day 2.

Before we begin, you’ll need to create a new site using the code from Day 2. You can either copy/paste the code from the article, or download the full source code. You’ll find the download link at the bottom of the post for Day 2.

Here’s a screenshot of the finished dropdown menu:

ASPNETMVCHTML5DropDownMenu_IE8

Here’s another screenshot of the menu in Chrome:

ASPNETMVCHTML5DropDownMenu_Chrome

Add Sub-Menu Items

First, modify the <nav> item in the Site.Master file to include sub-menu items as <ul> child elements added to the “nav ul” element. Below is an example:

<nav>
    <ul>
        <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
        <li>
            <a href="#">Customers</a>
            <ul>
                <li><a href="#">Create New</a></li>
                <li><a href="#">Latest</a></li>
                <li><a href="#">View All</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Orders</a>
            <ul>
                <li><a href="#">Create New</a></li>
                <li><a href="#">Latest: Last 30 Days</a></li>
                <li><a href="#">Search: Date Range</a></li>
                <li><a href="#">Search: Customer</a></li>
                <li><a href="#">View All</a></li>
            </ul>
        </li>
        <li><%: Html.ActionLink("About", "About", "Home")%></li>
    </ul>
</nav>

Of course don’t forget to replace the “<a href” tags with “Html.ActionLink” calls to wire up the Views in your application. For this article, we’ll just leave the code like this.

Add CSS Styles to the Sub-Menus

We do not need to modify the styles in the “Site.css” file for this. We just need to add some CSS to style the sub-menus appropriately.

Here’s the minimum styles necessary for the sub-menu to display correctly:

<style>
body nav ul ul {
    display: none;
    position:absolute;
}
</style>

Even though the above style will make the sub-menus display correctly (hide on page load, and absolutely positioned), we still need to style them to match the overall style of the ASP.NET MVC site template.

Here’s the full CSS necessary to style the sub-menus in the new ASP.NET MVC HTML5 Template:

<style>
body nav ul ul {
    display: none;
    position:absolute;
    width: 100%;
    
    background: #5C87B2;
    border-top: 5px solid #5C87B2;;
    filter:alpha(opacity=80);
    -moz-opacity:0.8;
    -khtml-opacity: 0.8;
    opacity: 0.8;
}
body nav ul ul li a{
    line-height: 3.1em;
}
</style>

This CSS colors the background of the sub-menu area the same color blue as the page, but it also makes it semi-transparent. So any content behind the sub-menu will show through just a little bit.

Let’s wire things up with jQuery

Now we just need a little jQuery code to wire up our sub-menus to display when the use hovers the mouse over their parents. The jQuery code I’m using for this was inspired by Dan Wellman’s “How to Create a Drop-down Nav Menu with HTML5, CSS3 and jQuery” post.

$(function () {
    $("body nav li").each(function () {
        if ($(this).find("ul").length > 0) {

            //show subnav on hover  
            $(this).mouseenter(function () {
                $(this).find("ul").stop(true, true).slideDown();
            });

            //hide submenus on exit  
            $(this).mouseleave(function () {
                $(this).find("ul").stop(true, true).slideUp();
            });

            $(this).find("ul").mousemove(function () {
                $(this).stop(true, true).show();
            });
        }
    });
});

As you can see, with jQuery the javascript code necessary to wire this up is rather short. Oh, and did I mention that the sub-menus slide down into place?

Conclusion

I know this post isn’t so much HTML5 as it is jQuery and CSS. But, after all, isn’t HTML5 really about the same thing as jQuery? RIA.

I know, yesterday, I said that today I would move on to adding some “patch” code to add some HTML5 support to older browsers. I decided to take a detour back to the ASP.NET MVC HTML5 template. We shall see, again, where tomorrow takes us.

HTML, ASP.NET MVC, JavaScript , , , , , , ,

Comments

12/4/2010 11:16:29 AM #
I may share this info! THX
12/8/2010 2:53:34 AM #
Is your country full of football fans?
12/8/2010 7:24:06 AM #
you have a great blog here! would you like to make some invite posts on my blog?
12/9/2010 1:20:37 AM #
Choose Glamest  http://www.cnfibcs.com   for Tour guides
12/11/2010 2:43:26 AM #
Hi everyone I considered the post , good information, Can we fix the RSS feeder. . .?  I can't get it from the browser.  Well, keep up the good work.

http://www.cataclysmcheats.info - Warcraft Cataclysm Super Zygor Cheat!!
12/12/2010 4:09:44 PM #
Thank you very much for this great article. The best wishes for you Smile
12/12/2010 6:31:23 PM #
I acknowledge or in it commenters above rocking blog post I must add
12/12/2010 8:29:18 PM #
A very interesting article, giving me a lot of inspiration and ways to overcome my problem. with writing a straightforward, simple and easy to understand I really helped after reading this article. Thank you.
12/12/2010 10:17:07 PM #
Thanks for another  groovy   office  on this program . Love the updates,  hold them coming. <a href=http://www.tiffanyloving.com>Tiffanys racelet</a>
12/12/2010 10:30:50 PM #
Awesome Post!  I'm glad I stumbled onto this!  This post was very useful. Thanks for Posting!
12/12/2010 11:28:10 PM #
Great piece of writing, l quite agree with your submission. By the way l love your template nice and clean.
12/13/2010 1:33:32 AM #
Obesity in any form is not to be admired. There are many people who have explored this field over many years with the statistical issues being one of western standards debasement namely food eating habits and corporate avarice over customer wellbeing
12/13/2010 3:51:30 AM #
Excellent blog! I actually love how it is easy on my eyes and also the data are well written. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!
12/13/2010 4:19:40 AM #
Your site is very informative and helpful. This should be bookmarked by anyone who is looking for such info. Your site has been mentioned in one of the forums to visit and check out.
12/13/2010 5:08:13 AM #
I am writing a report on this subject and your post is very helpful and informative. Thank you  

Regards!


12/13/2010 6:29:50 AM #
lol a couple of the reviews bloggers write are silly and unrelated, there are times i wonder whether they at all read the post before writing or whether they just look at the subject of the blog post and compose the very first thought that drifts to their minds. But it is great to see a intelligent commentary every now and then in contrast to the exact same, traditional blog bull which I too many times notice on the blogs. Regards PokemonCraft Admin.
12/13/2010 7:32:46 AM #
Sweet post, many thanks. I've been intending on doing a post along these lines for months, do you mind if I quote you? I will link back to you naturally.
12/13/2010 9:07:02 AM #
Hey there!  I just want to tell u that I found your website enough great to me.  Usefull thoughts and all is nice arranged. Thank you for your work. I will visit your site more ofter from now and I bookmarked it. You can also visit my <a href="www.24hrcrimescenecleaners.com">crime scene cleanup</a> site if you have a time.
12/13/2010 10:01:26 AM #
out some more later ..
12/13/2010 10:01:33 AM #
Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!
12/13/2010 12:39:26 PM #
You are my personal God! ;) Thank for this post!
12/13/2010 8:51:16 PM #
nice
12/13/2010 9:50:33 PM #
Hey - I really enjoyed the post and would like to say it was an outstanding read.
12/13/2010 10:27:49 PM #
Who admins of this web site? I'd like to trade links.  Looking at the amount of traffic that
  HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery
gets and it'd be great for me to exchange links.  It's good to get ahead of the competition and all.  And, most of my links come from places just like here.
12/13/2010 10:42:34 PM #
That is great to know, you gave me something to think about on the way house from work. Will you be writing far more about this topic? FYI I tried your rss button and it didnt work. I will try once more in a couple of hours.
12/13/2010 11:27:44 PM #
Intriguing article. I know I’m a little late in posting my comment but the article was to the point and just the information I was looking for. I can’t say that I agree with all you mentioned but it was emphatically fascinating! BTW…I found your site through a Google search. I’m a frequent visitor to your blog and will return again soon.
12/14/2010 2:11:17 AM #
excellent post I'm greatful to visit your blog