Skip to content

Instantly share code, notes, and snippets.

@bezenson
Created April 27, 2014 13:01
Show Gist options
  • Save bezenson/11345048 to your computer and use it in GitHub Desktop.
Save bezenson/11345048 to your computer and use it in GitHub Desktop.
jQuery Tabs
<!-- http://jsfiddle.net/evMdx/ -->
<div class="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<div class="tabsCont">
<div class="tab">first</div>
<div class="tab">Second</div>
</div>
</div>
.tabs > ul li {
cursor: pointer;
display: inline-block;
list-style: none;
margin: 0;
text-align: center;
}
.tabs > ul li.active {background: #eee;}
.tabsCont .tab {display: none;}
.tabsCont .tab.active {display: block;}
$(document).ready(function() {
$('.tabs').each(function() {
var $tabs = $(this),
$toggles = $tabs.children('ul').find('li'),
$tab = $tabs.find('.tab');
$toggles.eq(0).addClass('active');
$tab.eq(0).addClass('active');
$toggles.on('click', function() {
var $this = $(this),
index = $this.index();
if(!$this.hasClass('active')) {
$toggles.removeClass('active');
$this.addClass('active');
$tab.hide();
$tab.eq(index).fadeIn(200);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment