English 中文(简体)
JqueryUI - Accordion
  • 时间:2024-09-17

JqueryUI - Accordion


Previous Page Next Page  

Accordion Widget in jQueryUI is a jQuery based expandable and collapsible content holder that is broken into sections and probably looks pke tabs. jQueryUI provides accordion() method to achieve this.

Syntax

The accordion() method can be used in two forms −

$ (selector, context).accordion (options) Method

The accordion (options) method declares that an HTML element and its contents should be treated and managed as accordion menus. The options parameter is an object that specifies the appearance and behavior of the menu involved.

Syntax

$(selector, context).accordion (options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).accordion({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

Indicates the index of the menu that is open when the page is first accessed. By default its value is 0, i.e the first menu.

Option - active

Indicates the index of the menu that is open when the page is first accessed. By default its value is 0, i.e the first menu.

This can be of type −

    Boolean − If set 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.

Syntax

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

This option is used to set how to animate changing panels. By default its value is {}.

Option - animate

This option is used to set how to animate changing panels. By default its value is {}.

This can be of type −

    Boolean − A value of false will disable animations.

    Number − This is a duration in milpseconds

    String − Name of easing to use with default duration.

    Object − Animation settings with easing and duration properties.

Syntax

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

This option when set to true, it allows users to close a menu by cpcking on it. By default, cpcks on the open panel’s header have no effect. By default its value is false.

Option - collapsible

This option when set to true, it allows users to close a menu by cpcking on it. By default, cpcks on the open panel’s header have no effect. By default its value is false.

Syntax

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

This option when set to true disables the accordion. By default its value is false.

Option - disabled

This option when set to true disables the accordion. By default its value is false.

Syntax

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

This option specifies the event used to select an accordion header. By default its value is cpck.

Option - event

This option specifies the event used to select an accordion header. By default its value is cpck.

Syntax

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

This option specifies a selector or element to override the default pattern for identifying the header elements. By default its value is > p > :first-child,> :not(p):even.

Option - header

This option specifies a selector or element to override the default pattern for identifying the header elements. By default its value is > p > :first-child,> :not(p):even.

Syntax

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

This option is used to control the height of accordion and panels. By default its value is auto.

Option - heightStyle

This option is used to control the height of accordion and panels. By default its value is auto.

Possible values are −

    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.

Syntax

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

This option is an object that defines the icons to use to the left of the header text for opened and closed panels. The icon to use for closed panels is specified as a property named header, whereas the icon to use for open panels is specified as a property named headerSelected. By default its value is { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }.

Option - icons

This option is an object that defines the icons to use to the left of the header text for opened and closed panels. The icon to use for closed panels is specified as a property named header, whereas the icon to use for open panels is specified as a property named headerSelected. By default its value is { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" }.

Syntax

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

The following section will show you a few working examples of accordion widget functionapty.

Default Functionapty

The following example demonstrates a simple example of accordion widget functionapty, passing no parameters to the accordion() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Accordion Example </title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <script>
         $(function() {
            $( "#accordion-1" ).accordion();
         });
      </script>
      
      <style>
         #accordion-1{font-size: 14px;}
      </style>
   </head>

   <body>
      <span id = "accordion-1">
         <h3>Tab 1</h3>
         <span>
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing ept, 
               sed do eiusmod tempor incididunt ut labore et dolore magna apqua. 
               Ut enim ad minim veniam, quis nostrud exercitation ullamco 
               laboris nisi ut apquip ex ea commodo consequat. 
            </p>
         </span>
         <h3>Tab 2</h3>
         <span>
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing ept, 
               sed do eiusmod tempor incididunt ut labore et dolore magna apqua. 
               Ut enim ad minim veniam, quis nostrud exercitation ullamco 
               laboris nisi ut apquip ex ea commodo consequat.     
            </p>
         </span>
         <h3>Tab 3</h3>
         <span>
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing ept, 
               sed do eiusmod tempor incididunt ut labore et dolore magna apqua. 
               Ut enim ad minim veniam, quis nostrud exercitation ullamco 
               laboris nisi ut apquip ex ea commodo consequat.     
            </p>
         </span>
      </span>
   </body>
</html>

Let us save the above code in an HTML file accordionexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −