Tabs - 图1

Tabs

Tabs allow to simply switch between different content.

Tabs Layout

Let’s look at Tabs layout:

  1. <!-- Tabs wrapper, shoud have "tabs" class. Required element -->
  2. <div class="tabs">
  3. <!-- First tab, should have "tab" class and unique id attribute -->
  4. <!-- First tab is active by default - additional "tab-active" class -->
  5. <div class="tab tab-active" id="tab1">
  6. ... Tab 1 content goes here ...
  7. </div>
  8. <!-- Second tab, should have "tab" class and unique id attribute -->
  9. <div class="tab" id="tab2">
  10. ... Tab 2 content goes here ...
  11. </div>
  12. <!-- Third tab, should have "tab" class and unique id attribute -->
  13. <div class="tab" id="tab3">
  14. ... Tab 3 content goes here ...
  15. </div>
  16. </div>

Where:

  • div class="tabs" - required wrapper for all tabs. If you miss this element, tabs will not work!
  • div class="tab" - single tab. Should have unique id attribute
  • div class="tab tab-active" - single active tab. Tab which is active (visible) by default, should have additional tab-active class

Scrollable Tabs

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:

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <!-- tabs is a direct child of page -->
  4. <div class="tabs">
  5. <!-- each tabs is a "page-content" -->
  6. <div class="page-content tab tab-active" id="tab1">
  7. ... Tab 1 content goes here ...
  8. </div>
  9. <div class="page-content tab" id="tab2">
  10. ... Tab 2 content goes here ...
  11. </div>
  12. <!-- Third tab, should have "tab" class and unique id attribute -->
  13. <div class="page-content tab" id="tab3">
  14. ... Tab 3 content goes here ...
  15. </div>
  16. </div>
  17. </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:

  1. <!-- Link that activates first tab, has the same href attribute (#tab1) as the id attribute of first tab (tab1) -->
  2. <a href="#tab1" class="tab-link tab-link-active">Tab 1</a>
  3. <!-- Link that activates 2nd tab, has the same href attribute (#tab2) as the id attribute of 2nd tab (tab2) -->
  4. <a href="#tab2" class="tab-link">Tab 2</a>
  5. <!-- Link that activates 3rd tab, has the same href attribute (#tab2) as the id attribute of 3rd tab (tab3) -->
  6. <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 Segmented Control 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:

  1. <!-- Top Tabs -->
  2. <div class="tabs tabs-top">
  3. <div class="tab tab1 tab-active">...</div>
  4. <div class="tab tab2">...</div>
  5. <div class="tab tab3">...</div>
  6. </div>
  7. <!-- Bottom Tabs -->
  8. <div class="tabs tabs-bottom">
  9. <div class="tab tab1 tab-active">...</div>
  10. <div class="tab tab2">...</div>
  11. <div class="tab tab3">...</div>
  12. </div>
  13. <!-- Tabs links -->
  14. <div>
  15. <!-- This links will switch top and bottom tabs to .tab1 -->
  16. <a href="#" class="tab-link tab-link-active" data-tab=".tab1">Tab 1</a>
  17. <!-- This links will switch top and bottom tabs to .tab2 -->
  18. <a href="#" class="tab-link" data-tab=".tab2">Tab 2</a>
  19. <!-- This links will switch top and bottom tabs to .tab3 -->
  20. <a href="#" class="tab-link" data-tab=".tab3">Tab 3</a>
  21. </div>

Where data-tab attribute of tab-link with CSS selector of target tab/tabs

Views As Tabs

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:

  1. <body>
  2. <!-- App root -->
  3. <div id="app">
  4. <!-- Views/Tabs container -->
  5. <div class="views tabs">
  6. <!--
  7. Tabbar for switching views-tabs. Should be a first child in Views.
  8. Additional "toolbar-bottom" class to set it on bottom
  9. -->
  10. <div class="toolbar tabbar toolbar-bottom">
  11. <div class="toolbar-inner">
  12. <a href="#view-home" class="tab-link tab-link-active">
  13. <i class="icon icon-home"></i>
  14. </a>
  15. <a href="#view-catalog" class="tab-link">
  16. <i class="icon icon-catalog"></i>
  17. </a>
  18. <a href="#view-settings" class="tab-link">
  19. <i class="icon icon-settings"></i>
  20. </a>
  21. </div>
  22. </div>
  23. <!-- Your main view/tab, should have "view-main" class. It also has "tab-active" class -->
  24. <div id="view-home" class="view view-main tab tab-active">
  25. ...
  26. </div>
  27. <!-- Catalog View -->
  28. <div id="view-catalog" class="view tab">
  29. ...
  30. </div>
  31. <!-- Settings View -->
  32. <div id="view-settings" class="view tab">
  33. ...
  34. </div>
  35. </div>
  36. </div>
  37. ...
  38. </body>

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":

  1. <!-- Tabs animated wrapper, required to switch tabs with transition -->
  2. <div class="tabs-animated-wrap">
  3. <!-- Tabs, tabs wrapper -->
  4. <div class="tabs">
  5. <!-- Tab 1, active by default -->
  6. <div id="tab1" class="tab tab-active">
  7. ... Tab 1 content ...
  8. </div>
  9. <!-- Tab 2 -->
  10. <div id="tab2" class="tab">
  11. ... Tab 2 content ...
  12. </div>
  13. <!-- Tab 3 -->
  14. <div id="tab3" class="tab">
  15. ... Tab 3 content ...
  16. </div>
  17. </div>
  18. </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:

  1. <!-- Tabs swipeable wrapper, required to switch tabs with swipes -->
  2. <div class="tabs-swipeable-wrap">
  3. <!-- Tabs, tabs wrapper -->
  4. <div class="tabs">
  5. <!-- Tab 1, active by default -->
  6. <div id="tab1" class="tab tab-active">
  7. ... Tab 1 content ...
  8. </div>
  9. <!-- Tab 2 -->
  10. <div id="tab2" class="tab">
  11. ... Tab 2 content ...
  12. </div>
  13. <!-- Tab 3 -->
  14. <div id="tab3" class="tab">
  15. ... Tab 3 content ...
  16. </div>
  17. </div>
  18. </div>

To achieve swipeable effect, swipeable tabs are actually will be converted to Swiper, so it is possible to tweak their behavior by passing Swiper parameters using data- attributes.

Tabs App Methods

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 newTabEl and oldTabEl properties with shown and hidden tabs HTML elements

app.tab.show(tabEl, tabLinkEl, animate)

  • 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 and oldTabEl 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

EventTargetDescription
tab:showTab Element<div class=”tab”>Event will be triggered when Tab becomes visible/active
tab:hideTab Element<div class=”tab”>Event will be triggered when Tab becomes hidden/inactive
page:tabshowPage Element<div class=”page”>Event will be triggered when page’s parent View as Tab becomes visible
page:tabhidePage Element<div class=”page”>Event will be triggered when page’s parent View as Tab becomes hidden

App Instance Events

There are app instance events as well:

EventArgumentsTargetDescription
tabShowtabElappEvent will be triggered when Tab becomes visible/active. As an argument event handler receives tab that became visible
tabHidetabElappEvent will be triggered when Tab becomes hidden/inactive. As an argument event handler receives tab that became hidden
pageTabShowpageElappEvent will be triggered when page’s parent View as Tab becomes visible. As an argument event handler receives page element which is a child of the View-Tab that became visible
pageTabHidepageElappEvent will be triggered when page’s parent View as Tab becomes hidden. As an argument event handler receives page element which is a child of the View-Tab that became hidden

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 or componentUrl

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:

  1. routes = [
  2. {
  3. // Page main route
  4. path: '/tabs/',
  5. // Will load page from tabs/index.html file
  6. url: './pages/tabs/index.html',
  7. // Pass "tabs" property to route, must be array with tab routes:
  8. tabs: [
  9. // First (default) tab has the same url as the page itself
  10. {
  11. // Tab path
  12. path: '/',
  13. // Tab id
  14. id: 'tab-1',
  15. // Fill this tab content from content string
  16. content: `
  17. <div class="block">
  18. <h3>About Me</h3>
  19. <p>...</p>
  20. </div>
  21. `
  22. },
  23. // Second tab
  24. {
  25. path: '/tab-2/',
  26. id: 'tab-2',
  27. // Fill this tab content with Ajax request:
  28. url: './pages/tabs/tab-2.html',
  29. },
  30. // Third tab
  31. {
  32. path: '/tab-3/',
  33. id: 'tab-3',
  34. // Load this tab content as a component with Ajax request:
  35. componentUrl: './pages/tabs/tab-3.html',
  36. },
  37. ],
  38. }
  39. ]

On the /tabs/index.html page we may have the following structure, for example:

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner">
  5. <div class="title">Routable Tabs</div>
  6. </div>
  7. </div>
  8. <div class="toolbar tabbar toolbar-bottom">
  9. <div class="toolbar-inner">
  10. <!-- additional "data-route-tab-id" must match to tab's ID in the specified routes -->
  11. <a href="/" class="tab-link" data-route-tab-id="tab-1">Tab 1</a>
  12. <a href="/tab-2/" class="tab-link" data-route-tab-id="tab-2">Tab 2</a>
  13. <a href="/tab-3/" class="tab-link" data-route-tab-id="tab-3">Tab 3</a>
  14. </div>
  15. </div>
  16. <!-- Additional "tabs-routable" is required on tabs -->
  17. <div class="tabs tabs-routable">
  18. <div class="tab page-content" id="tab-1"></div>
  19. <div class="tab page-content" id="tab-2"></div>
  20. <div class="tab page-content" id="tab-3"></div>
  21. </div>
  22. </div>

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 route
  • tabs-routable - required additional class on tabs 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

EventTargetDescription
tab:init
tab:mounted
Tab Element<div class=”tab”>Event will be triggered right after routable Tab content will be loaded
tab:beforeremoveTab Element<div class=”tab”>Event will be triggered right before routab Tab content will be removed

Router Instance Events

Router instance emits events on both self instance, then on its parent view instance and app instance:

EventTargetArgumentsDescription
tabInit
tabMounted
router
view
app
(newTabEl, tabRoute)Event will be triggered right after routable Tab content will be loaded. As an argument event handler receives:
  • newTabEl - tab HTML element where route content was just loaded (new tab)
  • tabRoute - new tab route
tabBeforeRemoverouter
view
app
(oldTabEl, newTabEl, tabRoute)Event will be triggered right before routable Tab content will be removed. As an argument event handler receives:
  • oldTabEl - tab HTML element where route content was just removed (old tab)
  • newTabEl - tab HTML element where route content was just loaded (new tab)
  • tabRoute - new tab route

Examples

Static Tabs

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner sliding">
  5. <div class="left">
  6. <a href="#" class="link back">
  7. <i class="icon icon-back"></i>
  8. <span class="if-not-md">Back</span>
  9. </a>
  10. </div>
  11. <div class="title">Static Tabs</div>
  12. </div>
  13. </div>
  14. <div class="toolbar tabbar toolbar-bottom">
  15. <div class="toolbar-inner">
  16. <a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
  17. <a href="#tab-2" class="tab-link">Tab 2</a>
  18. <a href="#tab-3" class="tab-link">Tab 3</a>
  19. </div>
  20. </div>
  21. <div class="tabs">
  22. <div id="tab-1" class="page-content tab tab-active">
  23. <div class="block">
  24. <p>Tab 1 content</p>
  25. ...
  26. </div>
  27. </div>
  28. <div id="tab-2" class="page-content tab">
  29. <div class="block">
  30. <p>Tab 2 content</p>
  31. ...
  32. </div>
  33. </div>
  34. <div id="tab-3" class="page-content tab">
  35. <div class="block">
  36. <p>Tab 3 content</p>
  37. ...
  38. </div>
  39. </div>
  40. </div>
  41. </div>

Animated Tabs

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner sliding">
  5. <div class="left">
  6. <a href="#" class="link back">
  7. <i class="icon icon-back"></i>
  8. <span class="if-not-md">Back</span>
  9. </a>
  10. </div>
  11. <div class="title">Animated Tabs</div>
  12. </div>
  13. </div>
  14. <div class="toolbar tabbar toolbar-bottom">
  15. <div class="toolbar-inner">
  16. <a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
  17. <a href="#tab-2" class="tab-link">Tab 2</a>
  18. <a href="#tab-3" class="tab-link">Tab 3</a>
  19. </div>
  20. </div>
  21. <div class="tabs-animated-wrap">
  22. <div class="tabs">
  23. <div id="tab-1" class="page-content tab tab-active">
  24. <div class="block">
  25. <p>Tab 1 content</p>
  26. ...
  27. </div>
  28. </div>
  29. <div id="tab-2" class="page-content tab">
  30. <div class="block">
  31. <p>Tab 2 content</p>
  32. ...
  33. </div>
  34. </div>
  35. <div id="tab-3" class="page-content tab">
  36. <div class="block">
  37. <p>Tab 3 content</p>
  38. ...
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>

Swipeable Tabs

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner sliding">
  5. <div class="left">
  6. <a href="#" class="link back">
  7. <i class="icon icon-back"></i>
  8. <span class="if-not-md">Back</span>
  9. </a>
  10. </div>
  11. <div class="title">Swipeable Tabs</div>
  12. </div>
  13. </div>
  14. <div class="toolbar tabbar toolbar-bottom">
  15. <div class="toolbar-inner">
  16. <a href="#tab-1" class="tab-link tab-link-active">Tab 1</a>
  17. <a href="#tab-2" class="tab-link">Tab 2</a>
  18. <a href="#tab-3" class="tab-link">Tab 3</a>
  19. </div>
  20. </div>
  21. <div class="tabs-swipeable-wrap">
  22. <div class="tabs">
  23. <div id="tab-1" class="page-content tab tab-active">
  24. <div class="block">
  25. <p>Tab 1 content</p>
  26. ...
  27. </div>
  28. </div>
  29. <div id="tab-2" class="page-content tab">
  30. <div class="block">
  31. <p>Tab 2 content</p>
  32. ...
  33. </div>
  34. </div>
  35. <div id="tab-3" class="page-content tab">
  36. <div class="block">
  37. <p>Tab 3 content</p>
  38. ...
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>

Routable Tabs

  1. var app = new Framework7({
  2. routes: [
  3. {
  4. path: '/tabs-routable/',
  5. template: tabsRoutable,
  6. tabs: [
  7. {
  8. path: '/',
  9. id: 'tab1',
  10. content: ' \
  11. <div class="block"> \
  12. <p>Tab 1 content</p> \
  13. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ullam enim quia molestiae facilis laudantium voluptates obcaecati officia cum, sit libero commodi. Ratione illo suscipit temporibus sequi iure ad laboriosam accusamus?</p> \
  14. <p>Saepe explicabo voluptas ducimus provident, doloremque quo totam molestias! Suscipit blanditiis eaque exercitationem praesentium reprehenderit, fuga accusamus possimus sed, sint facilis ratione quod, qui dignissimos voluptas! Aliquam rerum consequuntur deleniti.</p> \
  15. <p>Totam reprehenderit amet commodi ipsum nam provident doloremque possimus odio itaque, est animi culpa modi consequatur reiciendis corporis libero laudantium sed eveniet unde delectus a maiores nihil dolores? Natus, perferendis.</p> \
  16. </div> \
  17. ',
  18. },
  19. {
  20. path: '/tab2/',
  21. id: 'tab2',
  22. content: '\
  23. <div class="block"> \
  24. <p>Tab 2 content</p> \
  25. <p>Suscipit, facere quasi atque totam. Repudiandae facilis at optio atque, rem nam, natus ratione cum enim voluptatem suscipit veniam! Repellat, est debitis. Modi nam mollitia explicabo, unde aliquid impedit! Adipisci!</p> \
  26. <p>Deserunt adipisci tempora asperiores, quo, nisi ex delectus vitae consectetur iste fugiat iusto dolorem autem. Itaque, ipsa voluptas, a assumenda rem, dolorum porro accusantium, officiis veniam nostrum cum cumque impedit.</p> \
  27. <p>Laborum illum ipsa voluptatibus possimus nesciunt ex consequatur rem, natus ad praesentium rerum libero consectetur temporibus cupiditate atque aspernatur, eaque provident eligendi quaerat ea soluta doloremque. Iure fugit, minima facere.</p> \
  28. </div> \
  29. ',
  30. },
  31. {
  32. path: '/tab3/',
  33. id: 'tab3',
  34. content: '\
  35. <div class="block"> \
  36. <p>Tab 3 content</p> \
  37. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ullam enim quia molestiae facilis laudantium voluptates obcaecati officia cum, sit libero commodi. Ratione illo suscipit temporibus sequi iure ad laboriosam accusamus?</p> \
  38. <p>Deserunt adipisci tempora asperiores, quo, nisi ex delectus vitae consectetur iste fugiat iusto dolorem autem. Itaque, ipsa voluptas, a assumenda rem, dolorum porro accusantium, officiis veniam nostrum cum cumque impedit.</p> \
  39. <p>Laborum illum ipsa voluptatibus possimus nesciunt ex consequatur rem, natus ad praesentium rerum libero consectetur temporibus cupiditate atque aspernatur, eaque provident eligendi quaerat ea soluta doloremque. Iure fugit, minima facere.</p> \
  40. </div> \
  41. ',
  42. },
  43. ],
  44. },
  45. ]
  46. });
  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner sliding">
  5. <div class="left">
  6. <a href="#" class="link back">
  7. <i class="icon icon-back"></i>
  8. <span class="if-not-md">Back</span>
  9. </a>
  10. </div>
  11. <div class="title">Tabs Routable</div>
  12. </div>
  13. </div>
  14. <div class="toolbar tabbar toolbar-bottom">
  15. <div class="toolbar-inner">
  16. <a href="./" class="tab-link" data-route-tab-id="tab1">Tab 1</a>
  17. <a href="tab2/" class="tab-link" data-route-tab-id="tab2">Tab 2</a>
  18. <a href="tab3/" class="tab-link" data-route-tab-id="tab3">Tab 3</a>
  19. </div>
  20. </div>
  21. <div class="tabs tabs-routable">
  22. <div class="page-content tab" id="tab1"></div>
  23. <div class="page-content tab" id="tab2"></div>
  24. <div class="page-content tab" id="tab3"></div>
  25. </div>
  26. </div>

← Swiper

Text Editor →