jQuery UI API - Accordion Widget
Category
Usage
**Description:** Turns a pair of header and content panels into an accordion.
**Version added:** 1.0
The markup of the accordion container requires a pair of header and content panels.
First header
First content panel
Second header
Second content panel
The accordion supports arbitrary markup, but each content panel must be the next sibling of its associated header panel. See header the options for how to use a custom markup structure.
Panels can be activated programmatically by setting the active option.
Keyboard interaction
When focus is on a header, the following keyboard commands are available:
- UP/LEFT - Moves focus to the previous header. If on the first header, moves focus to the last header.
- DOWN/RIGHT - Moves focus to the next header. If on the last header, moves focus to the first header.
- HOME - Moves focus to the first header.
- END - Moves focus to the last header.
- SPACE/ENTER - Activates the panel associated with the focused header.
When focus is on a panel, the following keyboard commands are available:
- CTRL+UP: Moves focus to the associated header.
Theming
The Accordion widget uses the jQuery UI CSS Framework to define its look-and-feel styles. If you need to use accordion-specific styles, the following CSS class names are available:
ui-accordion: the outer container of the accordion.ui-accordion-header: the header of the accordion. If the header containsicons, the header will additionally have aui-accordion-iconsclass。ui-accordion-content: the content panel of the accordion.
Depends on
- UI Core
- Widget Factory
- Effects Core(optional; when used with the
animateoption)
Additional Notes
- This widget requires some functional CSS; otherwise it will not work. If you create a custom theme, use the widget-specific CSS file as a starting point.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| active | Boolean or Integer | Which panel is currently open. |
支持多个类型:
active is false Collapses all panels. This requires the collapsible option to be true。Code example:
Initialize the accordion with the specified active option:
$( ".selector" ).accordion({ active: 2 });
After initialization, get or set the active Option:
// getter var active = $( ".selector" ).accordion( "option", "active" );| 0| onSelect | event | animate | Boolean or Number or String or Object | Whether to use animation to change panels, and how to animate the change.// setter $( ".selector" ).accordion( "option", "active", 2 );
Multiple types are supported:
- Boolean:
falsevalue disables animation. - Number: the default duration for easing, in milliseconds.
- String: the easing name to use for the default duration easing name.
- Object:
easinganddurationthe animation settings for the property. - Any of the options above can contain
downattribute. - A "Down" animation occurs when the activated panel has a lower index than the currently active panel.
Code example:
Initialize the accordion with the specified animate option:
$( ".selector" ).accordion({ animate: "bounceslide" });
After initialization, get or set the animate Option:
// getter var animate = $( ".selector" ).accordion( "option", "animate" );| {}| onSelect | event | collapsible | Boolean | Whether all sections can be closed immediately. Allows the active section to be collapsed.// setter $( ".selector" ).accordion( "option", "animate", "bounceslide" );
Code example:
Initialize the accordion with the specified collapsible option:
$( ".selector" ).accordion({ collapsible: true });
After initialization, get or set the collapsible Option:
// getter var collapsible = $( ".selector" ).accordion( "option", "collapsible" );| false| onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).accordion( "option", "collapsible", true );
true, the accordion is disabled.
Code example:
Initialize the accordion with the specified disabled option:
$( ".selector" ).accordion({ disabled: true });
After initialization, get or set the disabled Option:
// getter var disabled = $( ".selector" ).accordion( "option", "disabled" );| false| onSelect | event | event | String | The event the accordion header responds to in order to activate the associated panel. Multiple events can be specified, separated by spaces.// setter $( ".selector" ).accordion( "option", "disabled", true );
Code example:
Initialize the accordion with the specified event option:
$( ".selector" ).accordion({ event: "mouseover" });
After initialization, get or set the event Option:
// getter var event = $( ".selector" ).accordion( "option", "event" );| "click"| onSelect | event | header | Selector | Selector for the header elements, applied via .find() on the main accordion element. A content panel must be a sibling immediately following its associated header.// setter $( ".selector" ).accordion( "option", "event", "mouseover" );
Code example:
Initialize the accordion with the specified header option:
$( ".selector" ).accordion({ header: "h3" });
After initialization, get or set the header Option:
// getter var header = $( ".selector" ).accordion( "option", "header" );| "> li > :first-child,> :not(li):even"| onSelect | event | heightStyle | String | Controls the height of the accordion and each panel. Possible values:// setter $( ".selector" ).accordion( "option", "header", "h3" );
"auto": all panels are set to the height of the tallest panel."fill": expands to the available height based on the accordion's parent height."content": each panel's height depends on its content.
Code example:
Initialize the accordion with the specified heightStyle option:
$( ".selector" ).accordion({ heightStyle: "fill" });
After initialization, get or set the heightStyle Option:
// getter var heightStyle = $( ".selector" ).accordion( "option", "heightStyle" );| "auto"| onSelect | event | icons | Object | Icons to use for the headers, matching the Icons provided by the jQuery UI CSS Framework matching. Set to// setter $( ".selector" ).accordion( "option", "heightStyle", "fill" );
false to display no icons.
- header (string, default: "ui-icon-triangle-1-e")
- activeHeader (string, default: "ui-icon-triangle-1-s")
Code example:
Initialize the accordion with the specified icons option:
$( ".selector" ).accordion({ icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } });
After initialization, get or set the icons Option:
// getter var icons = $( ".selector" ).accordion( "option", "icons" );| { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" } |// setter $( ".selector" ).accordion( "option", "icons", { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely removes the accordion functionality. This returns the element to its pre-initialization state. |
Code example:
Invoke the destroy method:
$( ".selector" ).accordion( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disables the accordion.
- This method accepts no arguments.
Code example:
Invoke the disable method:
$( ".selector" ).accordion( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enables the accordion.
- This method accepts no arguments.
Code example:
Invoke the enable method:
$( ".selector" ).accordion( "enable" );| onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to get.
Code example:
Invoke the method:
var isDisabled = $( ".selector" ).accordion( "option", "disabled" );| onSelect | event | option() | PlainObject | Gets an object containing key/value pairs representing the current accordion options hash.
- This method accepts no arguments.
Code example:
Invoke the method:
var options = $( ".selector" ).accordion( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the accordion option associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to set. - value
Type: Object
Description: the value to set for the option.
Code example:
Invoke the method:
$( ".selector" ).accordion( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the accordion.
- options
Type: Object
Description: the option-value pairs to set.
Code example:
Invoke the method:
$( ".selector" ).accordion( "option", { disabled: true } );
| onSelect | event | refresh() | jQuery (plugin only) | Processes any headers and panels added or removed directly in the DOM, and recalculates the height of the accordion. The result depends on the content and the `heightStyle` option.
- This method accepts no arguments.
Code example:
Invoke the refresh method:
$( ".selector" ).accordion( "refresh" );| onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the accordion.
- This method accepts no arguments.
Code example:
Invoke the widget method:
var widget = $( ".selector" ).accordion( "widget" );|
| Events | Type | Description |
|---|---|---|
| activate( event, ui ) | accordionactivate | Triggered after a panel is activated (after the animation completes). If the accordion was collapsed, then ui.oldHeader and ui.oldPanel will be an empty jQuery object. If the accordion is collapsing, then ui.newHeader and ui.newPanel will be an empty jQuery object. |
注意:由于 activate 事件只有在面板激活时才能触发,当创建 accordion 部件时,最初的面板不会触发该事件。如果您需要一个用于部件创建的钩,请使用 create 事件。
Type: Event
Type: Object
- newHeader
Type: jQuery
Description: the header that was just activated. - oldHeader
Type: jQuery
Description: the header that was just deactivated. - newPanel
Type: jQuery
Description: the panel that was just activated. - oldPanel
Type: jQuery
Description: the panel that was just deactivated.
Code example:
Initialize the accordion with the specified activate callback:
$( ".selector" ).accordion({
activate: function( event, ui ) {}
});
Bind an event listener to the accordionactivate event:
$( ".selector" ).on( "accordionactivate", function( event, ui ) {} );
| onSelect | event | beforeActivate( event, ui ) | accordionbeforeactivate | Triggered directly before a panel is activated. Can be canceled to prevent the panel from being activated. If the accordion is currently 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.
- event
Type: Event - ui
Type: Object- newHeader
Type: jQuery
Description: the header to be activated. - oldHeader
Type: jQuery
Description: the header to be deactivated. - newPanel
Type: jQuery
Description: the panel to be activated. - oldPanel
Type: jQuery
Description: the panel to be deactivated.
- newHeader
Code example:
Initialize the accordion with the specified beforeActivate callback:
$( ".selector" ).accordion({
beforeActivate: function( event, ui ) {}
});
Bind an event listener to the accordionbeforeactivate event:
$( ".selector" ).on( "accordionbeforeactivate", function( event, ui ) {} );
| onSelect | event | create( event, ui ) | accordioncreate | Triggered when the accordion is created. If the accordion is collapsed, `ui.header` and `ui.panel` will be empty jQuery objects.
- event
Type: Event - ui
Type: Object- header
Type: jQuery
Description: the active header. - panel
Type: jQuery
Description: the active panel.
- header
Code example:
Initialize the accordion with the specified create callback:
$( ".selector" ).accordion({
create: function( event, ui ) {}
});
Bind an event listener to the accordioncreate event:
$( ".selector" ).on( "accordioncreate", function( event, ui ) {} );
|
Example
A simple jQuery UI accordion.
折叠面板部件(Accordion Widget)演示
部分 1
Mauris mauris ante, blandit et, ultrices a, suscipit eget.
Integer ut neque. Vivamus nisi metus, molestie vel, gravida in,
condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros.
Nam mi. Proin viverra leo ut odio.
部分 2
Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus.
Vivamus hendrerit, dolor aliquet laoreet, mauris turpis velit,
faucibus interdum tellus libero ac justo.
部分 3
Nam enim risus, molestie et, porta ac, aliquam ac, risus.
Quisque lobortis.Phasellus pellentesque purus in massa.
- List item one
- List item two
- List item three