Skip to content

Instantly share code, notes, and snippets.

@renatorib
Created May 22, 2014 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatorib/39d00e1ebeed78c57d75 to your computer and use it in GitHub Desktop.
Save renatorib/39d00e1ebeed78c57d75 to your computer and use it in GitHub Desktop.
jQuery Accordion
<dl class="accordion">
<dt><a href="#">Panel 1</a></dt>
<dd>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</dd>
<dt><a href="#">Panel 2</a></dt>
<dd>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque felis.</dd>
<dt><a href="#">Panel 3</a></dt>
<dd>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus hendrerit. Pellentesque aliquet nibh nec urna. In nisi neque, aliquet vel, dapibus id, mattis vel, nisi. Sed pretium, ligula sollicitudin laoreet viverra, tortor libero sodales leo, eget blandit nunc tortor eu nibh. Nullam mollis. Ut justo. Suspendisse potenti.</dd>
</dl>
/**
* @author Renato Ribeiro
*
* the bullets is just a class 'on' or 'off' on dt and dd
* do your bullets in css with these classes ;)
*
*/
var Accordion = {
open: function(el, smooth){
if (smooth === undefined) smooth = true;
if(smooth == true){ el.slideDown(); } else { el.show(); }
this.bullet(el, 'on');
this.bullet(el.prev(), 'on');
},
close: function(el, smooth){
if (smooth === undefined) smooth = true;
if(smooth == true){ el.slideUp(); } else { el.hide(); }
this.bullet(el, 'off');
this.bullet(el.prev(), 'off');
},
bullet: function(el, state){
el.removeClass().addClass(state);
}
}
$(document).ready(function() {
var all = $('.accordion > dd');
Accordion.close(all, false);
var first = all.first();
Accordion.open(first, false);
$('.accordion > dt > a').click(function() {
var me = $(this).parent().next();
Accordion.close(all);
Accordion.open(me);
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment