jQuery Accordion

22nd August, 2010 @ 10:06am UTC

Development, JavaScript, jQuery, Tools, UI, Web, Web Standards, Widgets / 9 Comments

Update November 2010: Documentation for this jQuery Accordion can now be found here:

jQuery Accordion – Tim Huegdon’s Projects

Following on from my post about my jQuery Carousel, here’s another widget: an accordion.

Until recently, I’ve usually used my friend and ex-colleague Marco’s jQuery accordion plug-in. However, whilst prototyping some stuff for work, I noticed a rather uncomfortable animation jump which, after some investigation, seems to be a well documented issue with jQuery’s slideDown() method. Since I was prototyping at the time, I quickly knocked up my own very basic accordion and, over time, it grew into something more.

The current version of my accordion follows a recent refactor–there are still several features I’d like to add–but for now I’m happy to let it out into the wild.

Demo

As was the case with my carousel, I’d definitely recommend taking a look at my page of example accordion implementations. This should give you a reasonable idea of the capabilities of the script; and as I update the widget, I’ll update this page.

Source Code

All the source code for the accordion–and the example page above–is available in the following GitHub repository:

jQuery Accordion — GitHub repository

Implementation

HTML

Basic HTML for the accordion is as follows:

<ul class="accordion">
    <li>
        <h3>Handle 1</h3>
        <div class="panel">
            …
        </div>
    </li>
    <li>
        <h3>Handle 2</h3>
        <ul class="panel">
            …
        </ul>
    </li>
    <li>
        <h3>Handle 3</h3>
        <p class="panel">
            …
        </p>
    </li>
</ul>

For each panel in the accordion, you will require both a panel (the content of our accordion pane) AND a handle (an item that is clicked to open a panel). These can be anything you want as you can specify the jQuery selector used for each as part of your configuration. By default, the selector for handles is “h3″, and the selector for panels is “.panel”. Obviously that was fairly specific to my original implementation, so I’ll look to fix that in the near future.

Notice that, because we’re specifying our own selectors, we’re able to use flexible HTML for either the panel or the handle. However, it’s also worth remembering that the accordion script injects a link around the contents of whatever you specify as the handle, so that the handle becomes focusable and keyboard navigable. If you are likely to end up with invalid markup (i.e. the inline link wrapped around a block level element), you may get unpredictable behaviour cross-browser.

CSS

The included CSS is really only a reset style-sheet for the list elements within the accordion. You will probably already be achieving this with your own reset stylesheet, but you can add the following link to the head of your document:

<link rel="stylesheet" href="css/accordion.core.css" type="text/css" charset="utf-8">

JavaScript

To activate the accordion in your pages, you will require both jQuery and the accordion script itself to be included at the bottom of your page, just before the closing tag, but before any script that applies the accordion to a jQuery object.

A default implementation would look like this:

    …
    <script type="text/javascript" src="js/jquery-1.4.2.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery.accordion.2.0.js" charset="utf-8"></script>
    <script type="text/javascript">
        $(".accordion").accordion();
    </script>
</body>

As with my carousel, you can configure the accordion more specifically by passing a configuration object to the .accordion() method:

<script type="text/javascript">
    $(".accordion").accordion({
        "handle":           "h3",
        "panel":            ".panel",
        "speed":            200,
        "easing":           "swing",
        "accordion":        true,
        "toggle":           false,
        "activeClassPanel": "open",
        "activeClassLi":    "active",
        "lockedClass":      "locked"
    });
</script>

Note: The above options are all the defaults.

Parameters for the configuration object are as follows:

Option Value
handle A selector that specifies the element that will contain the injected link to trigger the opening of each panel.
panel A selector that specifies the panel element to open and close.
speed The animation speed for opening and closing. Defaults to 200.
easing The animation easing for opening and closing. Defaults to “swing”.
accordion A boolean to specify whether other items should close when one is opened. If this is false, all items work independently.
toggle A boolean to specify whether a handle can toggle opening and closing of an element.
activeClassPanel HTML class for the active panel.
activeClassLi HTML class for the active parent <li>
lockedClass HTML class used to lock any panels open.

I’ve tried to cover all the above concepts in the examples page. However, feel free to comment requests for more information, or bug reports. Alternatively, you can fork and append the GitHub repo, or register bugs there.

Like this post?

Tweet it or Del.icio.us it!

Pinboard

Advertising

Comments (9)

Skip to the comment form…

  1. Gravatar Image noviolence September 18, 2010 @ 1:14 pm

    Hello,

    I had some severe problems getting rid of some annoying jumping in my accordeons..
    I implemented your plugin and it seems to work really fine.

    There’s just one thing i had to change. The “active” class is changeing too late. For my usage it’s much better if it changes directly on the click and with the beginning of the slide, not after it’s completion. I assume it’s like that for most people.

    Except that, works like a charm, thanks a lot!

    Greetings Phil

  2. Gravatar Image jonnyjaniero September 21, 2010 @ 5:32 pm

    nice work. thanks. working great in ff but doesn’t work in ie7 or ie8. checked your demo page, that also doesn’t work with ie.

    i’d have no idea how to fix. any clues, bugs fixes ?

    thanks

  3. Gravatar Image noviolence September 22, 2010 @ 5:28 pm

    Thanks jonnyjaniero,

    I didnt test the functionality in IE,
    this really pisses me off.

    Judging on the looks of your Demo Site in IE you must be a IE Hater.
    Well, me too.. but i have to get stuff working in IE also.

    Now I gotta look for another way, yayks..

  4. Gravatar Image Tim September 23, 2010 @ 9:47 am

    Hmmm… I had originally tested in IE and it worked fine, but I’ve clearly made some changes and broken it.

    I’ll try and fix it today.

  5. Gravatar Image Tim September 23, 2010 @ 10:33 am

    IE bug fixed.

    I’d forgotten that IE will return “auto” from a jQuery css(“height”) call if the height is automatically calculated. Fixed by using offsetHeight.

  6. Gravatar Image noviolence September 26, 2010 @ 10:58 pm

    I apologize for my behavior.

    Tim did a great job fixing some bugs and even incorporated the wish from my first comment.
    Everything is working really nice in all major browsers ( IE6 included )

    If you run in some flicker problems in IE make sure it’s not rendering your page in Quirksmode.

    thanks again for your efforts Tim :)

  7. Gravatar Image Benny Halperin September 12, 2011 @ 10:26 am

    Hi,

    Please update the JavaScript section on this page to reflect your latest version:

    - the option “accordion” in now “canOpenMultiple”
    - the option “toggle” is now “canToggle”

    I spent some time figuring out why it didn’t work as advertized.

    Otherwise looks good.

    Thanks, Benny

  8. Gravatar Image Benny Halperin January 10, 2012 @ 5:10 pm

    Suggestion: add functionality to support actions triggered by clicking a handle. Specifically, if I have a panel-less accordion section, and I want the handle to trigger an action instead of opening its panel.

  9. Gravatar Image Tim January 10, 2012 @ 11:35 pm

    @Benny: As linked from the beginning of the article, up to date documentation can be found here:

    http://projects.timhuegdon.com/jquery-accordion/

    In that documentation you will find details of the custom events that are available for you to register your additional actions triggered by clicking a handle.

Leave a comment





Categories

Syndication

Technorati

© 2012 Tim Huegdon, All Rights Reserved / Website design and development by Nefarious Designs

Powered by Wordpress / Log in

This website was made entirely from sticky-back plastic and glue.