Accordion.JS

A free, lightweight jQuery Accordion plugin. It is only ~1KB gzipped. Configurable using direct options and HTML5 data-* attributes.
View on Github Download

Installation

Step by step install the plugin.

Install with npm


npm install accordionjs --save
		

or

Install with yarn


yarn add accordionjs
		

or

Manual install

Download the most recent version from Github and include the unzipped files in your project. Use the top link on this page.

Getting started

Step by step create the first accordion instance.

jQuery: Make sure that jQuery library is installed. If it is not that add the following line:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
		

CSS: Next include the CSS in document <head>:


<link rel="stylesheet" href="accordion.css" />
		

JS: And the JS just before the closing </body> tag:


<script src="accordion.min.js"></script>
		

Init: After this line add the following code to create the first instance:


<script>
    jQuery(document).ready(function($){
        $("#my-accordion").accordionjs();
    });
</script>
		

HTML: In this example we used #my-accordion as the ID for our new accordion(css class selectors works as well). That said we must add the HTML:


<ul id="my-accordion" class="accordionjs">

    <!-- Section 1 -->
    <li>
        <div>Section 1 title</div>
        <div>
            <!-- Section content here. -->
        </div>
    </li>

    <!-- Section 2 -->
    <li>
        <div>Section 2 title</div>
        <div>
            <!-- Section content here. -->
        </div>
    </li>

    <!-- Section 3 -->
    <li>
        <div>Section 3 title</div>
        <div>
            <!-- Section content here. -->
        </div>
    </li>

</ul>
		

Options

The plugin contains a few options that can be defined directly in plugin call or using data-* attributes:


<script>
    $("#my-accordion").accordionjs({
        // Allow self close.(data-close-able)
        closeAble   : false,

        // Close other sections.(data-close-other)
        closeOther  : true,

        // Animation Speed.(data-slide-speed)
        slideSpeed  : 150,

        // The section open on first init. A number from 1 to X or false.(data-active-index)
        activeIndex : 1,

        // Callback when a section is open
        openSection: function( section ){},

        // Callback before a section is open
        beforeOpenSection: function( section ){},
    });
</script>
		

Examples

A few examples that demonstrates the plugin's power.

Default example

This is the default example with default settings.

Second section active

The second section is open by default when activeIndex is set to 2.

Example 2

This example has no active section. When activeIndex is false, closeAble is automatically set to true.

Multiple sections open by default

activeIndex may be an array of integers. For example if you pass [2, 4], section 2 and 4 will be open by default.

Single section

When the accordion contains only one section it will be open by default and closeAble set to true

Single section, closed

To make it closed by default, set activeIndex to false

Do no close other sections

Set closeOther to false, so when a section is opened other section are not closed. This automatically set closeAble to true

Example 9( nested accordions)