Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Accordion module created in pure javascript & CSS. Very useful to create FAQ lists on your website.

License

NotificationsYou must be signed in to change notification settings

michu2k/Accordion

Repository files navigation

Lightweight and accessible accordion module with an extensible API. With the module you can create accordion on your website, useful especially for creating FAQ lists.

Version

3.4.0

Installation

npm

Install the package & import files

npm install accordion-js
importAccordionfrom"accordion-js";import"accordion-js/dist/accordion.min.css";
CDN

Include files using CDN.

https://unpkg.com/accordion-js@3.4.0/dist/accordion.min.csshttps://unpkg.com/accordion-js@3.4.0/dist/accordion.min.js
<linkrel="stylesheet"href="[CDN CSS URL]"/><scriptsrc="[CDN JS URL]"></script>
Github

You can also download files from Github and attach them manually to your project.
Note: On production use files (JS and CSS) only fromdist/ folder.

Usage

Include files

See the section above.

Create HTML layout

This is just an example of a layout. You can create your own HTML structure.

<divclass="accordion-container"><divclass="ac"><h2class="ac-header"><buttontype="button"class="ac-trigger">Lorem ipsum dolor sit amet.</button></h2><divclass="ac-panel"><pclass="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div></div><divclass="ac"><h2class="ac-header"><buttontype="button"class="ac-trigger">Lorem ipsum dolor sit amet.</button></h2><divclass="ac-panel"><pclass="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div></div><divclass="ac"><h2class="ac-header"><buttontype="button"class="ac-trigger">Lorem ipsum dolor sit amet.</button></h2><divclass="ac-panel"><pclass="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div></div></div>
Initialize the module
<script>newAccordion(".accordion-container");</script>

API

Examples

new Accordion(container, options)

  • container - string | HTMLElement (required), selector of accordion container
  • options - object (optional), accordion options
// Default optionsnewAccordion(".container-first");// User optionsnewAccordion(".container-second",{duration:400,showMultiple:true,onOpen:function(currentElement){console.log(currentElement);}});// Define several accordions with the same options (pass an array with selectors)newAccordion([".container-first",".container-second"],{});// or pass an array with HTMLElementsconstaccordions=Array.from(document.querySelectorAll(".accordion-container"));newAccordion(accordions,{});// Detach eventsconstaccordion=newAccordion(".container-first");accordion.detachEvents();
Options
OptionTypeDefault valueDescription
durationnumber500Animation duration in ms
ariaEnabledbooleantrueAdd ARIA elements to the HTML structure
collapsebooleantrueAllow collapse expanded panel
showMultiplebooleanfalseShow multiple elements at the same time
onlyChildNodesbooleantrueDisabling this option will find all items in the container. Warning: Setting tofalse will break the functionality of nested accordions
openOnInitarray[]Show accordion elements during initialization
elementClassstring"ac"Element class
triggerClassstring"ac-trigger"Trigger class
panelClassstring"ac-panel"Panel class
activeClassstring"is-active"Active element class
beforeOpenfunction-Calls before the item is opened.
beforeOpen: (currElement) => {}
onOpenfunction-Calls when the item is opened.
onOpen: (currElement) => {}
beforeClosefunction-Calls before the item is closed.
beforeClose: (currElement) => {}
onClosefunction-Calls when the item is closed.
onClose: (currElement) => {}
Methods
OptionDescriptionArguments
attachEvents()Attach events-
detachEvents()Detach events-
open()Open the accordion element with the given idx
E.g.acc.open(1)
idx - element index
close()Close the accordion element with the given idx
E.g.acc.close(1)
idx - element index
toggle()Toggle the accordion element with the given idx
E.g.acc.toggle(1)
idx - element index
openAll()Open all accordion elements (without animation)-
closeAll()Close all accordion elements (without animation)-
update()If there are new items added by lazy load, you can run this method to update the Accordion-
destroy()Destroy accordion instance:
Open elements, remove events, IDs & ARIA
-

v3 Release Info

There have been a lot of changes to the API in version3.0.0, so if you are using previous versions of the accordion (2.8.0 and below), I recommend updating the package to the latest version with new structure and options.

License

This project is under the MIT license.


[8]ページ先頭

©2009-2025 Movatter.jp