The MDC Tabs component contains components which are used to create spec-aligned tabbed navigation components adhering to the Material Design tabs guidelines. These components are:
Source: Material Docs
- mdc-tab: The individual tab elements
- mdc-tab-bar: The main component which is composed of
mdc-tab
elements - mdc-tab-bar-scroller: The component which controls the horizontal scrolling behavior of an
mdc-tab-bar
that overflows its container
Example:
Tabs
<section id="dynamic-demo-toolbar"> <nav id="dynamic-tab-bar" class="mdc-tab-bar mdc-tab-bar--indicator-accent" role="tablist"> <a role="tab" aria-controls="panel-1" class="mdc-tab mdc-tab--active" href="javascript:void(0)">Item One</a> <a role="tab" aria-controls="panel-2" class="mdc-tab" href="javascript:void(0)">
Item Two
</a> <a role="tab" aria-controls="panel-3" class="mdc-tab" href="javascript:void(0)">Item Three</a> <span class="mdc-tab-bar__indicator"></span> </nav> </section>
Tabs Content:
<section>
<div class="panels">
<div class="panel active" id="panel-1" role="tabpanel" aria-hidden="false">
Item One
</div>
<div class="panel" id="panel-2" role="tabpanel" aria-hidden="true">
Item Two
</div>
<div class="panel" id="panel-3" role="tabpanel" aria-hidden="true">Item Three</div>
</div>
</section>
Script:
(function() {
setTimeout(function () {
var dynamicTabBar = window.dynamicTabBar = new mdc.tabs.MDCTabBar(document.querySelector('#dynamic-tab-bar'));
var panels = document.querySelector('.panels');
dynamicTabBar.preventDefaultOnClick = true;
function updatePanel(index) {
var activePanel = panels.querySelector('.panel.active');
if (activePanel) {
activePanel.classList.remove('active');
}
var newActivePanel = panels.querySelector('.panel:nth-child(' + (index + 1) + ')');
if (newActivePanel) {
newActivePanel.classList.add('active');
}
}
dynamicTabBar.listen('MDCTabBar:change', function (t) {
var dynamicTabBar = t.detail;
var nthChildIndex = dynamicTabBar.activeTabIndex;
updatePanel(nthChildIndex);
});
},200)
})();
Working Demo
Item One
Source: Material Docs
Comments
Post a Comment