Skip to content

Instantly share code, notes, and snippets.

@scoaband
Created December 13, 2018 18:32
Show Gist options
  • Select an option

  • Save scoaband/e611d3d86465978fbdb33a52f7a6a09c to your computer and use it in GitHub Desktop.

Select an option

Save scoaband/e611d3d86465978fbdb33a52f7a6a09c to your computer and use it in GitHub Desktop.
Jquery Tab Plugin
.container
.tab-group
section#tab1(title="Long Tab Name")
h3 Content 1
p Aliquam eleifend magna mauris, id egestas eros dictum ac. Vivamus ac turpis at nisi mattis aliquam. In hac habitasse platea dictumst.
section#tab2(title="Another Tab")
h3 Content 2
p Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id.
section#tab3(title="Tab 3")
h3 Content 3
p Vivamus sem odio, mattis vel dui aliquet, iaculis lacinia nibh. Vestibulum tincidunt, lacus vel semper pretium, nulla sapien blandit massa, et tempor turpis urna eu mi.
section#tab4(title="Another Tab")
h3 Content 4
p Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id.
section#tab5(title="Another Tab")
h3 Content 5
p Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id.
;(function(defaults, $, window, document, undefined) {
'use strict';
$.extend({
// Function to change the default properties of the plugin
// Usage:
// jQuery.tabifySetup({property:'Custom value'});
tabifySetup : function(options) {
return $.extend(defaults, options);
}
}).fn.extend({
// Usage:
// jQuery(selector).tabify({property:'value'});
tabify : function(options) {
options = $.extend({}, defaults, options);
return $(this).each(function() {
var $element, tabHTML, $tabs, $sections;
$element = $(this);
$sections = $element.children();
// Build tabHTML
tabHTML = '<ul class="tab-nav">';
$sections.each(function() {
if ($(this).attr("title") && $(this).attr("id")) {
tabHTML += '<li><a href="#' + $(this).attr("id") + '">' + $(this).attr("title") + '</a></li>';
}
});
tabHTML += '</ul>';
// Prepend navigation
$element.prepend(tabHTML);
// Load tabs
$tabs = $element.find('.tab-nav li');
// Functions
var activateTab = function(id) {
$tabs.filter('.active').removeClass('active');
$sections.filter('.active').removeClass('active');
$tabs.has('a[href="' + id + '"]').addClass('active');
$sections.filter(id).addClass('active');
}
// Setup events
$tabs.on('click', function(e){
activateTab($(this).find('a').attr('href'));
e.preventDefault();
});
// Activate first tab
activateTab($tabs.first().find('a').attr('href'));
});
}
});
})({
property : "value",
otherProperty : "value"
}, jQuery, window, document);
// Calling the plugin
$('.tab-group').tabify();
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
@import "compass/css3";
/* =========
Get Fonts */
@import url(https://fonts.googleapis.com/css?family=Quicksand);
@import url(https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
/* ================
Assign Variables */
$primary-color: #6EB590;
$secondary-color: #777;
$radius: 10px;
$border-width: 1px;
/* ===========================
Setup Mixins/Helper Classes */
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
/* ==========
Setup Page */
*, *:before, *:after { box-sizing: border-box; }
body { padding: 3em; background-image: url(https://subtlepatterns.com/patterns/retina_wood.png); color: grey; font-family: 'Quicksand', sans-serif; }
/* =================
Container Styling */
.container {
position: relative;
background: white;
padding: 3em;
border-radius: $radius;
@extend .clearfix;
}
/* ===========
Tab Styling */
.tab-group {
position: relative;
border: 1px solid #eee;
margin-top: 2.5em;
border-radius: 0 0 $radius $radius;
section {
opacity: 0;
height: 0;
padding: 0 1em;
overflow: hidden;
transition: opacity .4s ease, height .4s ease ;
}
section.active {
opacity: 1;
height: auto;
overflow: visible;
}
}
.tab-nav {
list-style: none;
margin: -2.5em -1px 0 0;
padding: 0;
@extend .clearfix;
height: 2.5em;
//background: #ff0;
overflow: hidden;
li {
display: inline;
a {
top: 1px;
position: relative;
display: block;
float: left;
border-radius: $radius $radius 0 0;
background: #eee;
line-height: 2em;
padding: 0 1em;
text-decoration: none;
color: grey;
margin-top: .5em;
margin-right: 1px;
transition: background .2s ease, line-height .2s ease, margin .2s ease;
}
&.active a {
background: $primary-color;
color: white;
line-height: 2.5em;
margin-top: 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment