Git Product home page Git Product logo

accordion's Introduction

Accordion

Silky-smooth accordion widgets with no external dependencies.

npm install accordion --save
bower install silk-accordion --save

An simple interactive demo can be found here. More complicated and extreme demos can be found in the demos directory.

Usage

Include the following two files in your project:

src/accordion.css
src/accordion.js

Layout your markup like this:

<div class="accordion">

	<div>
		<h1> Heading </h1>
		<div> Content </div>
	</div>
	
	<div>
		<h1> Heading </h1>
		<div> Content </div>
	</div>
	
</div>

Then create an Accordion instance with a reference to a DOM element:

var el = document.querySelector(".accordion");
new Accordion(el);

Options can be passed in a second argument:

new Accordion(el, {
	onToggle: function(fold, isOpen){
		console.log(fold);   // -> Reference to a `Fold` instance
		console.log(isOpen); // -> true / false
	}
});

Styling

The base stylesheet is located at src/accordion.css. Embed it into your application's existing styling, tweaking it if desired.

Note: This stylesheet only includes properties necessary for the Accordion to function. Making it look appealing with colours and fonts is left as an exercise to the developer. Check the source of the bundled demos for some ideas.

Using ES6 modules

If your project uses native JavaScript modules, consider loading src/accordion.mjs instead:

<!-- ES6+ -->
<script type="module">
	import Accordion from "./src/accordion.mjs";
	for(const el of document.querySelectorAll(".accordion"))
		new Accordion(el);
</script>

The old accordion.js file contains only ES5, and can be used as a fallback for older platforms which lack ES module support:

<!-- Fallback to ES5 for legacy browsers -->
<script nomodule src="src/accordion.js"></script>
<script nomodule>
	"use strict";
	var accordions = document.querySelectorAll(".accordion");
	for(var i = 0, l = accordions.length; i < l; ++i)
		new Accordion(accordions[i]);
</script>

IE8 support

For IE8-9 compatibility, install fix-ie:

npm install fix-ie --save
bower install fix-ie --save

Then link to it using a conditional comment, before any other script on the page!

<!DOCTYPE html>
<html lang="en">
	<head>
	<!--[if lte IE 9]>
		<script src="node_modules/fix-ie/dist/ie.lteIE9.js"></script>
	<![endif]-->
	<meta charset="utf-8" />

This fixes IE's buggy handling of Object.defineProperty, which the Accordion makes extensive use of. fix-ie also provides oodles of helpful polyfills to fix IE8's shoddy DOM support.

Options

Name Type Default Description
closeClass String "closed" CSS class used to mark a fold as closed
disabled Boolean false Whether to disable the accordion on creation
disabledClass String undefined CSS class marking an accordion as disabled
edgeClass String "edge-visible" CSS class toggled based on whether the bottom-edge is visible
enabledClass String "accordion" CSS class marking an accordion as enabled
heightOffset Number 0 Distance to offset each fold by
modal Boolean false Whether to close the current fold when opening another
noAria Boolean false Disable the addition and management of ARIA attributes
noKeys Boolean false Disable keyboard navigation
noTransforms Boolean false Disable CSS transforms; positioning will be used instead
onToggle Function undefined Callback executed when opening or closing a fold
openClass String "open" CSS class controlling each fold's "open" state
snapClass String "snap" CSS class for disabling transitions between window resizes
useBorders Boolean "auto" Consider borders when calculating fold heights

accordion's People

Contributors

alhadis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

accordion's Issues

Strange type rendering issue with Chrome 55 / Mac 10.10.5

Hi I'm wondering if you can reproduce the issue I'm seeing below:
https://landofwomen.com/pages/united-states-stores

Notice in the screenshot attached "Arizona", the first accordion section title, appears a lighter weight font. It's fairly consistent on this page but I've noticed it elsewhere less consistently. The issue goes away if I resize the browser window or toggle certain .accordion css attributes using the inspector tool (like backface-visibility and transform), but actually commenting them out does nothing after a full page refresh.

screenshot 2017-01-10 12 40 21

I'm running Chrome 55.0.2883.95 on Mac OS 10.10.5.

Let me know if you have any thoughts!

Thanks

Great Plugin

Is there an easy way to keep first item expanded by default. Also when expanding a new element collapse the previous one? Thanks.

Anyway to access the click event?

I want to add buttons to the accordion header. So when there is a click event, I need to be able to figure out whether I clicked the heading or the button in the heading. How do I do this?

Feature: Only open one accordion at a time, collapse others?

Hello,

Somehow it must be possible to only open one accordion at a time whereby if an accordion item is open and you click to expand another accordion item, the open item closes and the clicked item expands.

Any help would be great.

Thanks!

To-do list

  • Write documentation
  • Add support for using CSS transforms to establish real smoothness
  • Die a painless death in my sleep

Responsive heightOffset

Is there any way of making the heightOffset responsive? I usually have a gap that is fluid between tablet and desktop

Loaded event?

Is there a way to check if the accordion is completely loaded and on screen?

Deep linking

Do you have an example where you visit a page with a hash in the URL and it opens the appropriate accordion item and scrolls to it?

I am currently not seeing a way to trigger an individual open myself once I get the link element.

Any help would be appreciated!

No easy way to add/remove folds after construction

Hi! Is it possible to fully destroy and init again accordion with the same container? (In case if items there have been changed)

When I call just update, it doesn't init recently added folds, and when I init a new accordion, I see the all previous are not deleted.

Enter and spacebar to open/close accordion item

First, thanks for the great package!

So I'm seeing code for

/** Enter: Toggle */
case 13: {
    THIS.open = !THIS.open;
    break;
}

But this doesn't appear to be working, it does not toggle the item to be open and closed. Ideally i'd also like to have the spacebar do the same thing. The right and left arrows work as I'd expect though.

Is this behavior something that could be added? Or do you have a recommendation of adding my own event listener to accomplish this?

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.