Tabs
Tabs allow to simply switch between different content.
Let’s look at Tabs layout:
Where:
- - required wrapper for all tabs. If you miss this element, tabs will not work!
div class="tab"
- single tab. Should have uniqueid
attributediv class="tab tab-active"
- single active tab. Tab which is active (visible) by default, should have additional**tab-active**
class
If you put tabs inside of scrollable <div class="page-content">
then they will have mutual scrolling - scrolling one tab will basically scroll the all tabs as well. To avoid this (if this is a case), it is recommended to make each tab as page-content, in this case each tab will have own scrolling:
<div class="page">
<div class="navbar">...</div>
<!-- tabs is a direct child of page -->
<div class="tabs">
<!-- each tabs is a "page-content" -->
<div class="page-content tab tab-active" id="tab1">
... Tab 1 content goes here ...
</div>
<div class="page-content tab" id="tab2">
... Tab 2 content goes here ...
</div>
<!-- Third tab, should have "tab" class and unique id attribute -->
<div class="page-content tab" id="tab3">
... Tab 3 content goes here ...
</div>
</div>
</div>
Switching Between Tabs
After we have our tabs layout we need some contoller so user can switch between them.
To make it work we need to create links (<a>
tags) with **tab-link**
class and href
attribute equal to the id
attribute of target tab:
<!-- Link that activates first tab, has the same href attribute (#tab1) as the id attribute of first tab (tab1) -->
<a href="#tab1" class="tab-link tab-link-active">Tab 1</a>
<!-- Link that activates 2nd tab, has the same href attribute (#tab2) as the id attribute of 2nd tab (tab2) -->
<a href="#tab2" class="tab-link">Tab 2</a>
<!-- Link that activates 3rd tab, has the same href attribute (#tab2) as the id attribute of 3rd tab (tab3) -->
<a href="#tab3" class="tab-link">Tab 3</a>
As you may see, first link also has **tab-link-active**
class. It is not required, but if all such links will be on the same DOM tree level (the same-level children of mutual parent), then script will also change this tab-link-active
class on link related to the active tab. It is useful when your “active” link has different visual style (like buttons in or links in Tabbar)
Switch Multiple Tabs
Such notation as above uses ID attributes to specify tabs we need to switch to. But sometimes we may have situation when we need to switch few tabs using one tab-link, for this case we may use classes instead of IDs and **data-tab**
attribute for tab-link. For example:
<!-- Top Tabs -->
<div class="tabs tabs-top">
<div class="tab tab1 tab-active">...</div>
<div class="tab tab2">...</div>
<div class="tab tab3">...</div>
</div>
<!-- Bottom Tabs -->
<div class="tabs tabs-bottom">
<div class="tab tab1 tab-active">...</div>
<div class="tab tab2">...</div>
<div class="tab tab3">...</div>
</div>
<!-- Tabs links -->
<div>
<!-- This links will switch top and bottom tabs to .tab1 -->
<a href="#" class="tab-link tab-link-active" data-tab=".tab1">Tab 1</a>
<!-- This links will switch top and bottom tabs to .tab2 -->
<a href="#" class="tab-link" data-tab=".tab2">Tab 2</a>
<!-- This links will switch top and bottom tabs to .tab3 -->
<a href="#" class="tab-link" data-tab=".tab3">Tab 3</a>
</div>
Why single Tab could not be a seprate View with its own navigation and layout? It can, so you can just switch Views as tabs. In this case we will have kind of Tabbed app structure, where each tab represents separate View:
Animated Tabs
It is also possible to switch tabs with simple transition. This requires additional **div class="tabs-animated-wrap"**
wrapper for div class="tabs"
:
<!-- Tabs animated wrapper, required to switch tabs with transition -->
<div class="tabs-animated-wrap">
<!-- Tabs, tabs wrapper -->
<div class="tabs">
<!-- Tab 1, active by default -->
<div id="tab1" class="tab tab-active">
... Tab 1 content ...
</div>
<!-- Tab 2 -->
<div id="tab2" class="tab">
... Tab 2 content ...
</div>
<!-- Tab 3 -->
<div id="tab3" class="tab">
... Tab 3 content ...
</div>
</div>
</div>
Note that animted tabs wrapper div class="tabs-animated-wrap"
must have fixed height. By default, it is 100% height of its parent
Swipeable Tabs
It is also possible to switch tabs with swipes. This requires additional **div class="tabs-swipeable-wrap"**
wrapper for div class="tabs"
.
In this example let’s put tab links in Subnavbar and we will use page-content as tab to keep scrolling position for each tab separately:
<!-- Tabs swipeable wrapper, required to switch tabs with swipes -->
<div class="tabs-swipeable-wrap">
<!-- Tabs, tabs wrapper -->
<div class="tabs">
<!-- Tab 1, active by default -->
<div id="tab1" class="tab tab-active">
... Tab 1 content ...
</div>
<!-- Tab 2 -->
<div id="tab2" class="tab">
... Tab 2 content ...
</div>
<!-- Tab 3 -->
<div id="tab3" class="tab">
</div>
</div>
</div>
To achieve swipeable effect, swipeable tabs are actually will be converted to Swiper, so it is possible to tweak their behavior by passing using data-
attributes.
We can control tabs using the following app methods:
app.tab.show(tabEl, animate)
- tabEl - HTMLElement or string (with CSS Selector) of Tab to show. Requred
- animate - boolean - Should it become visible with animation or not (in case of animated or swipeable tabs). Optional
- This method returns object with and
oldTabEl
properties with shown and hidden tabs HTML elements
- tabEl - HTMLElement or string (with CSS Selector) of Tab to show. Requred
- tabLinkEl - HTMLElement or string (with CSS Selector) of Tab link/button to be activated with this tab. Useful to pass in case you have a complex layout to tell Framework7 which tab link/button must be activated
- animate - boolean - Should it become visible with animation or not (in case of animated or swipeable tabs). Optional
- This method returns object with
newTabEl
andoldTabEl
properties with shown and hidden tabs HTML elements
Tabs Events
Tabs will fire the following DOM events on tab elements and events on app instance:
DOM Events
There are app instance events as well:
Routable Tabs
Tabs can be routable. What routable tabs means and why is it good?
- First of all, it provides opportunity to navigate to tabs by usual links instead of so called special tab-links.
- Second, when navigating to such tab routes you can load a page with required tab opened.
- Third, with enabled Push State, the same tab will be opened when you navigate back and forward in history.
- And the last but not least, when using routable tabs you can load tab content in the same ways as for pages, i.e. using
url
,content
,template
,templateUrl
,component
orcomponentUrl
First of all we need to specify tabs routes in app routes. Let’s assume we have a page with routable tabs on /tabs/
route:
routes = [
{
// Page main route
path: '/tabs/',
// Will load page from tabs/index.html file
url: './pages/tabs/index.html',
// Pass "tabs" property to route, must be array with tab routes:
tabs: [
// First (default) tab has the same url as the page itself
{
// Tab path
path: '/',
// Tab id
id: 'tab-1',
// Fill this tab content from content string
content: `
<div class="block">
<h3>About Me</h3>
<p>...</p>
</div>
`
},
// Second tab
{
path: '/tab-2/',
id: 'tab-2',
// Fill this tab content with Ajax request:
url: './pages/tabs/tab-2.html',
},
// Third tab
{
path: '/tab-3/',
id: 'tab-3',
// Load this tab content as a component with Ajax request:
componentUrl: './pages/tabs/tab-3.html',
},
],
}
]
On the /tabs/index.html
page we may have the following structure, for example:
Almost the same as with usual tabs but with the difference that there is no more “tab-link-active” and “tab-active” classes on tab links and tabs. These classes and tabs will be switched by router. And there is a new attribute and class:
data-route-tab-id
- additional tab link attribute which is required for tabs switcher to understand which link related to the required routetabs-routable
- required additional class ontabs
element
Note that Views can not be used as Routable Tabs. Routable Tabs can be used only inside of View/Router!
Routable Tabs Events
Router will fire the following DOM events on tab elements and events on router/view/app instance when routable tab content is loaded:
DOM Events
Router Instance Events
<div class="page">
<div class="navbar">
<div class="navbar-inner sliding">
<div class="left">
<a href="#" class="link back">
<i class="icon icon-back"></i>
<span class="ios-only">Back</span>
</a>
</div>
<div class="title">Static Tabs</div>
</div>
</div>
<div class="toolbar tabbar">
<div class="toolbar-inner">
<a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
<a href="#tab-2" class="tab-link">Tab 2</a>
<a href="#tab-3" class="tab-link">Tab 3</a>
</div>
</div>
<div class="tabs">
<div id="tab-1" class="page-content tab tab-active">
<div class="block">
<p>Tab 1 content</p>
...
</div>
</div>
<div id="tab-2" class="page-content tab">
<div class="block">
<p>Tab 2 content</p>
...
</div>
</div>
<div id="tab-3" class="page-content tab">
<div class="block">
<p>Tab 3 content</p>
...
</div>
</div>
</div>
</div>
Animated Tabs
<div class="page">
<div class="navbar">
<div class="navbar-inner sliding">
<div class="left">
<i class="icon icon-back"></i>
<span class="ios-only">Back</span>
</a>
</div>
<div class="title">Animated Tabs</div>
</div>
</div>
<div class="toolbar tabbar">
<div class="toolbar-inner">
<a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
<a href="#tab-2" class="tab-link">Tab 2</a>
<a href="#tab-3" class="tab-link">Tab 3</a>
</div>
</div>
<div class="tabs-animated-wrap">
<div class="tabs">
<div id="tab-1" class="page-content tab tab-active">
<div class="block">
<p>Tab 1 content</p>
...
</div>
</div>
<div id="tab-2" class="page-content tab">
<div class="block">
<p>Tab 2 content</p>
...
</div>
</div>
<div id="tab-3" class="page-content tab">
<div class="block">
<p>Tab 3 content</p>
...
</div>
</div>
</div>
</div>
</div>
<div class="page">
<div class="navbar">
<div class="navbar-inner sliding">
<div class="left">
<a href="#" class="link back">
<i class="icon icon-back"></i>
<span class="ios-only">Back</span>
</a>
</div>
<div class="title">Swipeable Tabs</div>
</div>
</div>
<div class="toolbar tabbar">
<div class="toolbar-inner">
<a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
<a href="#tab-2" class="tab-link">Tab 2</a>
<a href="#tab-3" class="tab-link">Tab 3</a>
</div>
</div>
<div class="tabs-swipeable-wrap">
<div class="tabs">
<div id="tab-1" class="page-content tab tab-active">
<div class="block">
<p>Tab 1 content</p>
...
</div>
</div>
<div id="tab-2" class="page-content tab">
<div class="block">
<p>Tab 2 content</p>
...
</div>
</div>
<div id="tab-3" class="page-content tab">
<div class="block">
<p>Tab 3 content</p>
...
</div>
</div>
</div>
</div>
</div>
Routable Tabs
<div class="page">
<div class="navbar">
<div class="navbar-inner sliding">
<div class="left">
<a href="#" class="link back">
<i class="icon icon-back"></i>
<span class="ios-only">Back</span>
</a>
</div>
<div class="title">Tabs Routable</div>
</div>
</div>
<div class="toolbar tabbar">
<div class="toolbar-inner">
<a href="./" class="tab-link" data-route-tab-id="tab1">Tab 1</a>
<a href="tab2/" class="tab-link" data-route-tab-id="tab2">Tab 2</a>
<a href="tab3/" class="tab-link" data-route-tab-id="tab3">Tab 3</a>
</div>
</div>
<div class="tabs tabs-routable">
<div class="page-content tab" id="tab1"></div>
<div class="page-content tab" id="tab2"></div>
<div class="page-content tab" id="tab3"></div>
</div>
</div>