Latest web development tutorials

jQuery UI API - folding panel member (Accordion Widget)

category

Widgets (Widgets)

usage

Description: converting one pairs title and content of the panel is folded into a panel.

New Version: 1.0

Folding panel labeled containers need a title and content of the panel.

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

Folding panel support any tag, but the contents of each panel must be at the same level associated with a head panel. See the header options on how to use the custom tag structure.

Panel can be set active programmatically activate the option.

Keyboard interaction

When the focus is in the title (header) on, the following keyboard commands are available:

  • UP / LEFT - Move cursor to a title (header). If the first title (header), the focus will move to the last title (header) on.
  • DOWN / RIGHT - Move cursor to the next title (header). If at the end of a title (header), the focus will move to the first title (header) on.
  • HOME - Move cursor to the first title (header) on.
  • END - Move cursor to the last title (header) on.
  • SPACE / ENTER - activates the focused title (header) associated panel (panel).

When the (panel) when the focus is on the panel, the following keyboard commands are available:

  • CTRL + UP: Move cursor to the relevant title (header).

Theming

Folding panel member (Accordion Widget) using jQuery UI CSS framework to define the look and feel of its style. If you need to use the specified style folding panel, you can use the following CSS class name:

  • ui-accordion : folding panel outer container.
    • ui-accordion-header : accordion title. If the title contains icons , the title would otherwise have a ui-accordion-icons , class.
    • ui-accordion-content : accordion content panels.

rely

Additional information

  • The part requires some functional CSS, otherwise it will not work. If you create a custom theme, use the widget specified CSS file as a starting point.

Quick navigation

选项 方法 事件

选项 类型 描述 默认值
active Boolean 或 Integer 当前打开哪一个面板。

支持多个类型:

  • Boolean :设置 activefalse 将折叠所有的面板。这要求 collapsible 选项必须为 true
  • Integer :激活打开的面板索引,以零为基础。负值则表示从最后一个面板后退选择面板。

代码实例:

初始化带有指定 active 选项的 accordion:

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

在初始化后,获取或设置 active 选项:

// getter
var active = $( ".selector" ).accordion( "option", "active" );
 
// setter
$( ".selector" ).accordion( "option", "active", 2 );
	
0
animate Boolean 或 Number 或 String 或 Object 是否使用动画改变面板,且如何使用动画改变面板。

支持多个类型:

  • Booleanfalse 值将禁用动画。
  • Number :easing 默认的持续时间,以毫秒计。
  • String :默认的持续时间要使用的 easing 名称。
  • Objecteasingduration 属性的动画设置。
    • 上面任意的选项都可以包含 down 属性。
    • 当被激活的面板有一个比当前激活面板较低的指数时,发生 "Down" 动画。

代码实例:

初始化带有指定 animate 选项的 accordion:

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

在初始化后,获取或设置 animate 选项:

// getter
var animate = $( ".selector" ).accordion( "option", "animate" );
 
// setter
$( ".selector" ).accordion( "option", "animate", "bounceslide" );
	
{}
collapsible Boolean 所有部分是否都可以马上关闭。允许折叠激活的部分。

代码实例:

初始化带有指定 collapsible 选项的 accordion:

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

在初始化后,获取或设置 collapsible 选项:

// getter
var collapsible = $( ".selector" ).accordion( "option", "collapsible" );
 
// setter
$( ".selector" ).accordion( "option", "collapsible", true );
	
false
disabled Boolean 如果设置为 true ,则禁用该 accordion。

代码实例:

初始化带有指定 disabled 选项的 accordion:

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

在初始化后,获取或设置 disabled 选项:

// getter
var disabled = $( ".selector" ).accordion( "option", "disabled" );
 
// setter
$( ".selector" ).accordion( "option", "disabled", true );
	
false
event String accordion 头部会作出反应的事件,用以激活相关的面板。可以指定多个事件,用空格间隔。

代码实例:

初始化带有指定 event 选项的 accordion:

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

在初始化后,获取或设置 event 选项:

// getter
var event = $( ".selector" ).accordion( "option", "event" );
 
// setter
$( ".selector" ).accordion( "option", "event", "mouseover" );
	
"click"
header Selector 标题元素的选择器,通过主要 accordion 元素上的 .find() 进行应用。内容面板必须是紧跟在与其相关的标题后的同级元素。

代码实例:

初始化带有指定 header 选项的 accordion:

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

在初始化后,获取或设置 header 选项:

// getter
var header = $( ".selector" ).accordion( "option", "header" );
 
// setter
$( ".selector" ).accordion( "option", "header", "h3" );
	
"> li > :first-child,> :not(li):even"
heightStyle String 控制 accordion 和每个面板的高度。可能的值:
  • "auto" :所有的面板将会被设置为最高的面板的高度。
  • "fill" :基于 accordion 的父元素的高度,扩展到可用的高度。
  • "content" :每个面板的高度取决于它的内容。

代码实例:

初始化带有指定 heightStyle 选项的 accordion:

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

在初始化后,获取或设置 heightStyle 选项:

// getter
var heightStyle = $( ".selector" ).accordion( "option", "heightStyle" );
 
// setter
$( ".selector" ).accordion( "option", "heightStyle", "fill" );
	
"auto"
icons Object 标题要使用的图标,与 jQuery UI CSS 框架提供的图标(Icons) 匹配。设置为 false 则不显示图标。
  • header (string,默认值:"ui-icon-triangle-1-e")
  • activeHeader (string,默认值:"ui-icon-triangle-1-s")

代码实例:

初始化带有指定 icons 选项的 accordion:

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

在初始化后,获取或设置 icons 选项:

// getter
var icons = $( ".selector" ).accordion( "option", "icons" );
 
// setter
$( ".selector" ).accordion( "option", "icons", { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } );
	
{ "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }

方法 返回 描述
destroy() jQuery (plugin only) 完全移除 accordion 功能。这会把元素返回到它的预初始化状态。
  • 该方法不接受任何参数。

代码实例:

调用 destroy 方法:

$( ".selector" ).accordion( "destroy" );
	
disable() jQuery (plugin only) 禁用 accordion。
  • 该方法不接受任何参数。

代码实例:

调用 disable 方法:

$( ".selector" ).accordion( "disable" );
	
enable() jQuery (plugin only) 启用 accordion。
  • 该方法不接受任何参数。

代码实例:

调用 enable 方法:

$( ".selector" ).accordion( "enable" );
	
option( optionName ) Object 获取当前与指定的 optionName 关联的值。
  • optionName
    类型:String
    描述:要获取的选项的名称。

代码实例:

调用该方法:

var isDisabled = $( ".selector" ).accordion( "option", "disabled" );
	
option() PlainObject 获取一个包含键/值对的对象,键/值对表示当前 accordion 选项哈希。
  • 该方法不接受任何参数。

代码实例:

调用该方法:

var options = $( ".selector" ).accordion( "option" );
	
option( optionName, value ) jQuery (plugin only) 设置与指定的 optionName 关联的 accordion 选项的值。
  • optionName
    类型:String
    描述:要设置的选项的名称。
  • value
    类型:Object
    描述:要为选项设置的值。

代码实例:

调用该方法:

$( ".selector" ).accordion( "option", "disabled", true );
	
option( options ) jQuery (plugin only) 为 accordion 设置一个或多个选项。
  • options
    类型:Object
    描述:要设置的 option-value 对。

代码实例:

调用该方法:

$( ".selector" ).accordion( "option", { disabled: true } );
	
refresh() jQuery (plugin only) 处理任何在 DOM 中直接添加或移除的标题和面板,并重新计算 accordion 的高度。结果取决于内容和 heightStyle 选项。
  • 该方法不接受任何参数。

代码实例:

调用 refresh 方法:

$( ".selector" ).accordion( "refresh" );
	
widget() jQuery 返回一个包含 accordion 的 jQuery 对象。
  • 该方法不接受任何参数。

代码实例:

调用 widget 方法:

var widget = $( ".selector" ).accordion( "widget" );
	

事件 类型 描述
activate( event, ui ) accordionactivate 面板被激活后触发(在动画完成之后)。如果 accordion 之前是折叠的,则 ui.oldHeaderui.oldPanel 将是空的 jQuery 对象。如果 accordion 正在折叠,则 ui.newHeaderui.newPanel 将是空的 jQuery 对象。

注意:由于 activate 事件只有在面板激活时才能触发,当创建 accordion 部件时,最初的面板不会触发该事件。如果您需要一个用于部件创建的钩,请使用 create 事件。

  • event
    类型:Event
  • ui
    类型:Object
    • newHeader
      类型:jQuery
      描述:刚被激活的标题。
    • oldHeader
      类型:jQuery
      描述:刚被取消激活的标题。
    • newPanel
      类型:jQuery
      描述:刚被激活的面板。
    • oldPanel
      类型:jQuery
      描述:刚被取消激活的面板。

代码实例:

初始化带有指定 activate 回调的 accordion:

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

绑定一个事件监听器到 accordionactivate 事件:

$( ".selector" ).on( "accordionactivate", function( event, ui ) {} );
	
beforeActivate( event, ui ) accordionbeforeactivate 面板被激活前直接触发。可以取消以防止面板被激活。如果 accordion 当前是折叠的,则 ui.oldHeaderui.oldPanel 将是空的 jQuery 对象。如果 accordion 正在折叠,则 ui.newHeaderui.newPanel 将是空的 jQuery 对象。
  • event
    类型:Event
  • ui
    类型:Object
    • newHeader
      类型:jQuery
      描述:将被激活的标题。
    • oldHeader
      类型:jQuery
      描述:将被取消激活的标题。
    • newPanel
      类型:jQuery
      描述:将被激活的面板。
    • oldPanel
      类型:jQuery
      描述:将被取消激活的面板。

代码实例:

初始化带有指定 beforeActivate 回调的 accordion:

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

绑定一个事件监听器到 accordionbeforeactivate 事件:

$( ".selector" ).on( "accordionbeforeactivate", function( event, ui ) {} );
	
create( event, ui ) accordioncreate 当创建 accordion 时触发。如果 accordion 是折叠的, ui.headerui.panel 将是空的 jQuery 对象。
  • event
    类型:Event
  • ui
    类型:Object
    • header
      类型:jQuery
      描述:激活的标题。
    • panel
      类型:jQuery
      描述:激活的面板。

代码实例:

初始化带有指定 create 回调的 accordion:

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

绑定一个事件监听器到 accordioncreate 事件:

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

Examples

A simple jQuery UI accordion (Accordion).

<! Doctype html>
<Html lang = "en">
<Head>
  <Meta charset = "utf-8">
  <Title> Folding panel member (Accordion Widget) Demo </ title>
  <Link rel = "stylesheet" href = "// code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <Script src = "// code.jquery.com/jquery-1.10.2.js"> </ script>
  <Script src = "// code.jquery.com/ui/1.10.4/jquery-ui.js"> </ script>
</ Head>
<Body>
 
<Div id = "accordion">
  <H3> Part 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> Part 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>