Creating Accordion with CSS3 and JQuery

Using some of the exciting properties of CSS3, we can create a beautiful accordion for the web content. This would give us vertical tabs that slide out when hovering. The HTML part of this task would consist of list where each accordion tab would be a list element. Markup with only a single list element is as follows:

1

2

3

4

5

6

7

8

9

10

11

&lt;ulclass="accordion"id="accordion"&gt;

&lt;liclass="bg1"&gt;

&lt;divclass="heading"&gt;Heading&lt;/div&gt;

&lt;divclass="bgDescription"&gt;&lt;/div&gt;

&lt;divclass="description"&gt;

&lt;h2&gt;Heading&lt;/h2&gt;

&lt;p&gt;Some descriptive text&lt;/p&gt;

&lt;ahref="#"&gt;more?&lt;/a&gt;

&lt;/div&gt;

&lt;/li&gt;

&lt;/ul&gt;

Although there would be other list elements having their respective classes, we would not really need those classes but would instead style the element using #accordion in our style sheet.

Now the style for the list would be as follows:

1

2

3

4

5

6

7

8

9

10

ul.accordion{

list-style:none;

position:absolute;

right:80px;

top:0px;

font-family:Cambria,serif;

font-size:16px;

font-style:italic;

line-height:1.5em;

}

Each list element would have a different background image which can be customized and changed according to the requirements. Next step would be to create a semi-transparent header for each list item. This would be done as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

ul.accordion li.heading{

background-color:#fff;

padding:10px;

margin-top:60px;

opacity:0.9;

text-transform:uppercase;

font-style:normal;

font-weight:bold;

letter-spacing:1px;

font-size:14px;

color:#444;

text-align:center;

text-shadow:-1px-1px1px#ccc;

}

The minor details can be set later on. Now we need to create a function which would make the list elements slide our when hovered upon them. This would be done with the help of JavaScript. Then the top heading should fade out while the gradient (CSS class “bgDescription”) and the description appear:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

&lt;script type="text/javascript"&gt;

$(function(){

$('#accordion &gt; li').hover(

function(){

var$this=$(this);

$this.stop().animate({'width':'480px'},500);

$('.heading',$this).stop(true,true).fadeOut();

$('.bgDescription',$this).stop(true,true).slideDown(500);

$('.description',$this).stop(true,true).fadeIn();

};

function(){

var$this=$(this);

$this.stop().animate({'width':'115px'},1000);

$('.heading',$this).stop(true,true).fadeIn();

$('.description',$this).stop(true,true).fadeOut(500);

$('.bgDescription',$this).stop(true,true).slideUp(700);

}

};

});

&lt;/script&gt;

The first function inside of $(‘#accordion > li’).hover is the function triggered when the mouse moves over the respective element and the second function gets triggered when moving the mouse out again. The accordion is ready and would run perfectly well on all the browsers that support this CSS3 feature.