Animated Navigation Bar Using jQuery
For my first Article I will be writing on creating an Animated Navigation Bar Using jQuery. Hopefully you guys will like it.
Date: June 4, 2009
For my first Article I will be writing on creating an Animated Navigation Bar Using jQuery. Hopefully you guys will like it.
Date: June 4, 2009
This is my first guest tutorial at WebMuch so please bear with me if I miss out on anything.
![]()
I am assuming that the readers are well versed with CSS and HTML.
<div class="container">
<div id="navbar1">
<ul id="sprite">
<li id="b0" class="a0">Home</li>
<li id="b1">News</li>
<li id="b2">Blog</li>
<li id="b3">Pictures</li>
<li id="b4">Videos</li>
<li id="b5">Gallery</li>
<li id="b6">About</li>
<li id="b7" style="border-right:1px solid #1f1f1f;">Contact</li>
</ul>
</div>
</div>
*NOTE: I do not encourage inline styling, I did it because It’s a sin I was willing to commit.
html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, code,
del, dfn, em, img, q, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
font-weight: inherit;
font-style: inherit;
font-size: 100%;
font-family: inherit;
vertical-align: baseline;
}
body{
background-color:#1f1f1f;
}
.container{
width:960px;
margin:0px auto;
color:#FFFFFF;
font-family:Arial,sans-serif;
font-size:20px;
font-weight:bold;
}
#navbar1{
float:left;
width:960px;
height:36px;
font-size:14px;
margin:20px 0 0 0;
background:url(images/navbar_bg.png);
}
#navbar1 ul{
float:left;
width:585px;
height:36px;
margin-left:188px;
color:#000000;
}
#navbar1 ul{
background: url(images/anim_3.png) no-repeat;
background-position:1px 4px;
}
#navbar1 ul li{
float:left;
width:72px;
margin:3px 0 0 0;
height:22px;
display: inline;
text-align:center;
padding:7px 0 0 0;
border-left:1px solid #1f1f1f;
cursor:pointer;
}
*NOTE: This tutorial is only effective when using a predefined width for each button or list item as shown in the above image, this can also be implemented on navigation bars using dynamic widths, but you might have to change the jQuery and the image sprite quite a bit to achieve any kind of consistency in your design. Well to give you a basic explanation as to how we will animate the navigation bar from here on in, we will take a background image equal to the size of the list item, which, on mouse-over positions itself to the left of each respective list item. So, as you can see in my HTML code I have named(id) my unordered list as ‘sprite’ with a backround image. I have also named(id) my list items b0,b1,b2,b3….b7, depending on the number of list items you wish to have in your navigation bar. I’ll explain why we are doing this in a bit, after you read through the jQuery code.
$(document).ready(function(){
var selected=0; // Default background position
var position=0;
$("#sprite").css({backgroundPosition:''+selected+'px 4px'});
$("#b0,#b1,#b2,#b3,#b4,#b5,#b6,#b7").css({backgroundPosition:'0px 0px'}).mouseover(function(){
position=$(this).attr("id").slice(1,2)*($(this).outerWidth());
/*width of your list item*/
$("#sprite").stop().animate({backgroundPosition:''+position+'px 4px'},{duration:200});
});
$("#b0,#b1,#b2,#b3,#b4,#b5,#b6,#b7").css({backgroundPosition:'0px 0px'}).mouseout(function(){
$("#sprite").stop().animate({backgroundPosition:''+selected+'px 4px'},{duration:200});
});
});
<script type="text/javascript">
$(document).ready(function(){
var check;
var i;
for(i=0;i<20;i++){
if($(".a"+i+"").attr("id")){
check=$(".a"+i+"").attr("id").slice(1,2);
}
}
var selected=check*($("#b0").outerWidth());
var position;
$("#sprite").css({backgroundPosition:''+selected+'px 4px'});
$("#b0,#b1,#b2,#b3,#b4,#b5,#b6,#b7").css({backgroundPosition:'0px 0px'}).mouseover(function(){
position=$(this).attr("id").slice(1,2)*($("#b0").outerWidth());/*width of your list item*/
$("#sprite").stop().animate({backgroundPosition:''+position+'px 4px'},{duration:300});
});
$("#b0,#b1,#b2,#b3,#b4,#b5,#b6,#b7").css({backgroundPosition:'0px 0px'}).mouseout(function(){
$("#sprite").stop().animate({backgroundPosition:''+selected+'px 4px'},{duration:300});
});
});
</script>
Now we can easily name the class a0 or a1 or a2 or a4 of our list item (active list item) in our HTML to set the default background position of our background in #sprite and the script basically looks for the class with prefix “a” then slices out the next element i.e. 0 or 1 or 2 or 3, etc and multiplies it to the width of the element and gives it a default position from the left.
Feel free to leave comments if you appreciate the tutorial or if you have any problems implementing it.
Thanks for reading.
Comments currently closed.
Comments:
Gus Co
June 4, 2009
Very good Script for a Great Start!!!
But remember to always link to a zip file with all the files needed.
Thanks and Keep Up the good work
Josh
June 4, 2009
Thankyou Atul, Nice tutorial, this is great!
Tyler Diaz
June 5, 2009
Clever technique, only there seems to be a bug whenever I hover over one of the links. I’m using Firefox 2 with Mac Osx – When ever I scroll over to a new li/link there seems to be a jump where the effect bar jumps 2-3 times before getting to where it needs to get.
Daniel
June 5, 2009
Hi, I have a question,
What if I have a big word in my menu that will not fit in, like a Publications, or some big word in foreign language, how can I stretch space for button?
Atul
June 5, 2009
@Daniel
Well Daniel if you have a big word in your menu you might have to add a special case in the javascript or redo it altogether…
Now, instead of naming your list items/ buttons b0,b2,b3,b4,b5…. name them b0(for the first button), b50(for the second button), b150(for the second button)and so on….
Now, 0,50,150 is the distance from the left the image has to move for the first , second and third tab….
Simply slice out the 0,50 ,150 from the name(id) and your good to go…
eg.
$(“#b0px,#b50px,#b150px,#b175px,#b250px,#b345px”)
.css({backgroundPosition:’0px 0px’}).mouseover(function(){
var position=$(this).attr(“id”).slice(1,6);
$(“#sprite”).stop()
.animate({backgroundPosition:”+position+’px 4px’});
});
*Note:
-Now their is no need for multiplication now
-There are other ways around this but to keep your javascript small the former mentioned is effective
-Choose an image which looks good on dynamic widths of your list item/ button
The Mafalian
June 6, 2009
i saw that effect before,
thanks
Nikhil
June 9, 2009
Hi Atul!
But it would have been really nice had you put the download link for all sources.
Great tutorial! I’m thinking of modifying it and using it..
Well I’m facing a problem using this effect.I’m using it in ASP.NET application. Specifically, In master page. problem is, once i click on links i have provided, the slider gets reset to left most link because of postback.I got no idea how to fix this! Help..!
Aayush
June 10, 2009
Hey Guys,
We have updated the post with a few tweaks in the jQuery code and the link to the source code has been added at the bottom.
Hopefully now there will be no problems. If not so, Please comment.
I have been adding the source code with the previous posts but this was Atul’s first post and he didn’t know about linking to the source code. From now on he’ll be linking when posting.
Thanks Alot For Reading.
Nikhil
June 10, 2009
Hey guys thanks a lot for updating the source code and providing the download links
I guess it is by virtue of ASP.NET postbacks.
I still have the same problem.
Nikhil
June 11, 2009
Problem solved!
Had to register a javascript at page load in the code behind simply to find the element by id and then set its className. Had tried doing the same in the page’s aspx itself, but the javascript was giving errors all the time. So tried this approach. May not be the best, just a workaround may be. And of course, the sliding menu is looking impressive! Thanks to webmunch!
Videos Chicas Meando
June 29, 2009
hh.. amazing
Ganeshji Marwaha
July 1, 2009
This effect is already available as a jquery plugin called LavaLamp. Check it out at http://www.gmarwaha.com/blog/2007/08/23/lavalamp-for-jquery-lovers/
Atul
July 2, 2009
@Ganeshji Marwaha
I was not aware that a plugin for this is already available…….
CSS Brigit | Animated Navigation Bar Using jQuery
August 1, 2009
Animated Navigation Bar Using jQuery…
A simple tutorial on creating a awesome looking animated background sliding navigation bar using HTML, CSS & jQuery.
…
Vikas
August 1, 2009
Awesome… Atlast i leaned something web related.. Now i can implement this.. thnx for such an awesome tutorials.
Love you guys..!!! keep up
regards
Vikas
All About Drop-down menu – Daily interesting stuffs
August 4, 2009
[...] Animated Navigation Bar Using jQuery [...]
Animated Navigation Bar Using jQuery | AJAX Collection & Research
August 27, 2009
[...] check it out [...]
Styling HTML Lists with CSS: Techniques and Resources - Smashing Magazine
December 11, 2009
[...] [...]
CaoInteractive Blog | Graphic & Web Design » Blog Archive » Styling HTML Lists with CSS: Techniques and Resources
December 17, 2009
[...] Animated Navigation Bar Using jQuery This tutorial on WebMunch uses list-based navigation to create an animated navigation bar powered by jQuery. The demo page displays four different variations of the animated effect. [...]
Styling HTML Lists with CSS: Techniques and Resources | TheUnical Technologies Blog
December 25, 2009
[...] Animated Navigation Bar Using jQuery [...]