Accordion for script.aculo.us
Everybody knows the Accordion effect from mootools. In the past I used script.aculo.us pretty much, and I couldn't find a good accordion effect, which worked the same like the mootools version. So I decided the create one, and this is the result.
How to Use
You'll need script.aculo.us, which you can download here.
Put the following in your <head> tag:
Create the html for the accordion:
And add before the </body> tag this code:
Constructor Arguments
The first parameter is a CSS selector, which selects all headings, for the accordion.
The second parameter is a CSS selector which selects all content elements, used by the accordion.
The third parameter is a list of options, see below.
Options
duration
The duration of the slide and fade effect.
default_open
The index of the body which is open by default.
OnStart
An event which will be called before opening a new content element.
OnFinish
An event which will be called after the open effect.
Download script as zip
Tags:
Javascript
Scriptaculous
How to Use
You'll need script.aculo.us, which you can download here.
Put the following in your <head> tag:
- <script type="text/javascript" src="js/prototype.js"></script>
- <script type="text/javascript" src="js/scriptaculous.js"></script>
- <script type="text/javascript" src="js/accordion.js"></script>
Create the html for the accordion:
- <div id="accordion">
- <h3>Title 1</h3>
- <p>Text 1</p>
- <h3>Title 2</h3>
- <p>Text 2</p>
- <h3>Title 3</h3>
- <p>Text 3</p>
- </div>
And add before the </body> tag this code:
- <script type="text/javascript">
- new Accordion("div#accordion h3", "div#accordion p");
- </script>
Constructor Arguments
The first parameter is a CSS selector, which selects all headings, for the accordion.
The second parameter is a CSS selector which selects all content elements, used by the accordion.
The third parameter is a list of options, see below.
Options
duration
The duration of the slide and fade effect.
- new Accordion('div#accordion h3', 'div#accordion p', {duration: 0.5});
default_open
The index of the body which is open by default.
OnStart
An event which will be called before opening a new content element.
OnFinish
An event which will be called after the open effect.
Download script as zip
Source
- Accordion.js
- /**
- * Accordion Effect for Script.aculo.us
- * Created by Lucas van Dijk
- * http://www.return1.net
- *
- * Copyright 2007 by Lucas van Dijk
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- */
- var Accordion = Class.create();
- Accordion.prototype =
- {
- initialize: function(handles, bodys, options)
- {
- this.options = this._set_options(options);
- this.headers = $$(handles);
- this.bodys = $$(bodys);
- if(this.bodys.length != this.headers.length)
- {
- throw Error('Number of headers/bodys does not match!');
- }
- for(var i = 0; i < this.headers.length; i++)
- {
- Event.observe(this.headers[i], this.options.event_trigger, this.show.bind(this, i));
- this.headers[i].style.cursor = "pointer";
- this.bodys[i].style.display = "none";
- }
- this.bodys[this.options.default_open].id = "visible";
- this.show(this.options.default_open, true);
- },
- show: function(index, force)
- {
- if ((index >= this.length) || (index < 0))
- {
- throw Error('Index out of range');
- }
- if (this.bodys[index].id == 'visible')
- {
- if (typeof force == "boolean")
- {
- this.options.OnStart(index, this.bodys[index]);
- // Force display the visible object
- for(var i = 0; i < this.bodys.length; i++)
- {
- if(this.bodys[i].style.display != 'none' && i != index)
- {
- new Effect.BlindUp(this.bodys[i]);
- }
- }
- new Effect.BlindDown(this.bodys[index]);
- }
- }
- else
- {
- this.options.OnStart(index, this.bodys[index]);
- // Normal change
- new Effect.Parallel(
- [
- new Effect.Fade($('visible')),
- new Effect.BlindUp($('visible')),
- new Effect.BlindDown(this.bodys[index]),
- new Effect.Appear(this.bodys[index])
- ], {
- duration: this.options.duration
- }
- );
- $('visible').id = "";
- this.bodys[index].id = "visible";
- }
- this.options.OnFinish(index, this.bodys[index]);
- },
- _default_options:
- {
- duration: 0.3,
- event_trigger: 'click',
- OnStart: function() { },
- OnFinish: function() { },
- default_open: 0
- },
- _set_options: function(options)
- {
- if(typeof options != "undefined")
- {
- var result = [];
- for(option in this._default_options)
- {
- if(typeof options[option] == "undefined")
- {
- result[option] = this._default_options[option];
- }
- else
- {
- result[option] = options[option];
- }
- }
- return result;
- }
- else
- {
- return this._default_options;
- }
- }
- };
- Effect.Accordion = Accordion;

