Responsive Tabs/Accordion
Show a tab o an accordion component. Convert Tab to accordion on two conditions: 1. With CSS if the screen width is lower than 750px. 2. If the tabs width is mayor than the parent elemente fo tne component.
by Ketan Patel
HTML
<div id="tab1" class="cnt-tab-1">
<ul class="resp-tabs-list">
<li>Tab menu 1</li>
<li>Tab menu 2</li>
<li>Tab menu 3</li>
<li>Tab menu 4</li>
<li>Tab menu 5</li>
<li>Tab menu 3</li>
<li>Tab menu 4</li>
<li>Tab menu 5</li>
</ul>
<div class="resp-tabs-container">
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam modi!</p>
</div>
<div>
<p>Dolores consequatur ipsa magnam similique deserunt placeat ullam porro optio!</p>
</div>
<div>
<p>Quaerat numquam voluptas labore a obcaecati ex suscipit facilis maiores!</p>
</div>
<div>
<p>Veniam porro ad ipsam repellat minima ratione repudiandae sit numquam.</p>
</div>
<div>
<p>Mollitia odio tempore in placeat optio aliquam soluta autem impedit.</p>
</div>
</div>
</div>
CSS
body {
margin: 0px;
padding: 0px;
background: #f5f5f5;
font-family: 'Segoe UI';
padding: 20px;
}
ul.resp-tabs-list, p {
margin: 0px;
padding: 0px;
}
.resp-tabs-list li {
font-weight: 600;
font-size: 13px;
display: inline-block;
padding: 13px 15px;
margin: 0;
list-style: none;
cursor: pointer;
float: left;
}
.resp-tabs-container {
padding: 0px;
background-color: #fff;
clear: left;
}
h2.resp-accordion {
cursor: pointer;
padding: 5px;
display: none;
}
.resp-tab-content {
display: none;
padding: 15px;
}
.resp-tab-active {
border: 1px solid #c1c1c1;
border-bottom: none;
margin-bottom: -1px !important;
padding: 12px 14px 14px 14px !important;
}
.resp-tab-active {
border-bottom: none;
background-color: #fff;
}
.resp-content-active, .resp-accordion-active {
display: block;
}
.resp-tab-content {
border: 1px solid #c1c1c1;
}
h2.resp-accordion {
font-size: 13px;
border: 1px solid #c1c1c1;
border-top: 0px solid #c1c1c1;
margin: 0px;
padding: 10px 15px;
}
h2.resp-tab-active {
border-bottom: 0px solid #c1c1c1 !important;
margin-bottom: 0px !important;
padding: 10px 15px !important;
}
h2.resp-tab-title:last-child {
border-bottom: 12px solid #c1c1c1 !important;
background: blue;
}
/*-----------Vertical tabs-----------*/
.resp-vtabs ul.resp-tabs-list {
float: left;
width: 30%;
}
.resp-vtabs .resp-tabs-list li {
display: block;
padding: 15px 15px !important;
margin: 0;
cursor: pointer;
float: none;
}
.resp-vtabs .resp-tabs-container {
padding: 0px;
background-color: #fff;
border: 1px solid #c1c1c1;
float: left;
width: 68%;
min-height: 250px;
border-radius: 4px;
clear: none;
}
.resp-vtabs .resp-tab-content {
border: none;
}
.resp-vtabs li.resp-tab-active {
border: 1px solid #c1c1c1;
border-right: none;
background-color: #fff;
...
JavaScript
// Easy Responsive Tabs Plugin
// Author: Samson.Onna <Email : [email protected]>
// Modded by Lucas Dasso. <Email: [email protected]>
(function ($) {
$.fn.extend({
easyResponsiveTabs: function (options) {
//Set the default values, use comma to separate the settings, example:
var defaults = {
type: 'default', //default, vertical, accordion;
width: 'auto',
fit: true,
closed: false,
activate: function(){}
}
//Variables
var options = $.extend(defaults, options);
var opt = options, jtype = opt.type, jfit = opt.fit, jwidth = opt.width, vtabs = 'vertical', accord = 'accordion';
var hash = window.location.hash;
var historyApi = !!(window.history && history.replaceState);
//Events
$(this).bind('tabactivate', function(e, currentTab) {
if(typeof options.activate === 'function') {
options.activate.call(currentTab, e)
}
});
//Main function
this.each(function () {
var $respTabs = $(this);
var $respTabsList = $respTabs.find('ul.resp-tabs-list');
var respTabsId = $respTabs.attr('id');
$respTabs.find('ul.resp-tabs-list li').addClass('resp-tab-item');
$respTabs.css({
'display': 'block',
'width': jwidth
});
$respTabs.find('.resp-tabs-container > div').addClass('resp-tab-content');
jtab_options();
//Properties Function
function jtab_options() {
if (jtype == vtabs) {
$respTabs.addClass('resp-vtabs');
}
if (jfit == true) {
$respTabs.css({ width:...