Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use navbars as tabs in jquery mobile

This is the following code which i tried on fiddle Jsfiddle code

<div data-role="navbar">
<ul>
    <li><a href="#tab-1">Tab1</a></li>
    <li><a href="#tab-2">Tab2</a></li>
    <li><a href="#tab-3">Tab3</a></li>
</ul>
</div>

<div data-role="content">
    <div id="tab-1">
       <h2>Here is the first tab</h2>
    </div>

    <div id="tab-2">
        <h2>Here is the second tab</h2>
    </div>
    <div id="tab-3">
        <h2>Here is the third tab</h2>
    </div>
</div>
  1. I need to show the first tab & its content by default
  2. I need hide the contents of other tab when one particular tab is displayed

How to do it?

like image 568
coderslay Avatar asked Dec 28 '22 05:12

coderslay


1 Answers

Here is a sample code that use navbars as tabs.

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My Page</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script>
        var prevSelection = "tab1";
        $("#navbar ul li").live("click",function(){
            var newSelection = $(this).children("a").attr("data-tab-class");
            $("."+prevSelection).addClass("ui-screen-hidden");
            $("."+newSelection).removeClass("ui-screen-hidden");
            prevSelection = newSelection;
        });
    </script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
    <style>
        .tab-content{
            width:100%;
            height:250px;
            background-color:white;
            border-bottom-left-radius:0.5em;
            border-bottom-right-radius:0.5em;
        }
        .tab-content>div{
            padding:5px;
        }                   

    </style>
    </head> 

    <body> 
        <div data-role="page">

        <div data-role="header">
        <h1>My Title</h1>
        </div><!-- /header -->

        <div data-role="content">   
            <div data-role="navbar" id="navbar">
                <ul>
                    <li><a href="#" class="ui-btn-active" data-tab-class="tab1">Tab1</a></li>
                    <li><a href="#" data-tab-class="tab2">Tab2</a></li>
                    <li><a href="#" data-tab-class="tab3">Tab3</a></li>
                    <li><a href="#" data-tab-class="tab4">Tab4</a></li>
                </ul>
            </div>
            <div class="tab-content">
                <div class="tab1">
                    Tab1
                </div>
                <div class="tab2 ui-screen-hidden">
                    Tab2
                </div>
                <div class="tab3 ui-screen-hidden">
                    Tab3
                </div>
                <div class="tab4 ui-screen-hidden">
                    Tab4
                </div>
            </div>
        </div><!-- /content -->

        </div><!-- /page -->
    </body>
</html>

A demo - http://jsfiddle.net/m8wQM/

Let me know if it helps.

like image 85
user700284 Avatar answered Jan 08 '23 05:01

user700284