Navbar - 图1

Navbar

Navbar is a fixed area at the top of a screen that contains Page title and navigation elements.

Navbar has 3 main parts: Left, Title and Right. Each part may contain any HTML content, but it is recommended to use them in the following way:

  • Left part is designed to be used with “back link”, icons or with single text link.

  • Title part is used to display page title or tab links (buttons row/segmented controller).

  • Right part is for the same as the Left part.

Navbar HTML Layout

Navbar layout is pretty simple and self explaining:

  1. <div class="navbar">
  2. <div class="navbar-inner">
  3. <div class="left">Left</div>
  4. <div class="title">Page Title</div>
  5. <div class="right">Right</div>
  6. </div>
  7. </div>

Note that Navbar’s Title element has lowest width priority, and when window screen will not fit all three elements Title part will be cut.

So if you use plain text in Title part it will have ellipsis (…) on the end when cuted. But you need to take care about it if you have some custom elements there.

Navbar With Links

To add links in Left or Right part you just need to add the plain <a> tag with additional link class:

  1. <div class="navbar">
  2. <div class="navbar-inner">
  3. <div class="left">
  4. <a href="#" class="link">Left Link</a>
  5. </div>
  6. <div class="title">Page Title</div>
  7. <div class="right">
  8. <a href="#" class="link">Right Link</a>
  9. </div>
  10. </div>
  11. </div>

Multiple Links

Nothing extraordinary. Just add more <a class="link" to the required part:

  1. <div class="navbar">
  2. <div class="navbar-inner">
  3. <div class="left">
  4. <a href="#" class="link">Left 1</a>
  5. <a href="#" class="link">Left 2</a>
  6. </div>
  7. <div class="title">Page Title</div>
  8. <div class="right">
  9. <a href="#" class="link">Right 1</a>
  10. </div>
  11. </div>
  12. </div>

Links With Icons + Text

Here comes a little difference. In this case we need to wrap link’s text with <span> element. It is required for correct spacing between icon and “word”, and for animation:

  1. <div class="navbar">
  2. <div class="navbar-inner">
  3. <div class="left">
  4. <a href="#" class="link">
  5. <i class="icon icon-back"></i>
  6. <span>Back</span>
  7. </a>
  8. </div>
  9. <div class="title">Title</div>
  10. <div class="right">
  11. <a href="#" class="link">
  12. <i class="icon another-icon"></i>
  13. <span>Menu</span>
  14. </a>
  15. </div>
  16. </div>
  17. </div>

Links With Icons Only

If need links with icons and without text we need to additional **icon-only** class to links. With this class link will have fixed size so we can’t miss it with finger:

  1. <div class="navbar">
  2. <div class="navbar-inner">
  3. <div class="left">
  4. <a href="#" class="link icon-only">
  5. <i class="icon icon-back"></i>
  6. </a>
  7. </div>
  8. <div class="title">Title</div>
  9. <div class="right">
  10. <a href="#" class="link icon-only">
  11. <i class="icon another-icon"></i>
  12. </a>
  13. </div>
  14. </div>
  15. </div>

Theme-specific Styling

In iOS theme Navbar has thin border on the bottom. To disable this border you need to add **no-hairline** class to navbar element:

  1. <div class="navbar no-hairline">...</div>

In MD theme Navbar has shadow. To disable this shadow you need to add **no-shadow** class to navbar element:

  1. <div class="navbar no-shadow">...</div>

Navbar Type

Now let’s look where we can place our Navbar in DOM. There are several rules to place Navbar.

Static Navbar

Static navbar position is the probably most rarely used layout type. In this case Navbar is just part of the scrollable page content:

  1. <div class="page">
  2. <!-- Scrollable page content -->
  3. <div class="page-content">
  4. <!-- Static navbar goes in the beginning inside of page-content -->
  5. <div class="navbar">...</div>
  6. ...
  7. </div>
  8. </div>

Fixed Navbar

Fixed navbar is also part of the page but it is always visible on screen not depending on page scroll. In this case it must be a direct child of page and if page has also fixed toolbar then it must be BEFORE the toolbar:

  1. <div class="page">
  2. <!-- Fixed navbar goes ALWAYS FIRST -->
  3. <div class="navbar">...</div>
  4. <!-- Fixed toolbar goes ALWAYS AFTER navbar -->
  5. <div class="toolbar">...</div>
  6. <!-- Scrollable page content -->
  7. <div class="page-content">
  8. ...
  9. </div>
  10. </div>

Fixed Navbar must always be a direct child of a page and BEFORE the toolbar (if fixed toolbar is used on this page)

Common Navbar

If we need only one common navbar for all pages in View then it must be a direct child of this view and be BEFORE all pages in view:

  1. <div class="view">
  2. <!-- View common navbar -->
  3. <div class="navbar">...</div>
  4. <!-- Pages -->
  5. <div class="page">...</div>
  6. </div>

If we need only one common navbar / navbar for all views then it must be a direct child of Views element and be BEFORE all views.

  1. <div class="views tabs">
  2. <!-- Views common navbar -->
  3. <div class="navbar">...</div>
  4. <div class="view tab tab-active" id="tab-1">...</div>
  5. <div class="view tab" id="tab-2">...</div>
  6. ...
  7. </div>

Common Navbar must always be a direct child of Views/View and BEFORE the navbar (if same common toolbar is used)

Dynamic Navbar

Dynamic Navbar is supported only in iOS Theme

One of the iOS-theme awesome features is the dynamic navbar. Navbar’s elements will slide and fade during pages transition and swipe back when dynamic navbar is enabled.

It is enabled by default when you use Fixed-positioned navbars, when Navbar is a direct child of the page.

If you want to disable it you need to pass iosDynamicNavbar: false for required View on its initialisation or in global app parameters for all views. For example:

  1. // Disable globally
  2. var app = new Framework7({
  3. view: {
  4. iosDynamicNavbar: false,
  5. },
  6. });
  7. // Disable only for view on its initialisation
  8. var mainView = app.views.create('.view-main', {
  9. iosDynamicNavbar: false,
  10. });

Note that when Dynamic Navbar (iosDynamicNavbar) is enabled and iosSeparateDynamicNavbar parameter is enabled (by default), Navbar HTML element will be detached from the page and moved under View HTML element. In this case to access page related dynamic navbar element use app.navbar.getElByPage(pageEl) method

Dynamic Navbar Layout

Dynamic Navbar layout is the same as for usual Navbar, the only difference is that you can add additional classes to the Navbar parts (Left, Title, Right) to tell which type of transition effect you want on this part:

  • By default (if there is no additional classes) each Navbar part has “Fade” transition effect

  • If you add **sliding** class to any of Navbar parts then it will have “Sliding” effect

  • If you add **sliding** class to the navbar-inner then all Navbar parts will have “Sliding” effect

  • For better look you should keep identic transition type for the same Navbar parts through different pages.

  1. <!-- No additional classes, all elements will have "fade" effect on page transition -->
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="left">
  5. <a href="#" class="link">Home Left</a>
  6. </div>
  7. <div class="title">Home</div>
  8. <div class="right">
  9. <a href="#" class="link">Home Right</a>
  10. </div>
  11. </div>
  12. </div>
  13. <!-- Title and Left will have "sliding" effect on page transition -->
  14. <div class="navbar">
  15. <div class="navbar-inner">
  16. <!-- Additional "sliding" class -->
  17. <div class="left sliding">
  18. <a href="#" class="link">Home Left</a>
  19. </div>
  20. <!-- Additional "sliding" class -->
  21. <div class="title sliding">Home</div>
  22. <div class="right">
  23. <a href="#" class="link">Home Right</a>
  24. </div>
  25. </div>
  26. </div>
  27. <!-- All parts will have "sliding" effect on page transition -->
  28. <div class="navbar">
  29. <!-- Additional "sliding" class -->
  30. <div class="navbar-inner sliding">
  31. <div class="left">
  32. <a href="#" class="link">Home Left</a>
  33. </div>
  34. <div class="title">Home</div>
  35. <div class="right">
  36. <a href="#" class="link">Home Right</a>
  37. </div>
  38. </div>
  39. </div>

Navbar App Methods

We can use following app methods available for Navbar:

app.navbar.hide(navbarEl, animate)Hide navbar
  • navbarEl - HTMLElement or string (with CSS Selector) of required navbar. Required.
  • animated - Boolean - wheter it should be hidden with animation or not. By default is true
app.navbar.show(navbarEl, isAnimated)Show navbar
  • navbarEl - HTMLElement or string (with CSS Selector) of required navbar. Required.
  • animated - Boolean - wheter it should be shown with animation or not. By default is true
app.navbar.size(navbarEl)Recalculate positional styles for Navbar elements. It could be useful after you change some of Navbar elements dynamically. This will have effect only in iOS theme
  • navbarEl - HTMLElement or string (with CSS Selector) of required navbar. Required.
app.navbar.getElByPage(pageEl)Get navbar HTML element by specified page element. Useful only when dynamic navbar is enabled. In this case it is out of the page container. This will have effect only in iOS theme
  • pageEl - HTMLElement or string (with CSS Selector) of page where to look for navbar. Required.

Navbar App Parameters

It is possible to control some default navbar behavior using global app parameters by passing navbar related parameters under navbar parameter:

ParameterTypeDefaultDescription
hideOnPageScrollbooleanfalseWill hide Navbars on page scroll
showOnPageScrollEndbooleantrueSet to true to show hidden Navbar when scrolling reaches end of the page
showOnPageScrollTopbooleantrueSet to false and hidden Navbar will not become visible when you scroll page to top everytime. They will become visible only at the most top scroll position, in the beginning of the page
scrollTopOnTitleClickbooleantrueWhen enabled then every click on navbar’s title element will scroll related page to the top
iosCenterTitlebooleantrueWhen enabled then it will try to position title at the center in iOS theme. Sometime (with some custom design) it may not needed. This will have effect only in iOS theme

For example:

  1. var app = new Framework7({
  2. navbar: {
  3. hideOnPageScroll: true,
  4. iosCenterTitle: false,
  5. },
  6. });

Control Navbar With Page Classes

Framework7 allows you to hide/show Navbar on specific page or on specific page scoll by using additional classes.

If you want to hide navbar on page scroll on some specific page use additional classes on this page’s <div class="page-content"> element:

  • **hide-bars-on-scroll** - to hide both Navbar and Toolbar on page scroll
  • **hide-navbar-on-scroll** - to hide Navbar on page scroll

To disable this behavior on specific pages you may use the following additional classes:

  • **keep-bars-on-scroll** - to keep Navbar and Toolbar on page scroll
  • **keep-navbar-on-scroll** - to keep Navbar on page scroll

For example:

  1. <div class="page">
  2. <div class="navbar">
  3. ...
  4. </div>
  5. <!-- "hide-navbar-on-scroll" class to hide Navbar -->
  6. <div class="page-content hide-navbar-on-scroll">
  7. <div class="content-block">
  8. <p>Scroll page down</p>
  9. ...
  10. </div>
  11. </div>
  12. </div>

If you have common single Navbar across all pages/views of your app you can hide/show Navbar automatically for some pages where you don’t need it.

To make it work all you need is to add **no-navbar** class to loaded page (<div class="page no-navbar">):

  1. <!-- Page has additional "no-navbar" class -->
  2. <div class="page no-navbar">
  3. <div class="page-content">
  4. ...
  5. </div>
  6. </div>

Examples

Static Navbar

  1. <div class="page">
  2. <div class="page-content">
  3. <!-- Static Navbar -->
  4. <div class="navbar">
  5. <div class="navbar-inner">
  6. <div class="left">
  7. <a class="link back">
  8. <i class="icon icon-back"></i>
  9. <span class="ios-only">Back</span>
  10. </a>
  11. </div>
  12. <div class="title">Static Navbar</div>
  13. <div class="right"></div>
  14. </div>
  15. </div>
  16. <div class="block">
  17. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque quos asperiores ut, odio numquam ab, qui, alias adipisci in magni reiciendis reprehenderit. Labore esse quae ut tempore consequatur, reprehenderit similique!</p>
  18. ...
  19. </div>
  20. </div>
  21. </div>

Fixed Navbar

  1. <div class="page">
  2. <!-- Fixed/Dynamic Navbar -->
  3. <div class="navbar">
  4. <div class="navbar-inner sliding">
  5. <div class="left">
  6. <a class="link back">
  7. <i class="icon icon-back"></i>
  8. <span class="ios-only">Back</span>
  9. </a>
  10. </div>
  11. <div class="title">Fixed Navbar</div>
  12. </div>
  13. </div>
  14. <div class="page-content">
  15. <div class="block">
  16. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque quos asperiores ut, odio numquam ab, qui, alias adipisci in magni reiciendis reprehenderit. Labore esse quae ut tempore consequatur, reprehenderit similique!</p>
  17. ...
  18. </div>
  19. </div>
  20. </div>

Navbar API

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner sliding">
  4. <div class="left">
  5. <a class="link back">
  6. <i class="icon icon-back"></i>
  7. <span class="ios-only">Back</span>
  8. </a>
  9. </div>
  10. <div class="title">Navbar API</div>
  11. </div>
  12. </div>
  13. <div class="page-content">
  14. <div class="block">
  15. <p><a class="button button-raised hide-navbar">Hide Navbar</a></p>
  16. <p><a class="button button-raised show-navbar">Show Navbar</a></p>
  17. <p>Quis omnis, quam maiores voluptates vero atque porro? Soluta modi dolore eum dolor iste eos perspiciatis vero porro aliquam officia deleniti, cumque rem, consequatur, ea ipsa temporibus dicta architecto saepe.</p>
  18. ...
  19. </div>
  20. </div>
  21. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. $$('.hide-navbar').on('click', function () {
  4. app.navbar.hide('.navbar');
  5. });
  6. $$('.show-navbar').on('click', function () {
  7. app.navbar.show('.navbar');
  8. });

Hide On Scroll

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner sliding">
  4. <div class="left">
  5. <a class="link back">
  6. <i class="icon icon-back"></i>
  7. <span class="ios-only">Back</span>
  8. </a>
  9. </div>
  10. <div class="title">Hide On Scroll</div>
  11. </div>
  12. </div>
  13. <!-- Additional "hide-navbar-on-scroll" class to hide navbar on scroll automatically -->
  14. <div class="page-content hide-navbar-on-scroll">
  15. <div class="block">
  16. <p>Quis omnis, quam maiores voluptates vero atque porro? Soluta modi dolore eum dolor iste eos perspiciatis vero porro aliquam officia deleniti, cumque rem, consequatur, ea ipsa temporibus dicta architecto saepe.</p>
  17. ...
  18. </div>
  19. </div>
  20. </div>