Accordion Widgetversion added: 1.0
Description: Convert a pair of headers and content panels into an accordion.
The markup of your accordion container needs pairs of headers and content panels:
1
2
3
4
5
6
|
|
Accordions support arbitrary markup, but each content panel must always be the next sibling after its associated header. See the header
option for information on how to use custom markup structures.
The panels can be activated programmatically by setting the active
option.
Keyboard interaction
When focus is on a header, the following key commands are available:
-
UP
/LEFT
: Move focus to the previous header. If on first header, moves focus to last header. -
DOWN
/RIGHT
: Move focus to the next header. If on last header, moves focus to first header. -
HOME
: Move focus to the first header. -
END
: Move focus to the last header. -
SPACE
/ENTER
: Activate panel associated with focused header.
When focus is in a panel:
-
CTRL
+UP
: Move focus to associated header.
Theming
The accordion widget uses the jQuery UI CSS framework to style its look and feel. If accordion specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes
option:
-
ui-accordion
: The outer container of the accordion.-
ui-accordion-header
: The headers of the accordion. The active header will additionally have aui-accordion-header-active
class, the inactive headers will have aui-accordion-header-collapsed
class. The headers will also have aui-accordion-icons
class if they containicons
.-
ui-accordion-header-icon
: Icon elements within each accordion header.
-
-
ui-accordion-content
: The content panels of the accordion. The active content panel will additionally have aui-accordion-content-active
class.
-
Dependencies
- UI Core
- Widget Factory
-
Effects Core (optional; for use with the
animate
option)
Additional Notes:
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
Options
active
0
-
Boolean: Setting
active
tofalse
will collapse all panels. This requires thecollapsible
option to betrue
. - Integer: The zero-based index of the panel that is active (open). A negative value selects panels going backward from the last panel.
Initialize the accordion with the active
option specified:
1
2
3
|
|
Get or set the active
option, after initialization:
1
2
3
4
5
|
|
animate
{}
-
Boolean: A value of
false
will disable animations. - Number: Duration in milliseconds with default easing.
- String: Name of easing to use with default duration.
-
Object: An object containing
easing
andduration
properties to configure animations.- Can also contain a
down
property with any of the above options. - "Down" animations occur when the panel being activated has a lower index than the currently active panel.
- Can also contain a
Initialize the accordion with the animate
option specified:
1
2
3
|
|
Get or set the animate
option, after initialization:
1
2
3
4
5
|
|
classes
|
Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes
option.
Initialize the accordion with the classes
option specified, changing the theming for the ui-accordion
class:
1
2
3
4
5
|
|
Get or set a property of the classes
option, after initialization, here reading and changing the theming for the ui-accordion
class:
1
2
3
4
5
|
|
collapsible
false
Initialize the accordion with the collapsible
option specified:
1
2
3
|
|
Get or set the collapsible
option, after initialization:
1
2
3
4
5
|
|
disabled
false
true
.Initialize the accordion with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
event
"click"
Initialize the accordion with the event
option specified:
1
2
3
|
|
Get or set the event
option, after initialization:
1
2
3
4
5
|
|
header
function( elem ) { return elem.find( "> li > :first-child" ).add( elem.find( "> :not(li)" ).even() ); }
-
Selector: Selector for the header element, applied via
.find()
on the main accordion element. -
Function: A function accepting the accordion element and returning the header element (added in
1.13
).
Initialize the accordion with the header
option specified:
1
2
3
|
|
Get or set the header
option, after initialization:
1
2
3
4
5
|
|
Initialize the accordion with the header
option specified as a function:
1
2
3
4
5
|
|
heightStyle
"auto"
Controls the height of the accordion and each panel. Possible values:
-
"auto"
: All panels will be set to the height of the tallest panel. -
"fill"
: Expand to the available height based on the accordion's parent height. -
"content"
: Each panel will be only as tall as its content.
Initialize the accordion with the heightStyle
option specified:
1
2
3
|
|
Get or set the heightStyle
option, after initialization:
1
2
3
4
5
|
|
icons
|
Icons to use for headers, matching an icon provided by the jQuery UI CSS Framework. Set to false
to have no icons displayed.
- header (string, default: "ui-icon-triangle-1-e")
- activeHeader (string, default: "ui-icon-triangle-1-s")
Initialize the accordion with the icons
option specified:
1
2
3
|
|
Get or set the icons
option, after initialization:
1
2
3
4
5
|
|
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
instance()Returns: Object
Retrieves the accordion's instance object. If the element does not have an associated instance, undefined
is returned.
Unlike other widget methods, instance()
is safe to call on any element after the accordion plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
1
|
|
option( optionName )Returns: Object
Gets the value currently associated with the specified optionName
.
Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar"
would get the value of the bar
property on the foo
option.
-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
option( optionName, value )Returns: jQuery (plugin only)
Sets the value of the accordion option associated with the specified optionName
.
Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName
. For example, "foo.bar"
would update only the bar
property of the foo
option.
-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
refresh()Returns: jQuery (plugin only)
heightStyle
option.- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
widget()Returns: jQuery
jQuery
object containing the accordion.
- This method does not accept any arguments.
Invoke the widget method:
1
|
|
Events
activate( event, ui )Type: accordionactivate
Triggered after a panel has been activated (after animation completes). If the accordion was previously collapsed, ui.oldHeader
and ui.oldPanel
will be empty jQuery objects. If the accordion is collapsing, ui.newHeader
and ui.newPanel
will be empty jQuery objects.
activate
event is only fired on panel activation, it is not fired for the initial panel when the accordion widget is created. If you need a hook for widget creation use the create
event.Initialize the accordion with the activate callback specified:
1
2
3
|
|
Bind an event listener to the accordionactivate event:
1
|
|
beforeActivate( event, ui )Type: accordionbeforeactivate
ui.oldHeader
and ui.oldPanel
will be empty jQuery objects. If the accordion is collapsing, ui.newHeader
and ui.newPanel
will be empty jQuery objects.Initialize the accordion with the beforeActivate callback specified:
1
2
3
|
|
Bind an event listener to the accordionbeforeactivate event:
1
|
|
create( event, ui )Type: accordioncreate
ui.header
and ui.panel
will be empty jQuery objects.Initialize the accordion with the create callback specified:
1
2
3
|
|
Bind an event listener to the accordioncreate event:
1
|
|
Example:
A simple jQuery UI Accordion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
|