Include the files
You need to include accordion.js but it requires
prototype.js and effects.js (like I said this is for scriptaculous!).
As long as you are using at least the latest stable builds you are fine.
<script type="text/javascript" src="javascript/prototype.js"></script>
<script type="text/javascript" src="javascript/effects.js"></script>
<script type="text/javascript" src="javascript/accordion.js"></script>
The Markup
Pretty much anything will work, so its really up to you. The
presentation is up to you (see the source of this page for some
examples). Ideally you should probably use header tags with div's
containing the content as this would be best symantically speaking and
for seo. Below is a basic example.
<h2 class="accordion_toggle">Title Bar</h2>
<div class="accordion_content">...</div>
...
...
...
<h2 class="accordion_toggle">Title Bar</h2>
<div class="accordion_content">...</div>
The important thing here is that it need to be the title
element then the content element and so on, nothing in between, just
like every other accordion.
The Options
Not many options, but all you need to make this thing do anything you may require!
resizeSpeed : 8,
classNames : {
toggle : 'accordion_toggle',
toggleActive : 'accordion_toggle_active',
content : 'accordion_content'
},
defaultSize : {
height : null,
width : null
},
direction : 'vertical',
onEvent : 'click'
Now these are the default options set automatically, so
let's now take a look on how to create an accordion and change those
options.
The Javascript
Below is how I created the two accordions you see on this
page, one vertical, one horizontal with a specified width.
new accordion('container-selector', options);
var horizontalAccordion = new accordion('#top_container', {
classNames : {
toggle : 'horizontal_accordion_toggle',
toggleActive : 'horizontal_accordion_toggle_active',
content : 'horizontal_accordion_content'
},
defaultSize : {
width : 525
},
direction : 'horizontal'
});
var verticalAccordion = new accordion('#bottom_container');
Ok, so now we know how to do the markup, hopefully you got
creative with the css and looked at the source of this page to get
started, and not you know how make an accordion. What if you want to
use javascript to open a slide on load or at any time... here is how.
var verticalAccordion = new accordion('#bottom_container');
verticalAccordion.activate($$('#bottom_container .accordion_toggle')[0]);
So we use the selector method from prototype to get the
first title bar from the container+classname and we want the first one
so we use [0]. Couldn't be easier!
Preload
So let's say you want all your accordions closed on page load
but don't want the nasty flash and don't want to sacrifice
accessibility.
var verticalAccordions = $$('.accordion_toggle');
verticalAccordions.each(function(accordion) {
$(accordion.next(0)).setStyle({
height: '0px'
});
});