Accordion

Convert a pair of headers and content panels into an accordion.

The markup of your accordion container needs pairs of headers and content panels:

<div id="accordion">
  <h3>First header</h3>
  <div>First content panel</div>
  <h3>Second header</h3>
  <div>Second content panel</div>
</div>

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:

  • ui-accordion: The outer container of the accordion.
    • ui-accordion-header: The headers of the accordion. The headers will additionally have a ui-accordion-icons class if they contain icons.
    • ui-accordion-content: The content panels of the accordion.

Dependencies

  • 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.
active
Type: Boolean or Integer
Default: 0
Which panel is currently open.

Multiple types supported:

  • Boolean: Setting active to false will collapse all panels. This requires the collapsible option to be true.
  • Integer: The zero-based index of the panel that is active (open). A negative value selects panels going backward from the last panel.

Code examples:

Initialize the accordion with the active option specified:

$( ".selector" ).accordion({
  active: 2
});

Get or set the active option, after initialization:

// Getter
var active = $( ".selector" ).accordion( "option", "active" );
 
// Setter
$( ".selector" ).accordion( "option", "active", 2 );
animate
Type: Boolean or Number or String or Object
Default: {}
If and how to animate changing panels.

Multiple types supported:

  • 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 and duration 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.

Code examples:

Initialize the accordion with the animate option specified:

$( ".selector" ).accordion({
  animate: 200
});

Get or set the animate option, after initialization:

// Getter
var animate = $( ".selector" ).accordion( "option", "animate" );
 
// Setter
$( ".selector" ).accordion( "option", "animate", 200 );
collapsible
Type: Boolean
Default: false
Whether all the sections can be closed at once. Allows collapsing the active section.

Code examples:

Initialize the accordion with the collapsible option specified:

$( ".selector" ).accordion({
  collapsible: true
});

Get or set the collapsible option, after initialization:

// Getter
var collapsible = $( ".selector" ).accordion( "option", "collapsible" );
 
// Setter
$( ".selector" ).accordion( "option", "collapsible", true );
disabled
Type: Boolean
Default: false
Disables the accordion if set to true.

Code examples:

Initialize the accordion with the disabled option specified:

$( ".selector" ).accordion({
  disabled: true
});

Get or set the disabled option, after initialization:

// Getter
var disabled = $( ".selector" ).accordion( "option", "disabled" );
 
// Setter
$( ".selector" ).accordion( "option", "disabled", true );
event
Type: String
Default: "click"
The event that accordion headers will react to in order to activate the associated panel. Multiple events can be specified, separated by a space.

Code examples:

Initialize the accordion with the event option specified:

$( ".selector" ).accordion({
  event: "mouseover"
});

Get or set the event option, after initialization:

// Getter
var event = $( ".selector" ).accordion( "option", "event" );
 
// Setter
$( ".selector" ).accordion( "option", "event", "mouseover" );
header
Type: Selector
Default: "> li > :first-child,> :not(li):even"

Selector for the header element, applied via .find() on the main accordion element. Content panels must be the sibling immediately after their associated headers.

Code examples:

Initialize the accordion with the header option specified:

$( ".selector" ).accordion({
  header: "h3"
});

Get or set the header option, after initialization:

// Getter
var header = $( ".selector" ).accordion( "option", "header" );
 
// Setter
$( ".selector" ).accordion( "option", "header", "h3" );
heightStyle
Type: String
Default: "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.

Code examples:

Initialize the accordion with the heightStyle option specified:

$( ".selector" ).accordion({
  heightStyle: "fill"
});

Get or set the heightStyle option, after initialization:

// Getter
var heightStyle = $( ".selector" ).accordion( "option", "heightStyle" );
 
// Setter
$( ".selector" ).accordion( "option", "heightStyle", "fill" );
icons
Type: Object
Default: { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }

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")

Code examples:

Initialize the accordion with the icons option specified:

$( ".selector" ).accordion({
  icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" }
});

Get or set the icons option, after initialization:

// Getter
var icons = $( ".selector" ).accordion( "option", "icons" );
 
// Setter
$( ".selector" ).accordion( "option", "icons", { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } );
destroy()Returns: jQuery (plugin only)
Removes the accordion functionality completely. This will return the element back to its pre-init state.
  • This method does not accept any arguments.
