- Tab 1
- Tab 2
- Tab 1
- Tab 2
- Tab 1
- Tab 2
- Tab 1
- Tab 2
- Header 1 Wow, look at all this content that can be shown or hidden with a simple click!
- Header 2 Lorem ipsum dolor sit amet, consectetuer adipiscing elit Aenean sollicitudin Sed interdum pulvinar justo Nam iaculis volutpat ligula Integer vitae felis quis diam laoreet ullamcorper Etiam tincidunt est vitae est Ut posuere, mauris at sodales rutrum, turpis tellus fermentum metus, ut bibendum velit enim eu lectus Suspendisse potenti [ 58 ] Chapter
- Header 3 Donec at dolor ac metus pharetra aliquam Suspendisse purus Fusce tempor ultrices libero Sed quis nunc Pellentesque tincidunt viverra felis Integer elit mauris, egestas ultricies, gravida vitae, feugiat a, tellus.
- element into the accordion We can use the jQuery object shortcut $ to specify an anonymous function which will be executed as soon as the document is ready This is analogous to using $(document).ready(function(){}) and helps to cut down on the amount of code we have to type Following this, we use the simple ID selector $("#myAccordion") to specify the element on the page we want to transform We then use the accordion() constructor method to create the accordion Other elements can be turned into accordions as well All list element variants are supported including ordered lists and definition lists You don't even need to base the accordion on a list element at all You can build a perfectly functional accordion using just nested and elements, although additional configuration will be required In this example, we used an empty fragment (#) as the value of the href attribute You should note that any URLs supplied for accordion headers will not be followed when the header is clicked within the accordion when using the default implementation [ 60 ] Chapter Styling the accordion With no styling, the accordion will take up 100% of the width of its container Like with other widgets, we have several options for styling the accordion We can create our own custom stylesheet to control the appearance of the accordion and its content, we can use the default or flora themes that come with the library, or we can use Theme Roller to create an extensive skin for the whole library Let's see how using the flora theme for the accordion will cause it to render In accordion1.html, add the following tag to the of the page: Save the new file as accordion2.html, also in the jqueryui folder, and view it again in a browser It should appear something like this: The accordion theme file assumes that an unordered list is being used as the basis of the widget and specifically targets
- elements with certain style rules We can easily create our own custom theme to style the accordion for situations where we want to use a non list-based accordion widget, or if we simply want different colors or font styles You can use the excellent Firebug plugin for Firefox, or another DOM viewer, to see the class names that are automatically added to certain elements when the accordion is generated You can also read through an un-minified version of the source file if you really feel like it These will be the class names that we'll be targeting with our custom CSS [ 61 ] The Accordion Widget The following screenshot shows Firebug in action: Change accordion2.html so that it appears as follows (new code is shown in bold): jQuery UI Accordion Widget Example 3 Header 1Wow, look at all this content that can be shown or hidden with a simple click! Header 2Lorem ipsum [ 62 ] Chapter Header 3Donec at dolor ac metus pharetra aliquam Suspendisse purus Fusce tempor ultrices libero Sed quis nunc Pellentesque tincidunt viverra felis Integer elit mauris, egestas ultricies, gravida vitae, feugiat a, tellus. //function to execute when doc ready $(function() { //turn specified element into an accordion $("#myAccordion").accordion(); }); Save this version as accordion3.html in the jqueryui folder The class name ui-accordion is automatically added to the accordion's container element Therefore, we can use this as a starting point for most of our CSS selectors The links that form our drawer headers are given the class ui-accordion-header so we can also target this class name In a new file, create the following stylesheet: #myAccordion { width:200px; border:2px solid #000000; position:relative; list-style-type:none; padding-left:0; } ui-accordion-header { text-decoration:none; font-weight:bold; color:#000000; display:block; width:100%; text-align:center; } ui-accordion div div { font-size:90%; } [ 63 ] The Accordion Widget ui-accordion a { color:#ffffff; background:url( /img/accordion/header-sprite.gif) repeat-x 0px 0px; } ui-accordion a.selected { background:url( /img/accordion/header-sprite.gif) repeat-x 0px -22px; } ui-accordion a:hover { background:url( /img/accordion/header-sprite.gif) repeat-x 0px -44px; } /* container rounded corners */ corner { position:absolute; width:12px; height:13px; background:url( /img/accordion/corner-sprite.gif) no-repeat; } topLeft { top:-2px; left:-2px; background-position:0px 0px; } topRight { top:-2px; right:-2px; background-position:0px -13px; } bottomRight { bottom:-2px; right:-2px; background-position:0px -26px; } bottomLeft { bottom:-2px; left:-2px; background-position:0px -39px; } Save this file as accordionTheme.css in your styles folder and preview accordion3.html in a browser We will need a new folder for the images we use in this and subsequent examples Create a new folder inside the img folder and name it accordion With just two images, and a few simple style rules, we can drastically change the default appearance of the accordion with our own custom skin as shown in the following screenshot: [ 64 ] Chapter Configuring accordion The accordion has a range of configurable properties which allow us to easily change the default behaviour of the widget The following table lists the available properties, their default value, and gives a brief description of their usage: Property Default Value Usage active Selector for the initially open drawer alwaysOpen first child true animated "slide" Animate the opening of drawers autoHeight true Automatically set height according to the biggest drawer clearStyle false Clear styles after an animation event "click" Event on headers that trigger drawers to open fillSpace false Accordion completely fills height of its container header "a" Selector for header elements navigation false Enable navigation for accordion navigationFilter location.href By default, this property opens the drawer whose heading's href matches location.href selectedClass "selected" Class name applied to headers with open drawers Ensure that one drawer is open at all times [ 65 ] The Accordion Widget Configurable properties The configurable properties for all of the different components of jQuery UI are constantly evolving with each new release of the library You can keep track of the latest properties by looking through the online jQuery UI API pages Each component has its own page and can be accessed from http://docs.jquery.com/UI/ Most of the properties are self-explanatory, and the values they accept are usually booleans, strings, or element references Let's put some of them to use so we can explore their functionality Alter accordion3.html so that it appears as follows:� jQuery UI Accordion Widget Example 4 Header 1Wow, look at all this content that can be shown or hidden with a simple mouseover! Header 2Lorem ipsum dolor sit amet, consectetuer adipiscing elit Aenean sollicitudin Sed interdum pulvinar justo Nam iaculis volutpat ligula Integer vitae felis quis diam laoreet ullamcorper Etiam tincidunt est vitae est Ut posuere, mauris at sodales rutrum, turpis tellus fermentum metus, ut bibendum velit enim eu lectus Suspendisse potenti. Header 3Donec at dolor ac metus pharetra aliquam Suspendisse purus Fusce tempor ultrices libero Sed quis nunc Pellentesque tincidunt viverra felis Integer elit mauris, egestas ultricies, gravida vitae, feugiat a, tellus. [ 66 ] Chapter //function to execute when doc ready $(function() { //set the event property var accOpts = { event:"mouseover" } //turn specified element into an accordion $("#myAccordion").accordion(accOpts); }); First, we create a new object literal called accOpts which contains one property key and a value We then pass this object into the accordion() constructor as an argument, and it overrides the default properties of the widget The string we specified for the value of the event property becomes the event that triggers the activation of the drawers, making this a very useful property Save the changes as accordion4.html You should note that you can also set properties using an inline object within the widget's constructor method without creating a separate object (see accordion4Inline.html) Using the following code would be equally as effective, and would often be the preferred way for coding: //function to execute when doc ready $(function() { //turn specified element into an accordion $("#myAccordion").accordion({ event:"mouseover" }); }); [ 67 ] The Accordion Widget We can set other properties at the same time as well If we want to change which drawer is open by default when the accordion is rendered, as well as change the trigger event, we would supply both properties and the required values, with each pair separated by a comma Update accordion4.html so that it appears as follows: jQuery UI Accordion Widget Example 5 Header 1Wow, look at all this content that can be shown or hidden with a simple mouseover! Header 2Lorem ipsum dolor sit amet, consectetuer adipiscing elit Aenean sollicitudin Sed interdum pulvinar justo Nam iaculis volutpat ligula Integer vitae felis quis diam laoreet ullamcorper Etiam tincidunt est vitae est Ut posuere, mauris at sodales rutrum, turpis tellus fermentum metus, ut bibendum velit enim eu lectus Suspendisse potenti. Header 3Donec at dolor ac metus pharetra aliquam Suspendisse purus Fusce tempor ultrices libero Sed quis nunc Pellentesque tincidunt viverra felis Integer elit mauris, egestas ultricies, gravida vitae, feugiat a, tellus //function to execute when doc ready $(function() { //configure accordion [ 68 ] Chapter var accOpts = { event:"mouseover", active:"#header3" } //turn specified element into an accordion $("#myAccordion").accordion(accOpts); }); The first change is to give our header elements id attributes in the underlying HTML in order to target them with the active property In our object literal, we then specify the selector for the header we would like to open by default Save the file as accordion5.html When the page is opened, the third drawer should be displayed by default The other properties listed in the table at the start of this section are equally as easy to configure Change the object literal so that it appears as follows: //configure accordion var accOpts = { event:"mouseover", active:"#header3", alwaysOpen:false, autoHeight:false } Save these changes as accordion6.html and view the results in a browser First, you should find that when you first roll over a heading the drawer opens as normal, but the accordion grows or shrinks depending on how much content is in the drawer It no longer stays at a fixed height This can be seen in the following example: [ 69 ] The Accordion Widget You should also find that if you roll over a heading whose drawer is already open, the drawer will close and the accordion will shrink so that only the headers are displayed with no open drawers Note that when using false with the alwaysOpen property, the accordion will shrink in this way regardless of whether the autoHeight property is set to true or false The fillSpace property, if set, will override autoHeight You should also be aware that the clearStyle property will not work with autoHeight One final property we should look at is the navigation property This property is used to enable navigating to new pages from accordion headings Change accordion6.html to this: jQuery UI Accordion Widget Example 7 Header 1Wow, look at all this content that can be shown or hidden with a simple mouseover! Header 2Lorem ipsum dolor sit amet, consectetuer adipiscing elit Aenean sollicitudin Sed interdum pulvinar justo Nam iaculis volutpat ligula Integer vitae felis quis diam laoreet ullamcorper Etiam tincidunt est vitae est Ut [ 70 ] ... type="text/javascript" src="jqueryui1.6rc2/ jquery- 1 .2. 6.js"> ... type="text/javascript" src="jqueryui1.6rc2/ jquery- 1 .2. 6.js"> ... type="text/javascript" src="jqueryui1.6rc2/ jquery- 1 .2. 6.js">