Code

Web Design Tips: jQuery UI Accordion Widget

Adding an interactive accordion widget to your ecommerce site can provide a great design feature that conveys professionalism and makes your content more readable.

There are plenty of instances in website design when we would like to have a lot of content on a page, but, unless you are creating articles or blog posts, too much text causes the pages to scroll, which makes the content less readable.

As an example, imagine that you have a product detail page with a product description, product image, 20 or more customer reviews, washing and care instructions, inventory availability, related products, and shipping options. That can be a lot of text on a page type (i.e. a contact page or even a product detail page) that does not usually scroll well. So how do you manage all of that copy?

One solution is to use what designers call an accordion, which simultaneously shows some bit of content while hiding other content. You can think of it as a vertical tab.

In this edition of “Web Design Tips,” I am going to show you how to very quickly implement an accordion widget based on the jQuery library.

Video: Implementing a jQuery Accordion Widget

Step 1: Download

The jQuery user interface accordion widget is a simple-to-use, ready to implement solution for adding the aforementioned interaction and readability to your website. To get the accordion working, you are going to need to download three files from the jQuery site, including the jQuery library, jQuery UI core, and the jQuery UI effects core.

You can get all of these files from the jQuery UI download page. For this purpose, you only need to select three checkboxes–UI Core, Accordion, and Effects Core—but if you plan to use other jQuery UI effects or widgets on your site, you may want to selection more options before downloading.

Step 2: Add the jQuery UI Files to Your Site’s Directory

When you download the files you receive a zip file that includes the JavaScript files, some CSS, and even an HTML page. We are going to move this into our site hierarchy.

Step 3: Link the jQuery Files and User Interface CSS

Just add the code below to the <head> portion of your HTML document.

<code>&lt;link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet"/&gt;
&lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"&gt;&lt;/script&gt;
</code>

Step 4: Add the Accordion Function

Now add this accordion function:

            $(function(){

                // Accordion
                $("#accordion").accordion({ header: "h3" });

                //hover states on the static widgets
                $('#dialog_link, ul#icons li').hover(
                    function() { $(this).addClass('ui-state-hover'); }, 
                    function() { $(this).removeClass('ui-state-hover'); }
                );

            });

Step 5: Add Any Additional CSS

The HTML included in the jQuery UI download has an internal CSS to supplement the external styles. As you customize the accordion widget you may want to integrate these styles into your main CSS. Regardless of how you implement these styles, here they are.

<code>        /*demo page css*/
        .demoHeaders { margin-top: 2em; }
        #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
        #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
        ul#icons {margin: 0; padding: 0;}
        ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
        ul#icons span.ui-icon {float: left; margin: 0 4px;}
</code>

Step 6: Add The Accordion <div> And Its Children

Next we need to create the HTML that the accordion widget will modify. The <h3> tags represent the content title for each layer in the accordion.

<code>    &lt;div id="accordion"&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Flash Rebirth #3&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/flash.png" alt="Flash Rebirth" /&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Shazam!&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/shazam.png" alt="Shazam!" /&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Superman, Action Comics&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/superman.png" alt="Superman, Action Comics" /&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
</code>

Step 7: Add Your Content

Now just add your titles and content into each layer of the accordion, and you have a great jQuery UI widget that helps make your site easier to read and more interactive.

I have included all of the code from the sample page in the demonstration below. Be sure to watch the video to see just how I implemented this widget.

<code>&lt;head&gt;
&lt;title&gt;Practical eCommerce Mobile Commerce CSS Demonstration&lt;/title&gt;
&lt;link href="style.css" rel="stylesheet" type="text/css" media="screen" /&gt;
&lt;link href="handheld.css" rel="stylesheet" type="text/css" media="handheld" /&gt;
&lt;link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" /&gt;  
&lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
        $(function(){

            // Accordion
            $("#accordion").accordion({ header: "h3" });

            //hover states on the static widgets
            $('#dialog_link, ul#icons li').hover(
                function() { $(this).addClass('ui-state-hover'); }, 
                function() { $(this).removeClass('ui-state-hover'); }
            );

        });
&lt;/script&gt;
&lt;style type="text/css"&gt;
        /*demo page css*/
        .demoHeaders { margin-top: 2em; }
        #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
        #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
        ul#icons {margin: 0; padding: 0;}
        ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
        ul#icons span.ui-icon {float: left; margin: 0 4px;}
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="wrapper"&gt;
    &lt;div id="access"&gt;
        &lt;a href=""&gt;Skip to content&lt;/a&gt;
        &lt;a href=""&gt;Skip to search&lt;/a&gt;
    &lt;/div&gt;&lt;!--end access--&gt;

&lt;div id="header"&gt;

    &lt;a href=""&gt;&lt;img src="images/logo.png" alt="Nashville Comics" /&gt;&lt;/a&gt;

    &lt;div id="searchbox"&gt;
    &lt;input id="search" class="input-text" type="text" value="" name=""/&gt;
    &lt;input type="image" alt="Search" src="images/search.png"/&gt;
    &lt;/div&gt;&lt;!--end search--&gt;
    &lt;div id="top-nav"&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;a href=""&gt;Marvel&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;DC&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Darkhorse&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Image&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Verdigo&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Minx&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Zuda&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Space&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;PeC&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/div&gt;&lt;!--end top-nav--&gt;




&lt;/div&gt;&lt;!--end header--&gt;

&lt;div id="content"&gt;
    &lt;div id="hero-section"&gt;
    &lt;div id="accordion"&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Flash Rebirth #3&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/flash.png" alt="Flash Rebirth" /&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Shazam!&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/shazam.png" alt="Shazam!" /&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;
            &lt;h3&gt;&lt;a href="#"&gt;Superman, Action Comics&lt;/a&gt;&lt;/h3&gt;
            &lt;div&gt;&lt;img src="images/superman.png" alt="Superman, Action Comics" /&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;/div&gt;&lt;!--end hero-section--&gt;


    &lt;ul&gt;
    &lt;li id="product1"&gt;
    &lt;img src="images/ac11.png" /&gt;
    &lt;h3&gt;Action Comics #11&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis facliisis. &lt;/p&gt;
    &lt;span id="price"&gt;$11,990.00&lt;/span&gt;
    &lt;button class="form-button" onclick="setLocation('')"&gt;
    &lt;span&gt;Add to Cart&lt;/span&gt;
    &lt;/button&gt;
    &lt;/li&gt;&lt;!--end product1--&gt;

    &lt;li id="product2"&gt;
    &lt;img src="images/ac27.png" /&gt;
    &lt;h3&gt;Action Comics #27&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis facliisis. &lt;/p&gt;
    &lt;span id="price"&gt;$9,990.00&lt;/span&gt;
    &lt;button class="form-button" onclick="setLocation('')"&gt;
    &lt;span&gt;Add to Cart&lt;/span&gt;
    &lt;/button&gt;
    &lt;/li&gt;&lt;!--end product2--&gt;

    &lt;li id="product3"&gt;
    &lt;img src="images/ac1.png" /&gt;
    &lt;h3&gt;Action Comics #1&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis facliisis. &lt;/p&gt;
    &lt;span id="price"&gt;$2,102,990.00&lt;/span&gt;
    &lt;button class="form-button" onclick="setLocation('')"&gt;
    &lt;span&gt;Add to Cart&lt;/span&gt;
    &lt;/button&gt;
    &lt;/li&gt;&lt;!--end product3--&gt;

    &lt;li id="product4"&gt;
    &lt;img src="images/ac2.png" /&gt;
    &lt;h3&gt;Action Comics #2&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis facliisis. &lt;/p&gt;
    &lt;span id="price"&gt;$898,990.00&lt;/span&gt;
    &lt;button class="form-button" onclick="setLocation('')"&gt;
    &lt;span&gt;Add to Cart&lt;/span&gt;
    &lt;/button&gt;
    &lt;/li&gt;&lt;!--end product4--&gt;
    &lt;/ul&gt;
&lt;/div&gt;&lt;!--end content--&gt;

&lt;div id="footer"&gt;
    &lt;p&gt;Copyright 2009 Practical eCommerce, AKA Nashville Comics, All Rights Reserved&lt;/p&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;a href=""&gt;Marvel&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;DC&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Darkhorse&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Image&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Verdigo&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Minx&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Zuda&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Space&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;PeC&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;ul&gt;
    &lt;li&gt;&lt;a href=""&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Privacy&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=""&gt;Customer Service&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;&lt;!--end footer--&gt;

&lt;/div&gt;&lt;!--end wrapper--&gt;

&lt;/body&gt;

&lt;/html&gt;
</code>
Armando Roggio
Armando Roggio
Bio   •   RSS Feed


x