What is a Tab Based Navigation ?
Tab based navigation is where we keep all the menu items in a single page and display and hide them according to user activities.
Why We Use Tab Based Navigation ?
Tab based navigation is essential when you have to display high volume of content inside small screen space. With tabs you can load most of your content inside a single page and display and hide them whenever you want without refreshing the whole page. Also tab based navigation informs the user about what part of the application you are in and what are the other items you can visit.
Creating a Simple Tab Based Navigation
Lets start by creating a container for our tabs. I have used a div called tabbar as the container for tab menu items and tab content.
Then i have created 2 divs called tabContainer and tabcontent. Div called tabContainer is used for tab menu items and tabcontent for content of that tab.
Then i have created 2 divs called tabContainer and tabcontent. Div called tabContainer is used for tab menu items and tabcontent for content of that tab.
How to Add Tab Menu Items
Each tab menu item should be in the following format.
- Each tab menu item should be inside a div with the class tabMenuItem.
- Each div should have an anchor() tag inside.
- href attribute should have a unique id.
How to Add Tab Content
- Each tab content item should be inside a div and it should have the id as same as the name of tab menu item.
- Inside the div you can have any content you want. I have used an unordered list.
- Tab Menu 1 Item
How Tabs Work With Jquery
- First we have to hide all the tab contents.
- Class hidetab is added to all the tab contents to hide them.
- Default active tab will be #tab1. So the class hidetab is removed and displaytab is added to show the initial tab content.
// Hide all the tabs $(".tabcontent_inner>div").addClass('hidetab'); // Set the default active tab $("#tab1").removeClass('hidetab'); $("#tab1").addClass('displaytab');
- Next we have to specify what happens when you click a tab menu item.
- When you click the tab menu it will try to redirect to the location in href attribute. To prevent that we have to use e.preventDefault() function.
$(".tabContainerUl div a").click(function (e) { e.preventDefault();
- Next we have to hide all the tab content when the tab menu item is clicked.
// Hide all the tabs when the tab is clicked $(".tabcontent_inner>div").removeClass('displaytab'); $(".tabcontent_inner>div").addClass('hidetab');
- Then we display the content of clicked tab by using the following code.
// Display the clicked tab var hre=$(this).attr('href'); $(hre).addClass('displaytab');
- Next we change the active tab to the clicked tab and make other tabs inactive.
// Remove all currently active tabs $('.tabContainerUl>div a').each(function(index) { $(this).removeClass('displayActive'); }); // Activate the clicked tab $(this).addClass('displayActive'); });
Complete Source Code
- Tab Menu 1 Item
- Tab Menu 2 Item
- Tab Menu 3 Item
$(document).ready(function(){ // Hide all the tabs $(".tabcontent_inner>div").addClass('hidetab'); // Set the default active tab $("#tab1").removeClass('hidetab'); $("#tab1").addClass('displaytab'); $(".tabContainerUl div a").click(function (e) { e.preventDefault(); // Hide all the tabs when the tab is clicked $(".tabcontent_inner>div").removeClass('displaytab'); $(".tabcontent_inner>div").addClass('hidetab'); // Display the clicked tab var hre=$(this).attr('href'); $(hre).addClass('displaytab'); // Remove all currently active tabs $('.tabContainerUl>div a').each(function(index) { $(this).removeClass('displayActive'); }); // Activate the clicked tab $(this).addClass('displayActive'); }); });
Leave a Reply