Just javascript tabs. Use it anywhere. No dependencies. Lightweight. Customizable.
Examples: http://andrey-hohlov.github.io/tabit/
$ npm install tabit --save
<div id="tabs">
<nav>
<a href="#tab-1">Tab 1</a>
<a href="#tab-2">Tab 2</a>
<a href="#tab-3">Tab 3</a>
</nav>
<div id="tab-1">Content of first tab</div>
<div id="tab-2">Content of second tab</div>
<div id="tab-3">Content of third tab</div>
</div>
import Tabit from 'Tabit'
const element = document.getElementById('tabs');
const options = {};
const tabit = new Tabit(element, options);
...or manually download and inject the minified script into your website.
{
buttonSelector: 'a',
contentSelector: 'div',
buttonAttribute: 'href',
contentAttribute: 'id',
buttonActiveClass: null,
contentActiveClass: null,
event: 'click',
activeIndex: 0,
toggleDisplay: true,
closable: false,
beforeInit: null,
onInit: null,
beforeChange: null,
onChange: null,
onDestroy: null
}
buttonSelector
- (string) CSS selector for tab button - trigger for change tabcontentSelector
- (string) CSS selector for tab content blockbuttonAttribute
- (string) attribute that contains a reference to the value of thecontentAttribute
, forhref
attribute will be removed first#
contentAttribute
- (string) attribute contains the value that is referred to bybuttonAttribute
buttonActiveClass
- (string) class for active tab buttoncontentActiveClass
- (string) class for active tab content blockevent
- (string | array) event or events fired on tab button, support:click
,mouseover
,touchstart
activeIndex
- (number) index of active tab on init, set-1
for no active tabtoggleDisplay
- (boolean) toggle css display property for tabs content blocks (display: none for inactive)closable
- (boolean) close active tab after clickbeforeInit
- (function) callback when instance created but not set active tab yet, arguments:Tabit instance
onInit
- (function) callback after successfully init, arguments:Tabit instance
beforeChange
- (function) callback before tab changed, arguments:current active tab
,new tab
,Tabit instance
onChange
- (function) callback after tab changed, arguments:new active tab
,Tabit instance
onDestroy
- (function) callback after instance destroyed
next()
- go to next tab, turn to first from lastprev()
- go to previous tab, turn to last from firstgetActiveTab()
- return active tabgetActiveTabIndex()
- return active tab indexgetTab(index)
- get tab by indexsetActiveTab(index)
- set active tab by indexdestroy()
- destroy Tabit instanceTabit.getInstance(element)
- get Tabit instance from element
- IE 10+
- Chrome 24+
- Firefox 23+
- Opera 15+
- Safari 7+
- Android Browser 4.4+
- iOS Safari 7.1+
Works in IE 9 with Element.prototype.classList polyfill.
Find polyfills on polyfill.io.
<divid="tabs">
<div>
<button type="button" data-target="tab-1">Tab 1</button>
<button type="button" data-target="tab-2">Tab 2</button>
<button type="button" data-target="tab-3">Tab 3</button>
</div>
<div>
<div data-content="tab-1">Content of first tab</div>
<div data-content="tab-2">Content of second tab</div>
<div data-content="tab-3">Content of third tab</div>
</div>
</div>
new Tabit(
document.getElementById('tabs'),
{
buttonSelector: '[data-target]',
contentSelector: '[data-content]',
buttonAttribute: 'data-target',
contentAttribute: 'data-content',
}
);
new Tabit(
document.getElementById('tabs'),
{
contentActiveClass: 'is-active',
toggleDisplay: false
}
);
div:not(.is-active) {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
new Tabit(
document.getElementById('tabs'),
{
event: ['mouseover', 'click']
}
);
new Tabit(
document.getElementById('tabs'),
{
toggleDisplay: false,
onInit: function (instance) {
this.tabs.forEach(function (item, i) {
if (i !== 0) $(item.content).hide();
});
},
beforeChange: function (activeTab, newTab, instance) {
if (!activeTab) return; // no animate on init
$(activeTab.content)
.stop()
.fadeOut(function () {
$(newTab.content).stop().fadeIn()
});
}
}
);
var tabit = new Tabit(document.getElementById('tabs'));
var rotate = setInterval(function() {
tabit.next();
}, 2000)
- Add instance cache
- Add
getInstance
method
- Refactoring after code review
- Change options names
- Full rewrite on ES6.
The MIT License
Copyright (c) 2017 Andrey Hohlov