Skip to content

Instantly share code, notes, and snippets.

@adamwdraper
Created May 6, 2012 17:16
Show Gist options
  • Save adamwdraper/2623362 to your computer and use it in GitHub Desktop.
Save adamwdraper/2623362 to your computer and use it in GitHub Desktop.
Lightweight Tabs using jQuery
<div class="tabs cf">
<a class="tab active" data-type="tab1">tab1</a>
<a class="tab" data-type="tab2">tab2</a>
<a class="tab" data-type="tab3">tab3</a>
</div>
<div class="tab-panes">
<div class="tab-pane active" data-type="tab1">
tab1 info
</div>
<div class="tab-pane" data-type="tab2">
tab2 info
</div>
<div class="tab-pane" data-type="tab3">
tab3 info
</div>
</div>
$(function() {
$('.tab').on('click', function(e) {
$(this).parent().find('.tab.active').removeClass('active');
$(this).parent().parent().find('.tab-pane.active').removeClass('active');
var dataType = $(this).attr('data-type');
$(this).parent().find('.tab[data-type="'+dataType+'"]').addClass('active');
$(this).parent().parent().find('.tab-pane[data-type="'+dataType+'"]').addClass('active');
});
});
// LESS CSS
// Clearfix
// --------
// For clearing floats like a boss h5bp.com/q
.cf {
*zoom: 1;
&:before,
&:after {
display: table;
content: "";
}
&:after {
clear: both;
}
}
.tabs {
margin-bottom: .7em;
border-bottom: 1px solid #DDD;
.tab {
display: block;
float: left;
.border-radius(3px 3px 0 0);
margin: 0 7px -1px 0;
padding: 1px 2px;
border: 1px solid #FFF;
border-bottom: none;
cursor: pointer;
line-height: 1.5em;
font-size: 1.3em;
padding: 0 .5em;
&:hover {
background: #F3F3F3;
}
&.active {
border: 1px solid #DDD;
border-bottom: 1px solid #FFF;
color: #333;
&:hover {
background: none;
}
}
}
}
.tab-panes {
.tab-pane {
display: none;
&.active {
display: block;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment