Learn HTML step-by-step from A to Z or improve your professional skills.  Home HTML and CSS Tutorials jQuery Examples - Horizontal Accordion

jQuery Examples - Horizontal Accordion


Up until now I have always used Scriptaculous / Prototype for any Java Script animation and effects, but lately I've heard a lot of good things about jQuery. So, I thought I would give it a try, and start doing some experiments.

In this example, I have created an accordion effect that reveals a caption for each thumbnail. I've done similar navigations like this in flash, so I wanted to see how it compared to doing it with jQuery.

In order to use jQuery in your pages you first need to download the latest release and then include the Java Script library within your head tags.

Now lets take a look at the html for this example. I gave the first anchor tag an id, so that we could set an initial width and make it appear expanded when the page is loaded.

Here is the CSS, which is pretty straight forward. The main thing to note is the fixed height being set on the anchor tag. Doing this along with "overflow: hidden" prevents the contained p tag from dropping down below the thumbnail.

ul{
list-style: none;
margin: 0;
padding: 0;
}

ul li{
float: left;
padding: 10px;
display: block;
margin-right: 10px;
}

ul li a{
display: block;
overflow: hidden;
height: 75px;
width: 75px;
}

#a1{
width: 210px;
}

ul li img{
position: absolute;
border: 3px solid #881212;
}

ul li p{
margin: 0;
padding: 0;
width: 120px;
display: block;
margin-left: 85px;
}

And here is the jQuery script that makes it all happen. This can be placed in the head tag. The first thing we do is set a few initial variables: lastBlock represents the block that is already expanded, maxWidth is the width we want our block to be when it is expanded, and minWidth is the width when it is not expanded.

Then we simply set a hover event on all anchor tag contained within all list items. Within the hover event we make two calls of the animate() function: one to close the lastBlock, and another to expand the block we are hovering. Then we set lastBlock equal to the block we just expanded. That way jQuery will know which one to close the next time the hover event is fired. The animate() function allows you to create custom animations by setting new values for multiple properties. In this case we are only animating the width.

One important thing to note is setting "queue" to false. If it is set to true, every hover event is stored and will be animated one after another, resulting in opening and closing long after the last hover happens.

For more info on the animate() function and other jQuery goodness, check out the official documentation.



Author's URL: Design Reviver
Learn HTML step-by-step from A to Z or improve your professional skills. More Tutorials: Featured Materials | Fresh Materials | More HTML Tutorials at Markuptutorials.com

Reader's comments
comments chiton August 29, 2011 says:
nice
Reply
comments jQuery Examples March 19, 2011 says:
great, here you have an accordion list if you need more examples
Reply
comments Nikki February 04, 2011 says:
Hi everyone,

I created a horizontal accordion plugin for jQuery. It looks pretty neat - it makes good use of CSS3 - but it's designed to be lightweight, so it doesn't use any background images. You can find it here: http:// nicolahibbert.com/demo/liteAccordion

Thanks!

Reply
comments hiren December 14, 2010 says:
xcvcvc
Reply
comments Jayavardhan December 06, 2010 says:
How to work it for Only one div click...open and close the accordion
Reply
comments T June 11, 2010 says:
Never mind, i sorted it. Changed the trigger to be the span instead of the a and swapped the css.
Reply
comments T June 10, 2010 says:
This is great. However when I put an href inside the inside the sentence the structure of the paragraph falls apart. Iv tried everything, any suggestions?
Reply
comments sad May 18, 2010 says:
adsdda
Reply
comments memphis March 15, 2010 says:
how can i add submenu elements inside each expanded accordion item?
Reply
comments gaurav March 12, 2010 says:
how to create cycling these boxes every 10 seconds. means second event expanded after 10 or 15 sec. then third .... n

thanks
gaurav
gaurav.pta@gmail.com

Reply
Add comments to "jQuery Examples - Horizontal Accordion"

Captcha