Cover by Cody Lindley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

7.3. Creating a Horizontal Accordion

Problem

The jQuery UI library supports vertical accordions out of the box, and in fact there are a few simple code snippets that can be used to create a rudimentary accordion effect. However, making the accordion run horizontally requires specific CSS and a slightly different take on the jQuery code.

For this solution we won’t be using the template, because the markup is different for the horizontal accordion.

Solution

HTML

<div id="accordionWrapper">
  <h3 class="red"><a href="#red">Red</a></h3>
  <div id="red" class="box"><p>Lorem ipsum dolor sit amet, consectetur 
adipisicing.</p></div>

  <h3 class="green"><a href="#green">Green</a></h3>
  <div id="green" class="box"><p>Lorem ipsum dolor sit amet, consectetur 
adipisicing.</p></div>

  <h3 class="blue"><a href="#blue">Blue</a></h3>
  <div id="blue" class="box"><p>Lorem ipsum dolor sit amet, consectetur 
adipisicing.</p></div>
</div>

CSS

#accordionWrapper {
  margin: 10px;
}

#accordionWrapper h3 a {
  text-indent: -9999px;
  height: 150px;
  width: 50px;
  float: left;
}

#accordionWrapper .red {
  background: #c00 url(images/red.png) no-repeat;
}

#accordionWrapper .green {
  background: #0c0 url(images/green.png) no-repeat;
}

#accordionWrapper .blue {
  background: #00c url(images/blue.png) no-repeat;
}

#accordionWrapper div.box {
  float: left;
  height: 150px;
  width: 150px;
  border: 0;
  margin: 0;

  /* to cancel the image from .red, etc */
  background-image: none;
}

jQuery

$.fn.horizontalAccordion = function (speed) { return this.each(function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required