Clean, semantic tabs.
Tab 1 content
Tab 2 content
Tab 3 content
<div class="tab js-tab">
<h3 class="tab__header js-tab__header" id="Tab-1">Tab 1</h3>
<div class="tab__body js-tab__body">
<p>Tab 1 content</p>
</div>
<h3 class="tab__header js-tab__header js-tab__header--first" id="Tab-2">Tab 2</h3>
<div class="tab__body js-tab__body">
<p>Tab 2 content</p>
</div>
<h3 class="tab__header js-tab__header" id="Tab-3">Tab 3</h3>
<div class="tab__body js-tab__body">
<p>Tab 3 content</p>
</div>
</div>
// If js
.js {
// Tab block
.tab {
@extend %module;
position: relative;
margin-bottom: $grid-gutter + ($baseline * 2);
@include cf;
@include break(medium) {
margin-bottom: $grid-gutter / 2 + ($baseline * 2);
}
}
// Tab element
.tab__header {
height: $grid-gutter * 1.5;
float: left;
margin: 0;
padding: $grid-gutter / 2;
display: block;
border: 1px solid transparent;
&:hover {
cursor: pointer;
}
@include break(medium) {
height: $grid-gutter + 1px;
padding: $grid-gutter / 4;
}
}
// Tab element
.tab__body {
width: 100%;
position: absolute;
top: ($grid-gutter * 1.5) - 1px;
z-index: 10;
padding: $grid-gutter / 2;
border: 1px solid $color-border;
background-color: #FFF;
@include break(medium) {
top: $grid-gutter;
padding: $grid-gutter / 4;
}
}
// Tab state
.tab__header_is_active {
position: relative;
z-index: 20;
border-color: $color-border;
border-bottom: none;
background-color: #FFF;
.tab__title {
color: $color-text;
}
&:hover,
&:focus {
cursor: auto;
.tab__title {
text-decoration: none;
color: $color-text;
}
}
}
}
// Tab
$('.js-tab').each(function() {
// Variables
var tab = $(this);
var tabActive;
var tabHeight = tab.height();
var tabActiveHeight;
// Hide body of all tabs
tab.find('.js-tab__body').hide();
// Trigger the first tab
tabActive = tab.find('.js-tab__header').first().addClass('tab__header_is_active');
// Show body of triggered tab and calculate the height
tabActiveHeight = tabActive.next('.tab__body').show().height();
// Set total height of tab
tab.css('height', tabHeight + tabActiveHeight);
// Tabbing
$('.js-tab__header').click(function(e){
tab.find('.tab__header_is_active').removeClass('tab__header_is_active').next('.js-tab__body').hide();
tabActive = $(this).addClass('tab__header_is_active');
tabActiveHeight = tabActive.next('.js-tab__body').show().height();
tabHeight = tab.removeAttr('style').height();
tab.css('height', tabHeight + tabActiveHeight);
});
// Trigger selected tab
if( tab.find('.js-tab__header').hasClass('js-tab__header--first') ) {
tab.find('.js-tab__header--first').trigger('click');
}
// Trigger and scroll down to ID-tagged tab based on hashtag+ID in the URL
if ($(location).attr('hash') != "") {
try {
$( $(location).attr('hash') )
.trigger('click');
} catch(err) {}
}
});