jQuery Mobile Navigation Tabs

前端 未结 6 944
遇见更好的自我
遇见更好的自我 2020-12-25 15:39

I want to have a tab-navigation in my jQuery Mobile project. I know I can use the data-role \'navbar\' but I only want to change the content below that navbar without swipin

相关标签:
6条回答
  • 2020-12-25 15:52

    I liked @Ryan-Haney's answer, but thought I'd add my own rough draft in, if anyone can find a more efficient way of doing this, then please add a comment.. thanks

    I did it this way because I have a bunch of "include" files that get loaded into the DOM at runtime, so I couldn't hard-code that the n-th tab is highlighted/active for each page like Ryan could. I also do have the luxury of having only a single tabbar in my app.

    $(document).delegate('.ui-navbar a', 'tap', function ()
    {
      $('.ui-navbar').find('li').find('a').removeClass('ui-btn-active');
      $('.ui-navbar').find('li:nth-child(' + ($(this).parent().index() + 1) + ')').find('a').addClass('ui-btn-active');
    });
    
    0 讨论(0)
  • 2020-12-25 15:54

    UPDATE: Check out my jsfiddle at http://jsfiddle.net/ryanhaney/eLENj/

    I just spent some time figuring this out, so I thought I would answer this. Note I am using multi-page single file, YMMV.

    <div data-role="footer" data-position="fixed">
        <div data-role="navbar">
            <ul>
                <li><a href="#page-1" data-role="tab" data-icon="grid">Page 1</a></li>
                <li><a href="#page-2" data-role="tab" data-icon="grid">Page 2</a></li>
                <li><a href="#page-3" data-role="tab" data-icon="grid">Page 3</a></li>
            </ul>
        </div>
    </div>
    
    $("div[data-role=page]").bind("pagebeforeshow", function () {
        // prevents a jumping "fixed" navbar
        $.mobile.silentScroll(0);
    });
    
    $("a[data-role=tab]").each(function () {
            // bind to click of each anchor
            var anchor = $(this);
            anchor.bind("click", function () {
                // change the page, optionally with transitions
                // but DON'T navigate...
                $.mobile.changePage(anchor.attr("href"), {
                    transition: "none",
                    changeHash: false
            });
    
            // cancel the click event
            return false;
        });
    });
    
    0 讨论(0)
  • 2020-12-25 15:58

    I noticed that the question was asked four years ago, so i'm not sure whether the Tab widget were available with JQ Mobile at that time. anyway i'm a guy from 2015

    the awesome solution that i use as below with Jquery Mobile 1.4.5

    <div data-role="tabs" id="tabs">
      <div data-role="navbar">
        <ul>
          <li><a href="#one" data-ajax="false">one</a></li>
          <li><a href="#two" data-ajax="false">two</a></li>
          <li><a href="ajax-content-ignore.html" data-ajax="false">three</a></li>
        </ul>
      </div>
      <div id="one" class="ui-body-d ui-content">
        <h1>First tab contents</h1>
      </div>
      <div id="two">
        <ul data-role="listview" data-inset="true">
            <li><a href="#">Acura</a></li>
            <li><a href="#">Audi</a></li>
            <li><a href="#">BMW</a></li>
            <li><a href="#">Cadillac</a></li>
            <li><a href="#">Ferrari</a></li>
        </ul>
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-25 16:04

    You can use the jQuery Mobile navbar styling but use your own click-handler so instead of changing pages the click will just hide/show the proper content on the same page.

    HTML

    <div data-role="navbar">
        <ul>
            <li><a href="#" data-href="a">One</a></li>
            <li><a href="#" data-href="b">Two</a></li>
        </ul>
    </div><!-- /navbar -->
    <div class="content_div">onLoad Content</div>
    <div id="a" class="content_div">Some 'A' Content</div>
    <div id="b" class="content_div">Some 'B' Content</div>
    

    JAVASCRIPT

    $(document).delegate('[data-role="navbar"] a', 'click', function () {
        $(this).addClass('ui-btn-active');
        $('.content_div').hide();
        $('#' + $(this).attr('data-href')).show();
        return false;//stop default behavior of link
    });
    

    CSS

    .content_div {
        display: none;
    }
    .content_div:first-child {
        display: block;
    }
    

    Here is a jsfiddle of the above code: http://jsfiddle.net/3RJuX/

    NOTE:

    • Each of the links in the navbar have a "data-href" attribute set to the id of the div (or whatever container you want to use) that will be displayed.

    Update

    After 1 year I came back to this answer and noticed that the delegated event handler selector can be optimized a bit to utilize a class rather than an attribute (which is a lot faster of a lookup):

    $(document).delegate('.ui-navbar a', 'click', function () {
        $(this).addClass('ui-btn-active');
        $('.content_div').hide();
        $('#' + $(this).attr('data-href')).show();
    });
    

    Update

    This code can be made to be more modular by using relative selectors rather than absolute ones (like $('.content_div'), as this will select all matching elements in the DOM rather than just ones relative to the button clicked).

    //same selector here
    $(document).delegate('.ui-navbar ul li > a', 'click', function () {
    
        //un-highlight and highlight only the buttons in the same navbar widget
        $(this).closest('.ui-navbar').find('a').removeClass('ui-navbar-btn-active');
    
        //this bit is the same, you could chain it off of the last call by using two `.end()`s
        $(this).addClass('ui-navbar-btn-active');
    
        //this starts the same but then only selects the sibling `.content_div` elements to hide rather than all in the DOM
        $('#' + $(this).attr('data-href')).show().siblings('.content_div').hide();
    });​
    

    This allows you to nest tabs and/or have multiple sets of tabs on a pages or pseudo-pages.

    Some documentation for the "relative selectors" used:

    • .closest() : http://api.jquery.com/closest
    • .siblings() : http://api.jquery.com/siblings

    Here was an example: http://jsfiddle.net/Cfbjv/25/ (It's offline now)

    0 讨论(0)
  • 2020-12-25 16:04

    Please refers this below link for all kind of nav bar in jquery

    http://jquerymobile.com/demos/1.0rc2/docs/toolbars/docs-navbar.html

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

    thanks

    0 讨论(0)
  • 2020-12-25 16:15

    @Mike Bartlett

    I struggled with this myself but after breaking Jasper's code down it looks like there is a slight nuance from his posted code and that on the jsfiddle page.

    Where he has posted

    $(document).delegate('[data-role="navbar"] a', 'click', function () {
    $(this).addClass('ui-btn-active');
    $('.content_div').hide();
    $('#' + $(this).attr('data-href')).show(); });
    

    I found it useful to change the last line to simply call whatever content you set the "data-href" value to be in your navbar.

    $('div[data-role="navbar"] a').live('click', function () {
        $(this).addClass('ui-btn-active');
        $('div.content_div').hide();
        $($(this).attr('data-href')).show();
      });
    

    my navbar html then reads

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

    Which is pretty much the same as his but for some reason I got no "error loading page" message. Hope that helps...

    0 讨论(0)
提交回复
热议问题