Giter Site home page Giter Site logo

yangxin1994 / accordion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michu2k/accordion

0.0 0.0 0.0 1.51 MB

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

Home Page: https://michu2k.github.io/Accordion/

License: MIT License

HTML 6.52% JavaScript 85.54% SCSS 7.95%

accordion's Introduction

Accordion

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.1.1

Installation

npm

Install the package & import files

npm install accordion-js
import Accordion from 'accordion-js';
import 'accordion-js/dist/accordion.min.css';
CDN

Include files using CDN.

https://unpkg.com/[email protected]/dist/accordion.min.css
https://unpkg.com/[email protected]/dist/accordion.min.js
<link rel="stylesheet" href="[CDN CSS URL]">
<script src="[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 from dist/ 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.

<div class="accordion-container">
  <div class="ac">
    <h2 class="ac-header">
      <button class="ac-trigger">Lorem ipsum dolor sit amet.</button>
    </h2>
    <div class="ac-panel">
      <p class="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </div>

  <div class="ac">
    <h2 class="ac-header">
      <button class="ac-trigger">Lorem ipsum dolor sit amet.</button>
    </h2>
    <div class="ac-panel">
      <p class="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </div>

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

API

Examples

new Accordion(container, options)

  • container - string | HTMLElement (required), selector of accordion container
  • options - object (optional), accordion options
// Default options
new Accordion('.container-first');

// User options
new Accordion('.container-second', {
  duration: 400,
  showMultiple: true,
  onOpen: function(currentElement) {
    console.log(currentElement);
  }
});

// Define several accordions with the same options (pass an array with selectors)
new Accordion(['.container-first', '.container-second'], {});

// or pass an array with HTMLElements
const accordions = Array.from(document.querySelectorAll('.accordion-container'));
new Accordion(accordions, {});

// Detach events
const accordion = new Accordion('.container-first');
accordion.detachEvents();
Options
Option Type Default value Description
duration number 600 Animation duration in ms
ariaEnabled boolean true Add ARIA elements to the HTML structure
collapse boolean true Allow collapse expanded panel
showMultiple boolean false Show multiple elements at the same time
openOnInit array [] Show accordion elements during initialization
elementClass string 'ac' Element class
triggerClass string 'ac-trigger' Trigger class
panelClass string 'ac-panel' Panel class
activeClass string 'is-active' Active element class
beforeOpen function - Calls before the item is opened.
beforeOpen: (currElement) => {}
onOpen function - Calls when the item is opened.
onOpen: (currElement) => {}
beforeClose function - Calls before the item is closed.
beforeClose: (currElement) => {}
onClose function - Calls when the item is closed.
onClose: (currElement) => {}
Methods
Option Description Arguments
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 -
closeAll() Close all accordion elements -
destroy() Destroy accordion instance:
Open elements, remove events, IDs & ARIA
-

v3 Release Info

There have been a lot of changes to the API in version 3.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.

accordion's People

Contributors

dependabot[bot] avatar janmarkuslanger avatar michu2k avatar tristanag avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.