I recently stumbled upon Dragon Interactive (dragoninteractive.com). It’s a pretty well designed site. However, the pièce de résistance is their rather cool animated menu. Now… had this been designed in Adobe Flash, I wouldn’t had paid much attention. A closer inspection revealed that the menu is plain XHTML, CSS and Javascript. Today, I’m going to show you how to create an animated menu (very similar to Dragon Interactive’s menu).
Designing The Sprite
To begin with, you will need to build your design in Adobe Photoshop - or some other illustration program. You will need to design a sprite. I also quickly designed a repeating background image (just to make things look pretty):
![]()
The XHTML Markup
Here is the XHTML markup that is used in the demo:
<ul id="menu"> <li><a href="#" class="home"><span></span></a></li> <li><a href="#" class="portfolio"><span></span></a></li> </ul>
As you can see, an unordered list is used to structure the menu. In this tutorial, I’m only going to create 2 links. You can add as many links as you desire. Notice that each link is given a unique class. We will need to work with each class individually when we write the CSS.
The CSS
For the demo, I assigned the repeating image as a background image for the <ul> element. I also set the height and width:
ul#menu { width:80%; height:102px; background:url(bg.png) repeat-x; list-style:none; margin:0; padding:0; padding-top:20px; padding-left:20%; }
Notice that in the above CSS, “list-style:none;” is used to prevent styling the list using the default bullet point format. The rest is pretty self-explanatory. We will now need to float each <li> element so that the list elements are displayed horizontally rather than vertically:
ul#menu li { float:left; }
Now we get to the good stuff! We’re going to need to add the sprite as a background image to each link in the menu. However, we only want to display a section of the sprite for each link. We do this by restraining the link to some specified dimensions and then positioning the background image accordingly. Let’s look at the following sprite:
![]()
Each column represents a different link. In this case, we only have 2 columns since we only want 2 links. The bottom row defines how the link will look when you hover over it with your mouse cursor. Now, say we define the link to have a height of 81px and a width of 159px. If the background image is then positioned to the top left, we see that only the “Home” button is displayed:

This is because the “Home” button was designed such that it has a height of 81px and a width of 159px. As the background is positioned to the top left, this is the only part of the image that will fit inside the link - which is obviously when we want! So, how do we write this using CSS? Well… first you will need to recognise that in most cases, we will want more than a single link (in this case - 2 links). Hence, it is easier if we write some common CSS for all the links (i.e. CSS that may be inherited by each and every link in the menu). We can always add to (or overwrite) these common CSS attributes by later writing CSS for each specific class assigned to the link (see XHTML markup). Here is the common CSS that is used in the demo:
ul#menu li a { background:url(sprite.png) no-repeat scroll top left; display:block; height:81px; position:relative; }
In the above code, we position the background image to the top left. This is fine for the “Home” link. However, for the “Portfolio” link, we will need to overwrite the background position attribute for concerning class (see XHTML markup). Please also note that we define the height here since all the links will have the same height. As links by default are displayed inline, we must use “display:block” to specify the height of the link. Finally, we must position the link relatively. It will become apparent why we need to do this later.
We will now need to write some CSS for each individual link. We do this by specifying the class (see XHTML markup). This tutorial deals with just 2 links. The “Home” link has the class: “home”. The “Portfolio” link has the class: “portfolio”. We can hence define CSS attributes for each individual link as follows:
ul#menu li a.home { width:159px; } ul#menu li a.portfolio { width:157px; background-position:-159px 0px; }
Notice that it is here where we specify the width of the links (since each link may have a different width according to the design). It is also here that we overwrite the background position attribute if needed. We do not need to do this for the “Home” link since the common class already has this defined as “top left”.
We will want to position the sprite differently for the “Portfolio” link. Since the “Home” link is 159px wide, we will need to shift the background position 159px to the left in order to focus on the “Portfolio” area. We do this by writing “background-position:-159px 0px;”. This tells the browser to shift the background 159 pixels to the left and 0 pixels down. You can learn more about positioning background images using CSS over at SitePoint.

We now need to create the “hover” state. This is how the link will appear when you hover over it with your mouse cursor. The animation will blend between the 2 states. To create the hover state, we’re going to use a <span> tag. We’ll then use CSS to have that span tag completely fill the link (so that it is exactly the same size as the link). We’ll also need to shift the background image down for the span in order to display the “hover” area of the sprite. As before, we’ll use some common CSS:
ul#menu li a span { background:url(sprite.png) no-repeat scroll bottom left; display:block; position:absolute; top:0; left:0; height:100%; width:100%; z-index:100; }
Note that we again use “display:block;” since, by default, the <span> tag is displayed inline. Now that the <span> is displayed as a block, we can define the height and width to 100% - completely filling the link (so that the span and link are exactly the same size). Also notice that we position the <span> absolutely inside the relatively positioned link. Positioning the <span> in this way allows us to define a z-index. The assigning a high z-index ensures that the span’s background is displayed above the link’s background. All that we are left to do is position the background for the <span> tags (contained within the links):
ul#menu li a.home span { background-position:0px -81px; } ul#menu li a.portfolio span { background-position:-159px -81px; }
Notice that we shift the background down by 81 pixels for each span tag (since the height of the “Home” link is 81px). This causes the “hover” state to be displayed within each span tag. As before, we also shift the background left by 159px for the “Portfolio” link.
Using jQuery For The Animation
The animation is really quite simple. We’ll use jQuery to animate the opacity of the <span> tag. Remember that the <span> tag displays the “hover” state of the image. Hence, when the <span> is visible, the link takes the “hover” appearance. We’ll need the “hover” state to be invisible when the page loads. We can do this by setting the opacity of the <span> tag to “0″:
$(function() { // set opacity to nill on page load $("ul#menu span").css("opacity","0"); // the rest of the code will go here });
Placing the code inside of “$(function() { … });” tells the browser to execute the code when the document has loaded. The code that we’ll use to do the animation is as follows:
// on mouse over $("ul#menu span").hover(function () { // animate opacity to full $(this).stop().animate({ opacity: 1 }, 'slow'); }, // on mouse out function () { // animate opacity to nill $(this).stop().animate({ opacity: 0 }, 'slow'); });
Hence the entire Javascript used is as follows:
<!-- Include jQuery Library --> <script src="jquery-1.2.2.pack.js" type="text/javascript"></script> <!-- Let's do the animation --> <script type="text/javascript"> $(function() { // set opacity to nill on page load $("ul#menu span").css("opacity","0"); // on mouse over $("ul#menu span").hover(function () { // animate opacity to full $(this).stop().animate({ opacity: 1 }, "slow"); }, // on mouse out function () { // animate opacity to nill $(this).stop().animate({ opacity: 0 }, "slow"); }); }); </script>
Notice that we first include the jQuery library. You can learn more about jQuery animations by reading up on the documentation. One final addition to the CSS is required to ensure that the mouse cursor is display as a pointer when hovering over the <span> tag:
ul#menu li a span:hover { cursor:pointer; }
The full source code can be accessed via the demonstration page. I should just add that whilst this code seems to work well with FireFox and IE7, IE6 might (and probably will) be a different story.

July 3, 2008
Quote
Wow! I’m “googling” for “jquery menus” and I’ve found your site with this tutorial! And I don’t understand why it doesn’t have any comment!
It’s FANTASTIC!
Thank you so much, I’ll add shopdev.co.uk to my favourites now.
David Carreira
July 3, 2008
Quote
Many thanks! I’ve used the same principal as outlined in the tutorial for the logo at the top of this page. Again… thanks for the feedback!
July 15, 2008
Quote
NICE tutorial mang.
July 19, 2008
Quote
Very nice effect! Also very smart to stop() the animation on a change.
July 25, 2008
Quote
Thank you so much for this mate.
Great tutorial!
And I’ve added to it slightly. Instead of having the text contained within the image, I wanted it contained in the “a” tag for SEO purposes. So my link now looks like:
Also, I set up a sprite with multiple colours.
View an example here:
http://www.mmr.com.au/resource.....matedMenu/
July 25, 2008
Quote
sorry the tag didn’t work correctly with previous post:
July 25, 2008
Quote
Looks awesome! I edited your post as it looks like you forgot to wrap the code in “pre” tags. Thanks for the comment!
July 30, 2008
Quote
Man, this is amazingly nice.
Many thanks for the tutorial. (in fact I was just looking for a solution on how to stop the animation if you hover/out your mouse quickly. I found it also) !
August 17, 2008
Quote
this is exactly what i was looking for! anyone come up with an IE fix for it?
August 20, 2008
Quote
thank you!
i’ve try to find this effect so long
August 26, 2008
Quote
You’re very welcome guys!
@ Trisha, you could probably get the script to fall-back (without the fading transition). I don’t think that there’s a clever trick to get this working in IE6.
Just a note : IE7, Transparent PNGs & jQuery Animations do not play nicely. Avoid using transparent PNGs.
August 27, 2008
Quote
I have a question. Why are there scanlines on the sprites? Those do not appear in the example.
August 28, 2008
Quote
Hi Ethan!
If you’re talking about the images in the tutorial, the scanlines are meant to signify parts of the sprite not visible to the end user.
August 31, 2008
Quote
[…] Animated Menus Using jQuery (tags: CSS jquery navigation) […]
September 1, 2008
Quote
Hello - nice work! I took a look and implemented something very similar on my children’s party supplies site. With some jigging around I managed to use the same ’sprite’ for both the background, the foreground and the images that sits on top of it.
September 2, 2008
Quote
[…] ShopDev shows reverse-engineers the the interactive menus used on Dragon Interactive and shows how to were pull it off using JQuery. […]
September 4, 2008
Quote
very concise, and thorough instruction -thanks
September 10, 2008
Quote
this is great homer (as usual) definatly one im going to be using. keep up the great work on the articles - there some of the best instructions ive come across.
September 13, 2008
Quote
I’m glad you like it guys!
September 18, 2008
Quote
Good work.
I took a look and implemented something very similar on my saffron multimedia site.
September 18, 2008
Quote
Looks awesome Ronald!
September 29, 2008
Quote
[…] http://www.shopdev.co.uk/blog/.....ng-jquery/ […]
October 4, 2008
Quote
thanks! I did use this effect for my latest project.
October 9, 2008
Quote
Good write up, thank you for that. I’ve seen other sites try and explain this affect but this is the first one that made sense to me.
October 12, 2008
Quote
Thanx for the script. it’s very nice. i was googling this from a long day. but there is a problem. u did it for 2 steps. what if there are more than 2 step. consider the example of google.cn. see it. i want to do my menu like that. what to do. plz help me. it’s urgent.
October 19, 2008
Quote
Well done!! amazing tips thanks mate will try on mine
October 26, 2008
Quote
Beautiful fading effect!
October 29, 2008
Quote
Excellent tutorial, I’ll definitely be implementing this in my current project.
Extra Cool: I’ve found that if you set the text slightly (like 1 or 2 pixels) lower on the “hovered” part of the image, it truly gives the impression that the button is depressed (err… physically, not emotionally) ;-P
October 31, 2008
Quote
[…] the writing of this article, a similar technique was written up elsewhere, albeit without our nice CSS Sprites fallback. We also discovered a very […]
November 2, 2008
Quote
Thank you for your site
I made with photoshop backgrounds for myspace and youtube and more
my backgrounds:http://tinyurl.com/6ptkxd
have a good day and thank you again!
November 10, 2008
Quote
IE fix, just put 81px instead of 100% into
ul#menu li a span {
background:url(sprite.png) no-repeat scroll bottom left;
display:block;
position:absolute;
top:0;
left:0;
height:81px;
width:100%;
z-index:100;
}
BR
bjarke
November 17, 2008
Quote
Very nice! Similar effect can be created with IzzyMenu.com
November 25, 2008
Quote
this is one of the greatest javascript menus that I’ve found on the net.. the effect was awesome.. whoa!
with only a few lines of code… it’s really great…
I’m gonna ratimark this post
December 1, 2008
Quote
[…] View demo View tutorial […]
December 1, 2008
Quote
Homar,
First, thanks for the great tut - a very nice menu indeed. I’ve got it working beautifully in all respectable browsers but in IE6 my gifs are flashing
like Studio 54 on a Saturday night in 1977. I’m not interested in trying to find a fix for IE6 - only to *hide* the script from IE6. I’m a pro w/html
and css but I’m newb w/jQuery (and js in general). Can you/someone please advise me how to simply keep IE6 from seeing a particular bit o’ js?!?
many thanks again
svs
December 17, 2008
Quote
[…] the writing of this article, a similar technique was written up elsewhere, albeit without our nice CSS Sprites fallback. We also discovered a very […]
December 25, 2008
Quote
Hi!….
I was searching on internet and I found your web design blog…..it is really intresting…keep it up….look forward to read more from you
December 27, 2008
Quote
WOOOOOOO Cool fading effect! Very nice!
January 4, 2009
Quote
HI, i just wanted to thank you got this great article, I love the effect using Jquery, i would love to include this nice fancy effect in some of sites.
Thanks
deprowebs.com
January 4, 2009
Quote
Great!!!
January 6, 2009
Quote
Fabulous effect - particularly smooth transition and I think this could be used to great effect on a range of website designs. Thank you for the tutorial.
January 6, 2009
Quote
Nice thought and effort…. thinkin a design based on this menu for template. Thanks and keep it going
January 8, 2009
Quote
Empty tags are really wrong…
January 20, 2009
Quote
Bjarke’s fix for IE worked great for me! I highly recommend using it if you want it to work for IE6(that’s what I tested it in)
Thanks for the great tutorial!
January 24, 2009
Quote
Thanks 4 share.
This is a new approach to me, I know till now, moving background, but this technique is awesome.
@Trisha - try conditional comments to work around IE6.
January 27, 2009
Quote
[…] Animated Menus Using jQuery […]
January 29, 2009
Quote
[…] Animated Menus Using jQuery […]
January 29, 2009
Quote
[…] recently implement this jQuery effect from ShopDev into my own blog navigation. I, because I’m such a genius, actually improved it so that when […]
February 1, 2009
Quote
How could i use this on wordpress 2.7?
February 3, 2009
Quote
looks impressive… very neat…classy
February 4, 2009
Quote
[…] 25 - Animated Menus Using jQuery […]
February 5, 2009
Quote
Some may notice when the page loads, you see the “over” state for a split second then when the DOM is ready, it changes the opacity back to what it is suppose to look like.
To fix this, so no over state shows on load do the following:
In your CSS:
ul#menu li a span > set background align top, not bottom.
i.e:
ul#menu li a span background:url(../../images/menu_test.gif) no-repeat top left;
In the java rollover function add: background-image: -46px.
i.e:
$(this).stop().animate({
background-image: -46px
opacity: 1
}, “fast”);
This should make you image load the static state, even when it’s catching up on the page load.
Peace!
February 6, 2009
Quote
I’ve been looking for this tutorial for so long. Thank you very much. At first I thought the Dragon Interactive site was made in Flash, but after surfing for sometime, only I found they’re using javascript. Thank godness you’re showing us how to do this.
February 7, 2009
Quote
nice tutorial, thanks for sharing it
February 9, 2009
Quote
[…] Animated Menus Using jQuery […]
February 10, 2009
Quote
Hm, not really bad, but pretty simple and from SEO sight not very good for a site. If you can live without the engraved text ( or add some CSS magic ) you can archive the same effect with CSS alone.
February 15, 2009
Quote
Owesome!!!! Well done dude!
February 17, 2009
Quote
Simply Awesome!
February 21, 2009
Quote
Ok. So this does the fade thing. How would I achieve the same thing for sliding? Ideally without using a span thing in the a links. Could I just use the usual CSS of a:hover somehow to slide down? I am not interested in fade, I am interested in slide down. Would appreciate any tips on achieving this. (Please also email me if you respond as this blog has no “Notify when someone posts a comment” feature, thanks).
February 25, 2009
Quote
[…] 用jQuery制作动态菜单 Animated Menus Using jQuery […]
February 28, 2009
Quote
Very cool tutorial. I made my own version of the menu, but I have a huge problem: I need the menu items to stay highlighted when they’ve been clicked (like on Dragon Interactive’s site).
Any idea how to achieve this?
March 7, 2009
Quote
No bad, but I cannot find example of the using this technology.
March 11, 2009
Quote
Excellent tutorial!!
This cured the problem I was having with IE and fading in and out. I had to add wrap it in the document ready function instead of the plain “function()”, $(document).ready(function(){}); but I think that is because I have a later version of jQuery (1.3.2).
Thanks.
March 14, 2009
Quote
Very wonderful effect!
Greetings from de
March 25, 2009
Quote
Animated Menus Using jQuery…
Today, I’m going to show you how to create an animated menu….
March 27, 2009
Quote
I notice in the code that there is no ‘Real Text’ reference to the name of the target page. What I mean by that is that if you remove the CSS from the page, there is no text based navigation left in it’s place. Is this navigation good for SEO purposes? What would happen if you dropped in text in-between the spans?
March 30, 2009
Quote
[…] How To » Animated Menus Using jQuery […]
March 30, 2009
Quote
Wow!! I will definitely try this!! Thanks a lot for the tutorial.
March 30, 2009
Quote
very nice tutorial, thanks!
March 31, 2009
Quote
your comment…
I tried both these techniques to kill the flickering effect when the page loads in IE6 (yuck, I know, but the main audience of the site…) but none worked. I’m not sure about the second one, background-image is a tag for an image, unlike background-position. I tried with both and it broke the code.
Any ideas, anyone?
Oh, and great menu, really. Thank you so much.
March 31, 2009
Quote
[…] How To » Animated Menus Using jQuery […]
April 3, 2009
Quote
awesome! im using this on my site
April 5, 2009
Quote
Nice effect, simple but really effective.
April 7, 2009
Quote
This is a good start, but without a .active state this is somewhat pointless in it’s current form. It’s hard to sacrifice utility for pretty on something as basic as an active or current state to tell a visitor where they are in the site.
April 9, 2009
Quote
[…] 40 Exceptional jQuery Interface Techniques and Tutorials: 34. Animated Menus Using jQuery […]
April 9, 2009
Quote
I’m gonna try this!
April 9, 2009
Quote
Awesome! Awesome! Awesome! I love it, thanks!
April 11, 2009
Quote
[…] involves producing animation effects by changing the position of the background image. 81. Animated Menu using jQuery – This technique involves the use of animated menu using CSS, XHTML and javascript. 82. Create […]
April 14, 2009
Quote
A great tutorial I must say, also very descriptive. I started working with it, hope I’d be able to do some modifications into it.
April 20, 2009
Quote
I wondered, how DragonInteractive guys did it. Thank you for the cool and useful tutorial.
April 26, 2009
Quote
How would I make the links centered? I can’t find any simple way to have the links moved to the center rather than have a 20% margin.
Great tutorial! Thanks!
May 1, 2009
Quote
[…] 2. Animated Menu using jQuery […]
May 3, 2009
Quote
Fantastic contribution, thank you very much.
May 4, 2009
Quote
thanxxx cool menü
May 6, 2009
Quote
Very nice tutorial I must say,
In my point of view it is a really helpful for the users.Thanks for sharing.
May 11, 2009
Quote
This is simply awesome..!
Long live the mentor..!
May 11, 2009
Quote
Nice design and good tutorial.
May 14, 2009
Quote
Probably the best way of creating a FLASH type menu using XHTML and CSS with JQuery. Thanks for the tutorial.
If anyone can answer the following, it will be really helpful.
How can i animate the button to have a faster opacity? Basically I’d like to animate the button faster than the current pace. Can anyone help me with this?
May 28, 2009
Quote
Wow, that is really cool!
May 29, 2009
Quote
Hopefully to answer Birens question. I have not used this script as of posting this comment, but to speed up or slow down the fade transitions, remove the words slow from the script and replace them with a number like 300 which would be pretty fast or 2000 which would be pretty slow or slimply replace the words slow with the words fast.
Alos if you are using a number instead of a word like slow or fast, you can remove the “” or ” surrounding numbers in regards to duration of speed
May 29, 2009
Quote
this is a really great idea, but i think you could do this much more efficiently, also making the page more searchable and accessible…
if you use HTML text for the words “HOME” and “PORTFOLIO”, you could use a single background GIF with the full gradient (the same height you have above, but would only need to be, say, 10px wide, and no words in the GIF).
use CSS to set the background image to repeat-x, and use jQuery to animate the background image “up” on mouseover/focus, and back down on mouseout/blur (note the addition of focus/blur for keyboard users)…
you could then use the same, small background image for all navigation menu items, retain the inline text for bots and screen readers, and allow your users to increase their font size if they like (would probably want to give a background color as well, since the image is a fixed height)…
a good example of this can be found at Snook’s site:
http://snook.ca/archives/javas.....nimations/
just a couple thoughts,
Atg
May 30, 2009
Quote
Bjarke’s, the fix work fine! We will see disappear the IE6? This bug in an implementation of css is ridiculous
IE fix, just put 81px instead of 100% into
ul#menu li a span {
background:url(sprite.png) no-repeat scroll bottom left;
display:block;
position:absolute;
top:0;
left:0;
height:81px;
width:100%;
z-index:100;
}
BR
bjarke
June 2, 2009
Quote
Thanks to gave wonderful article…..
The demo explanation is really good…..
The animation in menus is really nice..
I am going to try these type of menus.
June 8, 2009
Quote
Great. I am searching a menu like this.
Thanks.
June 10, 2009
Quote
Thanks for the Photoshop file
June 14, 2009
Quote
Hi. just want to share the IE6 flickering bug. Just add html
{filter: expression(document.execCommand(”BackgroundImageCache”, false, true));}
on your css and it will be ok. It works fine to me.and set your cursor to cursor:pointer; to your ul#menu li a span
June 15, 2009
Quote
In CSS that kind of effect is only mouse over, i dont understand what is the difference of that?
Can you elaborate the difference thanks
June 23, 2009
Quote
Thanks for sharing this fine tutorial!
June 25, 2009
Quote
nice very nice..
June 25, 2009
Quote
Thank you