Advertisement

# Create a Simple, Intelligent Accordion Effect Using Prototype and Scriptaculous

Read Time: 12 min

We've all seen the "accordion" type effect used on many Web 2.0 sites; however, many accordion scripts are heavy, make poor use of the libraries they are based on, and don't handle things like ensuring that the accordion maintains a consistent height. In this tutorial, we will use the Prototype and Scriptaculous libraries to create a lightweight, intelligent accordion.

## Step 1 - The Goal

Our objective is to create a lightweight accordion script based on the Prototype and Scriptaculous javascript libraries.
The accordion should:

• Allow an unlimited number of accordion panes
• Be completely styled by CSS
• Be Unobtrusive—users without javascript turned on should see all of your accordion content
• Be Light Weight—With relatively few lines of code; use event delegation to limit memory consumption.
• Support any kind of content within the accordion
• Ensure that when the content of each accordion pane changes, the height of the accordion remains constant to avoid
the annoying "page bouncing" effect

This is a relatively advanced tutorial that assumes the reader has a reasonable knowledge of Javascript, CSS, HTML, Object-Oriented
programming, and a basic understanding of the Prototype and Scriptaculous libraries. However, complete source code is
available for you to study and the code is very simple to read and learn from if you are not familiar with the specific
libraries used.

Before we begin, you can see a working demonstration of the accordion in action.

## Step 2 - Begin with Basic Markup

To begin, we will create some simple HTML markup for our accordion:

 1 
 2 
Toggle 1
 3 
Content 1
 4 
Toggle 2
 5 
Content 2
 6 
Toggle 3
 7 
Content 3
 8 
Toggle 4
 9 
Content 4
 10 


## Step 3 - Add Some Style

Next, we need to add some style around our accordion to make it look like an accordion. To begin with, we'll do a first pass of basic styling and then add more when it's all working. There are also some additional
styles that need to be included to ensure that the accordion will display correctly as it is animating.

 1 div#test-accordion{  2  margin: 10px;  3  border: 1px solid #aaa;}  4 5 div.accordion {  6  position: relative; /* required for bounding--works around a "peculiarity" in Prototype */  7 }  8 9 div.accordion-toggle{  10  position: relative; /* required for effect */  11  z-index: 10; /* required for effect */  12  background: #eee; /* required for effect--can be anything except "transparent" */  13  cursor: pointer;  14 }  15 16 div.accordion-toggle-active{  17  background: #fff;  18 }  19 20 div.accordion-content{  21  overflow: hidden; /* required for effect */  22  background: #aaa;  23 } 

See the basic accordion with a simple stylesheet.

## Step 4 - Create the Javascript Accordion Class

Prototype provides a wonderful framework for building classes in Javascript and we'll use that functionality to build
our accordion class. This class will contain all the properties and methods of an accordion: the currently displayed
pane, the contents of the accordion, methods to expand and contract panes, and event handler methods to define what happens
when users take an action such as clicking. For now, we'll set up the basic structure of the class as well as all the
properties and methods we'll need:

 1 var Accordion = Class.create({  2  initialize: function(){  3  this.accordion = null; /* Stores a pointer to the the accordion element */  4  this.contents = null; /* Array of pointers to the headings and content panes of the accordion */  5  this.options = null; /* Allows user to define the names of the css classes */  6  this.maxHeight = 0; /* Stores the height of the tallest content pane */  7  this.current = null; /* Stores a pointer to the currently expanded content pane */  8  this.toExpand = null; /* Stores a pointer to the content pane to expand when a user clicks */  9  this.isAnimating = false; /* Keeps track of whether or not animation is currently running */  10 11  },  12 13  checkMaxHeight: function(){}, /* Determines the height of the tallest content pane */  14  initialHide: function(){}, /* Hides the panes which are not displayed by default */  15  attachInitialMaxHeight: function(){}, /* Ensures that the height of the first content pane matches the tallest */  16  expand: function(el){}, /* Tells the animation function which elements to animate */  17  animate: function(){}, /* Performs the actual animation of the accordion effect */  18  handleClick: function(e){} /* Determine where a user has clicked and act based on that click */  19 20 }); 

These are the basic methods and properties that we will need when building our accordion. Each of the next steps will
take you through building each method until we have a working accordion. If at any point during the tutorial you need
a quick refresher on what each method or property is for, you may use this heavily commented code as a reference.

## Step 5 - Initialize: Get Things Started

Prototype classes have a special method called initalize() which is a constructor; this means it acts when the user
creates a new instance object of that class. For any accordion, we need to know 2 things before we begin:

1. The id of the accordion element.
2. The default starting position of the accordion (if anything other than the first position)

So, we will need to allow our constructor to accept those two parameters. Additionally, our constructor must:

1. Retrieve and store the accordion and its contents as pointers to those elements
2. Set the user defined options
3. Set the current expanded element
4. Determine the maximum height we will use as the height for all of our content panes and apply it
5. Hide the content panes which are not shown by default
6. Add an event listener to the accordion to watch user clicks.

Here is the code for our initialize() method:

 1 initialize: function(id, defaultExpandedCount) {  2  if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");  3  this.accordion =$(id);  4  this.options = {  5  toggleClass: "accordion-toggle",  6  toggleActive: "accordion-toggle-active",  7  contentClass: "accordion-content"  8  }  9  this.contents = this.accordion.select('div.'+this.options.contentClass);  10  this.isAnimating = false;  11  this.maxHeight = 0;  12  this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];  13  this.toExpand = null;  14 15  this.checkMaxHeight();  16  this.initialHide();  17  this.attachInitialMaxHeight();  18 19  var clickHandler = this.clickHandler.bindAsEventListener(this);  20  this.accordion.observe('click', clickHandler);  21 } 

As you can see, we've set all of our properties to reasonable default values and called 3 methods to help get things set
up. Finally, we've attached the event handler to the accordion. Let's create those three methods and the event handler.

## Step 6 - Checking the Tallest Element

One of the requirements for our accordion is that it must scale so that even when the tallest content pane is expanded,
the overall accordion height will remain constant. To accomplish this goal, we will iterate through the content panes
determining which one is the tallest and set the maxHeight property accordingly:

 1 checkMaxHeight: function() {  2  for(var i=0; i this.maxHeight) {  4  this.maxHeight = this.contents[i].getHeight();  5  }  6  }  7 } 

## Step 7 - Hiding the Rest

Our accordion should only display the content pane specified as the current pane; all others should be hidden
by default. Additionally, we need to set these content pane's height attribute to 0; this prevents the content pane from
briefly appearing fully expanded before properly animating.

 1 initialHide: function(){  2  for(var i=0; i

## Step 8 - Show the Default Content Pane

Now that we've hidden all but the default content pane, we need to make sure the default content pane displays correctly;
it's heading should have the "active" style applied to it and it's height should match the maxHeight property:

 1 attachInitialMaxHeight: function() {  2  this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);  3  if(this.current.getHeight() != this.maxHeight) this.current.setStyle({height: this.maxHeight+"px"});  4 } 

## Step 9 - Create the Event Handler

If you come from a traditional event handling background where we attach the event handler to each area we want clickable,
it may seem confusing that we are only attaching the handler to one element. We are using event
delegation
. For those of you unfamiliar with the subject, I have written a brief
overview of event delegation
which
will introduce you to the concept and why it is so important. That said, we need an intelligent event handler:

 1 clickHandler: function(e) {  2  var el = e.element();  3  if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {  4  this.expand(el);  5  }  6 } 

There are two parts to this function. First, we determine what was clicked. Then, we check to make sure that it was a
heading that was clicked and that no animation is currently running. If this is the case, we call the expand() method
to start the process of the accordion. The variable we pass to the expand() method is the heading on which the user clicked.

## Step 10 - Start the Process

Now we can start the process of doing the accordion effect. We know the expand() method must take a parameter for the
element which was clicked. Using that parameter, the expand method determines which content pane to expand, and if it
is not already expanded, calls the animate() method to "do its magic!"

 1 expand: function(el) {  2  this.toExpand = el.next('div.'+this.options.contentClass);  3  if(this.current != this.toExpand){  4  this.toExpand.show();  5  this.animate();  6  }  7 }, 

## Step 11 - Doing the "Dirty Work"

At this point, all of the pieces are in place; we know which content pane is currently displayed, we know which heading
the user has clicked, and we know which content pane the user has requested to be shown. Now, we must create the accordion
animation. For this, we will create an animate() method that will use the Scriptaculous Effect.Parallel class to render
the two animations together; and the Effect.Scale class to change the size of each content pane. The animate method will
perform these steps:

1. Create an array that will be used to store our Effect.Scale objects
2. Collect the parameters to pass to the Effect.Scale constructor for the content pane that will be shown and create
the object
3. Add that object to our array
4. Collect the parameters to pass to the Effect.Scale constructor for the content pane that will be hidden and create
the object
5. Add that object to our array
6. Create the Effect.Parallel object that will run our Effect.Scale objects is sync.
7. Tell our Accordion object that we are animating
8. Run the animations
9. Clean up any styles left behind
10. Tell our Accordion object that we are finished animating
 1 animate: function() {  2  var effects = new Array();  3  var options = {  4  sync: true,  5  scaleFrom: 0,  6  scaleContent: false,  7  transition: Effect.Transitions.sinoidal,  8  scaleMode: {  9  originalHeight: this.maxHeight,  10  originalWidth: this.accordion.getWidth()  11  },  12  scaleX: false,  13  scaleY: true  14  };  15 16  effects.push(new Effect.Scale(this.toExpand, 100, options));  17 18  options = {  19  sync: true,  20  scaleContent: false,  21  transition: Effect.Transitions.sinoidal,  22  scaleX: false,  23  scaleY: true  24  };  25 26  effects.push(new Effect.Scale(this.current, 0, options));  27 28  new Effect.Parallel(effects, {  29  duration: 0.5,  30  fps: 35,  31  queue: {  32  position: 'end',  33  scope: 'accordion'  34  },  35  beforeStart: function() {  36  this.isAnimating = true;  37  this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);  38  this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);  39  }.bind(this),  40  afterFinish: function() {  41  this.current.hide();  42  this.toExpand.setStyle({ height: this.maxHeight+"px" });  43  this.current = this.toExpand;  44  this.isAnimating = false;  45  }.bind(this)  46  });  47 } 

