Create nice animation on your ASP.NET Menu control using jQuery

In this blog post, I will show how you can apply some nice animation effects on your ASP.NET Menu control.

ASP.NET Menu control offers many possibilities, but together with jQuery, you can make very rich, interactive menu accompanied with animations and effects.

Lets start with an example:

- Create new ASP.NET Web Application and give it a name

- Open your Default.aspx page (or any other .aspx page where you will create the menu)

- Our page ASPX code is:

<form id="form1" runat="server">
<div id="menu">
    <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" RenderingMode="List">            
        <Items>
            <asp:MenuItem NavigateUrl="~/Default.aspx" ImageUrl="~/Images/Home.png" Text="Home" Value="Home"  />
            <asp:MenuItem NavigateUrl="~/About.aspx" ImageUrl="~/Images/Friends.png" Text="About Us" Value="AboutUs" />
            <asp:MenuItem NavigateUrl="~/Products.aspx" ImageUrl="~/Images/Box.png" Text="Products" Value="Products" />
            <asp:MenuItem NavigateUrl="~/Contact.aspx" ImageUrl="~/Images/Chat.png" Text="Contact Us" Value="ContactUs" />
        </Items>
    </asp:Menu>
</div>
</form>

As you can see, we have ASP.NET Menu with Horizontal orientation and RenderMode=”List”. It has four Menu Items where for each I have specified NavigateUrl, ImageUrl, Text and Value properties.

All images are in Images folder in the root directory of this web application. The images I’m using for this demo are from Free Web Icons.

- Next, lets create CSS for the LI and A tags (place this code inside head tag)

<style type="text/css">
    li
    {
        border:1px solid black;
        padding:20px 20px 20px 20px;
        width:110px;
        background-color:Gray;
        color:White;
        cursor:pointer;
    }
    a { color:White; font-family:Tahoma; }
</style>

This is nothing very important and you can change the style as you want.

- Now, lets reference the jQuery core library directly from Microsoft CDN.

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js"></script>

- And we get to the most interesting part, applying the animations with jQuery

Before we move on writing jQuery code, lets see what is the HTML code that our ASP.NET Menu control generates in the client browser.

 

<ul class="level1">
    <li><a class="level1" href="Default.aspx"><img src="Images/Home.png" alt="" title="" class="icon" />Home</a></li>
    <li><a class="level1" href="About.aspx"><img src="Images/Friends.png" alt="" title="" class="icon" />About Us</a></li>
    <li><a class="level1" href="Products.aspx"><img src="Images/Box.png" alt="" title="" class="icon" />Products</a></li>
    <li><a class="level1" href="Contact.aspx"><img src="Images/Chat.png" alt="" title="" class="icon" />Contact Us</a></li>
</ul>

 

So, it generates unordered list which has class level1 and for each item creates li element with an anchor with image + menu text inside it.

If we want to access the list element only from our menu (not other list element sin the page), we need to use the following jQuery selector: “ul.level1 li”, which will find all li elements which have parent element ul with class level1.

Hence, the jQuery code is:

 

<script type="text/javascript">
    $(function () {
        $("ul.level1 li").hover(function () {
            $(this).stop().animate({ opacity: 0.7, width: "170px" }, "slow");
        }, function () {
            $(this).stop().animate({ opacity: 1, width: "110px" }, "slow");
        });
    });
</script>

 

I’m using hover, so that the animation will occur once we go over the menu item. The two different functions are one for the over, the other for the out effect.

The following line

$(this).stop().animate({ opacity: 0.7, width: "170px" }, "slow");
    does the real job. So, this will first stop any previous animations (if any) that are in progress and will animate the menu item by giving to it opacity of 0.7 and changing the width to 170px (the default width is 110px as in the defined CSS style for li tag). This happens on mouse over. The second function on mouse out reverts the opacity and width properties to the default ones. The last parameter “slow” is the speed of the animation.

The end result is:

The complete ASPX code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET Menu + jQuery</title>
    <style type="text/css">
        li
        {
            border:1px solid black;
            padding:20px 20px 20px 20px;
            width:110px;
            background-color:Gray;
            color:White;
            cursor:pointer;
        }
        a { color:White; font-family:Tahoma; }
    </style>

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js"></script>

    <script type="text/javascript">
        $(function () {
            $("ul.level1 li").hover(function () {
                $(this).stop().animate({ opacity: 0.7, width: "170px" }, "slow");
            }, function () {
                $(this).stop().animate({ opacity: 1, width: "110px" }, "slow");
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div id="menu">
        <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" RenderingMode="List">            
            <Items>
                <asp:MenuItem NavigateUrl="~/Default.aspx" ImageUrl="~/Images/Home.png" Text="Home" Value="Home"  />
                <asp:MenuItem NavigateUrl="~/About.aspx" ImageUrl="~/Images/Friends.png" Text="About Us" Value="AboutUs" />
                <asp:MenuItem NavigateUrl="~/Products.aspx" ImageUrl="~/Images/Box.png" Text="Products" Value="Products" />
                <asp:MenuItem NavigateUrl="~/Contact.aspx" ImageUrl="~/Images/Chat.png" Text="Contact Us" Value="ContactUs" />
            </Items>
        </asp:Menu>
    </div>
    </form>
</body>
</html>

Hope this was useful.

Regards,
Hajan

16 Comments

  • Nice post. thanks for sharing

  • I have a doubt that you used--- $("ul.level1 li").hover(function (). where as in your complete aspx code block there is no unordered list and level1 class. so please let me know how could I achieve the one you create. It really looks good but I cant create it in my project.

  • For me the menu control is being rendered as Table even if i give RenderingMode="List"...Any ideas?

  • @anooj, please read my comment right above yours ;). If you are using .NET 3.5 or earlier version, the RenderingMode attribute does not exists as a part of the menu control.

    Hope this helps.

  • Would you please post code for multi-level version.

    I tried:

    $("ul.level1 li, ul.level2 li").hover(function () {
    $(this).stop().animate({ opacity: 0.7, width: "170px" }, "slow");
    }, function () {
    $(this).stop().animate({ opacity: 1, width: "110px" }, "slow");
    });

    inside jquery ready function.

    This works initially but after a while level2 items don't display (in Firefox 3.6) - Don't know why? - Thanks

  • @john77, let me test your solution and I will get back to you with response.

    Thanks for your comment.

  • can you please help me change color for animate .hover function.
    I tried it but it didn't work.

  • gr8 work....thanks...:)

  • i tried ur code.But script fuctions not working.Only menu control and images are worked. Any Ideas?

  • priya & Pinky, did you try the same code I used or you have made your own modifications?

  • Hi, I used your code, it worked great, but i have a problem, I use it in a principal menu, and when you go into another page and then you want to ho back using a buttom, appeara an error saying this that Microsoft JScript: '$' is not definied, how can i fix this problem? thanks

  • For Sub menus this not work properly....any ideas?...........

  • Wooper, I am not sure that problem is caused by the menu script. Actually, when you get such error probably the script library may not be loaded correctly, so please check that first...

  • Dipak, the script is not initially intendend for submenu items, but if you will put a little more effort, you can make it work... still, I think its odd to add submenus if you have such type of animation (animating left -> right slide and vice versa)

  • Thier is one problam.. If select any Item .. That Efeect Shouldn't be applied... I mean when Ever The Page is loaded that jQuery effect is not applied.

  • Thank you so mich..........

Comments have been disabled for this content.