Code examples:

Invoke the destroy method:

$( ".selector" ).accordion( "destroy" );
disable()Returns: jQuery (plugin only)
Disables the accordion.
  • This method does not accept any arguments.
Code examples:

Invoke the disable method:

$( ".selector" ).accordion( "disable" );
enable()Returns: jQuery (plugin only)
Enables the accordion.
  • This method does not accept any arguments.
Code examples:

Invoke the enable method:

$( ".selector" ).accordion( "enable" );
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.
Code examples:

Invoke the instance method:

$( ".selector" ).accordion( "instance" );
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.

  • optionName
    Type: String
    The name of the option to get.
Code examples:

Invoke the method:

var isDisabled = $( ".selector" ).accordion( "option", "disabled" );
option()Returns: PlainObject
Gets an object containing key/value pairs representing the current accordion options hash.
  • This signature does not accept any arguments.
Code examples:

Invoke the method:

var options = $( ".selector" ).accordion( "option" );
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.

  • optionName
    Type: String
    The name of the option to set.
  • value
    Type: Object
    A value to set for the option.
Code examples:

Invoke the method:

$( ".selector" ).accordion( "option", "disabled", true );
option( options )Returns: jQuery (plugin only)
Sets one or more options for the accordion.
  • options
    Type: Object
    A map of option-value pairs to set.
Code examples:

Invoke the method:

$( ".selector" ).accordion( "option", { disabled: true } );
refresh()Returns: jQuery (plugin only)
Process any headers and panels that were added or removed directly in the DOM and recompute the height of the accordion panels. Results depend on the content and the heightStyle option.
  • This method does not accept any arguments.
Code examples:

Invoke the refresh method:

$( ".selector" ).accordion( "refresh" );
widget()Returns: jQuery
Returns a jQuery object containing the accordion.
  • This method does not accept any arguments.
Code examples:

Invoke the widget method:

var widget = $( ".selector" ).accordion( "widget" );
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.

Note: Since the 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.
  • event
    Type: Event
  • ui
    Type: Object
    • newHeader
      Type: jQuery
      The header that was just activated.
    • oldHeader
      Type: jQuery
      The header that was just deactivated.
    • newPanel
      Type: jQuery
      The panel that was just activated.
    • oldPanel
      Type: jQuery
      The panel that was just deactivated.
Code examples:

Initialize the accordion with the activate callback specified:

$( ".selector" ).accordion({
  activate: function( event, ui ) {}
});

Bind an event listener to the accordionactivate event:

$( ".selector" ).on( "accordionactivate", function( event, ui ) {} );
beforeActivate( event, ui )Type: accordionbeforeactivate
Triggered directly before a panel is activated. Can be canceled to prevent the panel from activating. 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
      The header that is about to be activated.
    • oldHeader
      Type: jQuery
      The header that is about to be deactivated.
    • newPanel
      Type: jQuery
      The panel that is about to be activated.
    • oldPanel
      Type: jQuery
      The panel that is about to be deactivated.
Code examples:

Initialize the accordion with the beforeActivate callback specified:

$( ".selector" ).accordion({
  beforeActivate: function( event, ui ) {}
});

Bind an event listener to the accordionbeforeactivate event:

$( ".selector" ).on( "accordionbeforeactivate", function( event, ui ) {} );
create( event, ui )Type: accordioncreate
Triggered when the accordion is created. If the accordion is collapsed, ui.header and ui.panel will be empty jQuery objects.
Code examples:

Initialize the accordion with the create callback specified:

$( ".selector" ).accordion({
  create: function( event, ui ) {}
});

Bind an event listener to the accordioncreate event:

$( ".selector" ).on( "accordioncreate", function( event, ui ) {} );
Examples:

A simple jQuery UI Accordion

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>accordion demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
 
<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>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.</p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus.
    Vivamus hendrerit, dolor aliquet laoreet, mauris turpis velit,
    faucibus interdum tellus libero ac justo.</p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus.
    Quisque lobortis.Phasellus pellentesque purus in massa.</p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
</div>
 
<script>
$( "#accordion" ).accordion();
</script>
 
</body>
</html>
doc_jQuery
2016-03-28 14:47:40
Comments
Leave a Comment

Please login to continue.