For a complete explanation of the option parameters we are passing to both the Effect.Scale and Effect.Parallel objects,
please see the Scriptaculous documentation.
The important aspects of the method are the beforeStart and afterFinish methods on our Effect.Parallel. The beforeStart
method tells the accordion that it is currently animating. This will prevent the event handler from attempting to start
any further changes so long as the animation is in progress. It also makes sure that the heading which was clicked is
given the "active" class name. The afterFinish method completely hides the content pane which had been previously displayed
(after it has disappeared due to the animation). It also ensures that the final height of the newly displayed content
pane is correct. Now that the swap is complete, it tells our accordion that the currently expanded content pane is the
one we have newly expanded and that the animation is complete.

## Step 12 - Adding Some More Style

At this point we have a decent looking accordion, which you can see in action here. But with a little CSS we can make it all look much more spectactular. So first we create a quick Photoshop mockup so we have a rough idea of how it should all look. With that in mind, we're going to need three images:

1. A 'logo' image -
2. A couple of nice background images - and

And here's the revised CSS code:

 1 body {  2  padding: 130px 50px 50px 50px;  3  background: #252422 url(../img/logo.gif) no-repeat;  4  background-position: 60px 40px;  5  font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, Sans-serif;  6  font-size: 11px;  7  line-height: 18px;  8 }  9 10 div#test-accordion{  11  border: 1px solid #343230;  12  background-color: #21201f;  13  padding: 10px;  14 }  15 16 div.accordion {  17  position: relative; /* required for bounding */https://code.tutsplus.com  18  width: 800px;  19 }  20 21 div.accordion-toggle{  22  position: relative; /* required for effect */  23  z-index: 10; /* required for effect */  24  background: #3f3c38 url(../img/off.jpg) repeat-x;  25  background-position: bottom;  26  color: #fff;  27  cursor: pointer;  28  margin-bottom: 1px;  29  padding: 9px 14px 6px 14px;  30  border-top: 1px solid #5d5852;  31 }  32 33 div.accordion-toggle:hover, div.accordion-toggle-active{  34  background-image: url(../img/on.jpg);  35  background-color: #6d493a;  36  border-top: 1px solid #a06b55;  37 }  38 39 div.accordion-content{  40  overflow: hidden; /* required for effect */  41  background: #302e2c;  42  color: #c4bab1;  43  border-bottom: 1px solid #000;  44 }  45 46 div.accordion-content p{  47 margin: 9px 24px 6px 24px;  48 } `

As you can see here we've:

1. Added some background styles around the page and the accordion class
2. Given the accordion-toggle div a regular background color
3. Set the accordion-toggle:hover and the active states to use the same reddish background

## Step 13 - See It in Action

You can see the working demonstration here. You can also add your own CSS and images
to tailor the look to your site.

Download: accordion.js & accordion.css

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.