05 - jQuery Mobile - Navbars

Introduction

If you are smart phone user who accesses websites using your mobile browser, you might have seen a number of navigation bars that help you navigate between screens. jQuery Mobile makes it so easy to handle navigation bars. A navigation bar is actually an unordered list of links wrapped in a div element. You just need to assign the role navbar to your div element to make it mobile optimized. Navigation bars usually appear within headers and footers.

Basic Navbar Widget

Links inside a navigation bar is automatically turned into buttons having width as its content. The basic jQuery Mobile navbar widget allows to provide up to 5 buttons in a bar. If you add only one link, then it will take 100% of the width and if you add two links, then each will take 50% and so on. If you add more than 5 links, then it will wrap into multiple lines.

Save the file as multirows.html and open it using browser. You will get a screen like this:

Here you could find that as there are more than five links, they are displayed in multiple rows. We also applied the same theme to all the buttons by adding the class ui-btn-b.

Icons and Positioning

You can also add icons to your buttons using the data-icon attribute. You can position the icons using the attribute data-iconpos and the possible values are top, right, bottom and left. Icons are placed on the top by default.

Save the file as icons.html and open it using your browser. You will get a screen like this:

Here we have added the navigation bar with the footer. You could find that all the icons are placed at the right.

Active Links

When a link is clicked, its gets a different look of being selected. If you want to achieve this look without actually clicking the link, then you can add the class ui-btn-active to the required link. If you want to give the selected look for each button that represents the page the user is on, then add the class ui-state-persist along with the ui-btn-active.

Save this file as persist.html and open it using your browser. You will get a screen like this

If you click the Cakes link, you will get a screen like this:

Here you could see that the button that represents the page you are on gets the selected look.

Summary

In this section, we have seen how to add navigation bars at the header or footer as you want. We have also see how we can add icons to the links and position them differently. We have also seen how to give the selected look to the button that represents the page the user is on.