Monday, July 9, 2012

How to Make a Smooth Animated Menu with jQuery


The Finished Product
Ever seen some excellent jQuery navigation that left you wanting to make one of your own? Today we’ll aim to do just that by building a menu and animate it with some smooth effects.


The Goal – One Smooth Menu

Here’s a demo of what we’ll end up with by the end.

Introduction – An explanation of easing

The menu has such a smooth animation because of a thing called “easing”. Adobe’s definition in the Flash Developer Center is a little more complete:
“The term easing refers to gradual acceleration or deceleration during an animation, which helps your animations appear more realistic. Easing creates a more natural appearance of acceleration or deceleration by gradually adjusting the rate of change.”
Thanks to the magic of the jQuery Easing plugin, we can now use easing outside of Flash and Actionscript environments. Download it on the official project site.

Step 1 – Set up the Structure

Before starting with any jQuery, we’ve got to build a quick menu structure with XHTML and load in the required project files. Make new XHTML, CSS, and javascript documents. I’ve chosen to name each of mine “animated-menu”. Make two folders in the root directory for images and javascript. I’ve attached a screenshot of my folder structure to clarify: Animated Menu Folder Nothing out of the ordinary here. Start by loading in the necessary libraries and external files in the head. I have chosen to load jQuery from the Google code archive online, while the easing plugin from above is placed into the “js” folder. The order of loading is important!
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  5. <head>  
  6.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  7.     <title>Smooth Animated jQuery Menu</title>  
  8.   
  9.     <link rel="stylesheet" href="animated-menu.css"/>  
  10.   
  11.     <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.js" type="text/javascript"></script>  
  12.     <script src="js/jquery.easing.1.3.js" type="text/javascript"></script>  
  13.     <script src="animated-menu.js" type="text/javascript"></script>  
  14. </head>  
Then copy in this menu structure into the body:
  1. <body>  
  2.     <p>Rollover a menu item to expand it.</p>  
  3.     <ul>  
  4.         <li class="green">  
  5.             <p><a href="#">Home</a></p>  
  6.             <p class="subtext">The front page</p>  
  7.         </li>  
  8.         <li class="yellow">  
  9.             <p><a href="#">About</a></p>  
  10.             <p class="subtext">More info</p>  
  11.         </li>  
  12.         <li class="red">  
  13.             <p><a href="#">Contact</a></p>  
  14.             <p class="subtext">Get in touch</p>  
  15.         </li>  
  16.         <li class="blue">  
  17.             <p><a href="#">Submit</a></p>  
  18.             <p class="subtext">Send us your stuff!</p>  
  19.         </li>  
  20.         <li class="purple">  
  21.             <p><a href="#">Terms</a></p>  
  22.             <p class="subtext">Legal things</p>  
  23.         </li>  
  24.     </ul>  
  25. </body>  
  26. </html>  
Menu items have a class assigned to it that will designate the color of the block. Within each menu block is a title and subtitle that will be hidden by default.

Step 3 – Style with CSS

Now that the menu structure is in place we’ll add some basic styles to neaten up and arrange the menu horizontally. Overflow must be set to overflow for each list item. This will ensure that the subtitle for each item will not display until the height expands to fit.
  1. body{  
  2.     font-family:"Lucida Grande"arialsans-serif;  
  3.     background:#F3F3F3;  
  4. }  
  5.   
  6. ul{  
  7.     margin:0;  
  8.     padding:0;  
  9. }  
  10.   
  11. li{  
  12.     width:100px;  
  13.     height:50px;  
  14.     float:left;  
  15.     color:#191919;  
  16.     text-align:center;  
  17.     overflow:hidden;  
  18. }  
  19.   
  20. a{  
  21.     color:#FFF;  
  22.     text-decoration:none;  
  23. }  
  24.   
  25. p{  
  26.     padding:0px 5px;  
  27. }  
  28.   
  29.     .subtext{  
  30.         padding-top:15px;  
  31.     }  
  32.   
  33. /*Menu Color Classes*/  
  34. .green{background:#6AA63B;}  
  35. .yellow{background:#FBC700;}  
  36. .red{background:#D52100;}  
  37. .purple{background:#5122B4;}  
  38. .blue{background:#0292C0;}  

Step 4 – Animate with jQuery

All of our jQuery code will go into the javascript file created earlier. It will be called “animated-menu.js” if you’ve been following my model.
  1. $(document).ready(function(){  
  2.   
  3.     //When mouse rolls over  
  4.     $("li").mouseover(function(){  
  5.         $(this).stop().animate({height:'150px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
  6.     });  
  7.   
  8.     //When mouse is removed  
  9.     $("li").mouseout(function(){  
  10.         $(this).stop().animate({height:'50px'},{queue:false, duration:600, easing: 'easeOutBounce'})  
  11.     });  
  12.   
  13. });  
There are two actions in the code above. When the mouse moves over a menu item, that item starts an animation where it expands to 150px tall over 0.6 seconds. The easing applied through the plugin is ‘easeOutBounce’ which causes the box to “bounce” a little as it reaches the end of the animation (“out”). When the mouse is moved off the animation to the starting size is triggered.
If you are using this within the context of a larger site, simply substitute the element selected (currently “li”) for the mouse events to the necessary selector.
The stop() method is chained before the animate in order to prevent a buffer from building where the animation will loop repeatedly if the mouse is moved around quickly over it. An article on Learning jQuery covers this solution in greater depth.

Step 5- Add Some Flair

I’ve added background images to each of the menu items in the live version and source files to illustrate some design possibilities. There are plenty of other creative ways to add some personality to the menu. Go on and experiment. If you come up with anything remarkable, be sure to share it with us in the comments.

0 comments: