W3cubDocs

/jQuery Mobile

Navbar Widget

Navbar Widgetversion added: 1.0

Description: Creates a navbar widget

QuickNavExamples

Methods

Events

Simple navbar

jQuery Mobile has a very basic navbar widget that is useful for providing up to 5 buttons with optional icons in a bar, typically within a header or footer. There is also a persistent navbar variation that works more like a tab bar that stays fixed as you navigate across pages.

A navbar is coded as an unordered list of links wrapped in a container element that has the data-role="navbar" attribute. This is an example of a two-button navbar:

<div data-role="navbar">
  <ul>
    <li><a href="a.html">One</a></li>
    <li><a href="b.html">Two</a></li>
  </ul>
</div><!-- /navbar -->

When a link in the navbar is clicked it gets the active (selected) state. The ui-btn-active class is first removed from all anchors in the navbar before it is added to the activated link. If this is a link to another page, the class will be removed again after the transition has completed.

To set an item to the active state upon initialization of the navbar, add class="ui-btn-active" to the corresponding anchor in your markup. Additionaly add a class of ui-state-persist to make the framework restore the active state each time the page is shown while it exists in the DOM. The example below shows a navbar with item "One" set to active:

<div data-role="navbar">
  <ul>
    <li><a href="a.html" class="ui-btn-active ui-state-persist">One</a></li>
    <li><a href="b.html">Two</a></li>
  </ul>
</div><!-- /navbar -->

Note that this on applies to navbars that are inside a page. If you use a true persistent toolbar that lives outside the page and want to set current item to the active state, you have to add a script that adds the class="ui-btn-active" class.

The navbar items are set to divide the space evenly so in this case, each button is 1/2 the width of the browser window:

Adding a third item will automatically make each button 1/3 the width of the browser window:

Adding a fourth more item will automatically make each button 1/4 the width of the browser window:

The navbar maxes out with 5 items, each 1/5 the width of the browser window:

If more than 5 items are added, the navbar will simply wrap to multiple lines:

Navbars with 1 item will simply render as 100%.

Navbars in headers

If you want to add a navbar to the top of the page, you can still have a page title and buttons. Just add the navbar container inside the header block, right after the title and buttons in the source order.

Navbars in footers

If you want to add a navbar to the bottom of the page so it acts more like a tab bar, simply wrap the navbar in a container with a data-role="footer"

<div data-role="footer">
  <div data-role="navbar">
    <ul>
      <li><a href="#">One</a></li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
    </ul>
  </div><!-- /navbar -->
</div><!-- /footer -->

Icons in navbars

Icons can be added to navbar items by adding the data-icon attribute specifying a standard mobile icon to each anchor. By default, icons are added above the text (data-iconpos="top"). The following examples add icons to a navbar in a footer.

The icon position is set on the navbar container instead of for individual links within for visual consistency. For example, to place the icons below the labels, add the data-iconpos="bottom" attribute to the navbar container.

<div data-role="navbar" data-iconpos="bottom">

This will result in a bottom icon alignment:

The icon position can be set to data-iconpos="left":

Or the icon position can be set to data-iconpos="right":

Using 3rd party icon sets

You can add any of the popular icon libraries like Glyphish to achieve the iOS style tab that has large icons stacked on top of text labels. All that is required is a bit of custom styles to link to the icons and position them in the navbar. Here is an example using Glyphish icons and custom styles (view page source for styles) in our navbar:

Icons by Joseph Wain / glyphish.com. Licensed under the Creative Commons Attribution 3.0 United States License.

Theming navbars

Navbars inherit the theme swatch from their parent container, just like buttons. If a navbar is placed in the header or footer toolbar, it will inherit the default toolbar swatch "a" for bars unless you set this in the markup.

Here are a few examples of navbars in various container swatches that automatically inherit their parent's swatch letter. Note that in these examples, instead of using a data-theme attribute, we're manually adding the swatch classes to apply the body swatch (ui-body-a) and the class to add the standard body padding (ui-body), but the inheritance works the same way:

To set the theme color for a navbar item, add the data-theme attribute to the individual links and specify a theme swatch. Note that applying a theme swatch to the navbar container is not supported.

Options

defaults

Type: Boolean
Default: false
Seting this option to true indicates that other widgets options have default values and causes jQuery Mobile's widget autoenhancement code to omit the step where it retrieves option values from data attributes. This can improve startup time.

This option is also exposed as a data attribute: data-defaults="true".

Code examples:

Initialize the navbar with the defaults option specified:

$( ".selector" ).navbar({
  defaults: true
});

Get or set the defaults option, after initialization:

// Getter
var defaults = $( ".selector" ).navbar( "option", "defaults" );
 
// Setter
$( ".selector" ).navbar( "option", "defaults", true );

disabled

Type: Boolean
Default: false
Disables the navbar if set to true.

This option is also exposed as a data attribute: data-disabled="true".

Code examples:

Initialize the navbar with the disabled option specified:

$( ".selector" ).navbar({
  disabled: true
});

Get or set the disabled option, after initialization:

// Getter
var disabled = $( ".selector" ).navbar( "option", "disabled" );
 
// Setter
$( ".selector" ).navbar( "option", "disabled", true );

iconpos

Type: String
Default: "top"
Positions the icon in the button. Possible values: left, right, top, bottom, none, notext. The notext value will display an icon-only button with no text feedback.
<div data-role="navbar" data-iconpos="bottom">

initSelector

Type: Selector
Default: See below

The default initSelector for the navbar widget is:

":jqmData(role='navbar')"

Note: This option is deprecated in 1.4.0 and will be removed in 1.5.0.
As of jQuery Mobile 1.4.0, the initSelector is no longer a widget option. Instead, it is declared directly on the widget prototype. Thus, you may specify a custom value by handling the mobileinit event and overwriting the initSelector on the prototype:

$( document ).on( "mobileinit", function() {
  $.mobile.navbar.prototype.initSelector = "div.custom";
});

Note: Remember to attach the mobileinit handler after you have loaded jQuery, but before you load jQuery Mobile, because the event is triggered as part of jQuery Mobile's loading process.

The value of this option is a jQuery selector string. The framework selects elements based on the value of this option and instantiates navbar widgets on each of the resulting list of elements.

(version deprecated: 1.4.0)

Events

create()Type: navbarcreate

triggered when a navbar is created
  • This method does not accept any arguments.

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the navbar with the create callback specified:

$( ".selector" ).navbar({
  create: function( event, ui ) {}
});

Bind an event listener to the navbarcreate event:

$( ".selector" ).on( "navbarcreate", function( event, ui ) {} );

Example:

A basic example of a navbar

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>navbar demo</title>
  <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
 
<div data-role="page" id="page1">
  <div data-role="header">
    <h1>jQuery Mobile Example</h1>
  </div>
  <div role="main" class="ui-content">
    <p>Some Content here</p>
  </div>
  <div data-role="footer">
    <div data-role="navbar">
      <ul>
        <li><a href="#" class="ui-btn-active">One</a></li>
        <li><a href="#">Two</a></li>
      </ul>
    </div><!-- /navbar -->
  </div><!-- /footer -->
</div><!-- /page -->
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquerymobile.com/navbar