Slide out and drawer effect
Posted on 6th January 2008 — John Resig, author of jQuery demonstrated how easy is create this simple slide aka drawer aka accordion effect during @media Ajax 2007 – it also makes for a great introduction to jQuery.








Posted on 6th January 2008 — John Resig, author of jQuery demonstrated how easy is create this simple slide aka drawer aka accordion effect during @media Ajax 2007 – it also makes for a great introduction to jQuery.
The effect is commonly known as an ‘accordion’ and it’s usually used to slide up, or down blocks of content to expose new blocks.
The Apple web site is a great demonstration of this effect in action, where the mouse settles on the title of the ‘section’ and the associated links are exposed. What makes this effect particularly cool, is that the drawers maintain a fixed height and slide between restricted area.
This tutorial will walk through how to create your own simple plugin, and then replicate the Apple downloads drawers using the very excellent Accordion plugin.
Markup
Since we’re practising unobtrusive JavaScript, the markup will start with headings separating the list items.
There’s two approaches depending on your content. Either to use list elements or definition lists. Our example will use list elements.
And so on, with as many drawer elements as we want. Note that in this example, the ‘open’ class has only been applied to the first drawer.
Simple jQuery slide effect
In it’s simplest form, the effect is this when the user clicks on the drawer handle:
In addition, when the page loads, we should hide all the drawer content, except for our starting point.
This is achieved with the following code:
If you wanted to make better using of jQuery’s chaining the code could be written as this:
If you want it to slide, we can easily swap
.hide()for.slideUp()and.show()for.slideDown(), as I have done in the working example of the simple slide demoIf you look at the demo, for me, the annoying aspect is that the height changes depending on which drawer you’ve got open. In particular, if you have the first drawer open, and click on the second, the position of the third drawer handle moves up.
That’s where the accordion plugin helps us.
jQuery accordion plugin
Using the same markup as the previous example, if we take Jörn Zaefferer’s Accordion plugin we can match the effect on Apple’s site.
Before we can use the plugin however, there are a few prerequisites: in particular, you must have the dimensions plugin. The best place to get the latest files is the plugin repository for Accordion. Download the zip file (using the link towards the foot of the page) and grab the following files:
Again, using our existing markup, we can use the following code to get the plugin working straight away:
That’s it! Simple and very easy to use so long as you have all the right files.
See the plugin version of the slide out effect
You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!