Creating Apple's New Slide Menu with jQuery
January 30th, 2011 / Code
I don't know how long it's been there, but I noticed yesterday that Apple has a new product nav menu that replaced their old horizontal scroller with some new animated candy. I was impressed to see that all transitions and animations are CSS-based. Unfortunately, it will take some time before all major browsers even support this cool new CSS wizardry. Never fear—you can create a similar menu with jQuery that works in every browser, even IE6.
Guts of the menu
At its heart, this isn't a very complicated menu. You have a few hidden unordered lists that you scroll into view one item at a time, the animations have a slight easing to give the bouncy appearance, and the direction of the entrance and exit animations depend on the order you navigate through the menu (nice attention to detail).
How I set it up
The menu is made up of unordered lists inside the parent container:
<div id="menu-container"> <ul> <li><a href="#"></a></li> <li><a href="#"></a></li> <li><a href="#"></a></li> <li><a href="#"></a></li> </ul> <ul> <li><a href="#"></a></li> <li><a href="#"></a></li> <li><a href="#"></a></li> </ul> <ul> <li><a href="#"></a></li> <li><a href="#"></a></li> <li><a href="#"></a></li> <li><a href="#"></a></li> </ul> </div>
The lists and list items are positioned absolutely so they pile up on top of one another, and when it's time to animate I pull out the appropriate list's items one at a time and position them with some simple math. Here's what the framework looks like:
The controls for the menu are simple as well:
<div id="menu-controls"> <a href="#" data-target="0">List 1</a> <a href="#" data-target="1">List 2</a> <a href="#" data-target="2">List 3</a> </div>
data-target attribute determines which list to animate when clicked. This target refers to the index of the list items. So target 0 refers to list 1, 1 to 2, etc. The script will add
class="active" to the selected menu button's <a> tag, so you can style accordingly.
Once the basics were down I firmed up the logic, added some animation easing, and made every piece of it customizable. The one thing I didn't do was add the animated menu indicator (a triangle in Apple's menu) that slowly animates to the active menu button. I consider that a flourish that goes along with the entire design of the Apple's site. I left my demo on the simple side so the active button just gets bolded on mine.
I created a demo page with a sample menu for four separate categories:
The HTML is commented enough to explain all the options, and you can get the code here at bitbucket.
switchTo(index)method has been added so you can easily manipulate the menu. It takes one parameter, the index of the <ul> you want to switch to. Note: The default controller click handlers won't start a new animation until the previous one finishes, so frantic clicking on the menu controls will not mess anything up. There is no such restriction on the
switchTomethod, so use smartly.
- By default, the menu will now wait for all images in the first list to load before animating in. There is also a configuration option
preloadAllwhich will make it load all menu images before starting when set to true.
- If you want to use a loading graphic, you can tell the menu where it is so it will hide it for you once all the images are loaded and ready to go.
- Updated the easing method used in transitions so looks a little smoother.
View demo source for more explanation.