SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: Jquery tabs

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery tabs

    Hey,

    With the following code, how can I select which tab to open using hyper links?

    Code:
    // Easy Responsive Tabs Plugin
    // Author: Samson.Onna <Email : samson3d@gmail.com>
    (function ($) {
        $.fn.extend({
            easyResponsiveTabs: function (options) {
                //Set the default values, use comma to separate the settings, example:
                var defaults = {
                    type: 'default', //default, vertical, accordion;
                    width: 'auto',
                    fit: true
                }
                //Variables
                var options = $.extend(defaults, options);            
                var opt = options, jtype = opt.type, jfit = opt.fit, jwidth = opt.width, vtabs = 'vertical', accord = 'accordion';
    
                //Main function
                this.each(function () {
                    var $respTabs = $(this);
                    $respTabs.find('ul.resp-tabs-list li').addClass('resp-tab-item');
                    $respTabs.css({
                        'display': 'block',
                        'width': jwidth
                    });
    
                    $respTabs.find('.resp-tabs-container > div').addClass('resp-tab-content');
                    jtab_options();
                    //Properties Function
                    function jtab_options() {
                        if (jtype == vtabs) {
                            $respTabs.addClass('resp-vtabs');
                        }
                        if (jfit == true) {
                            $respTabs.css({ width: '100%', margin: '0px' });
                        }
                        if (jtype == accord) {
                            $respTabs.addClass('resp-easy-accordion');
                            $respTabs.find('.resp-tabs-list').css('display', 'none');
                        }
                    }
    
                    //Assigning the h2 markup to accordion title
                    var $tabItemh2;
                    $respTabs.find('.resp-tab-content').before("<h2 class='resp-accordion' role='tab'><span class='resp-arrow'></span></h2>");
    
                    var itemCount = 0;
                    $respTabs.find('.resp-accordion').each(function () {
                        $tabItemh2 = $(this);
                        var innertext = $respTabs.find('.resp-tab-item:eq(' + itemCount + ')').html();
                        $respTabs.find('.resp-accordion:eq(' + itemCount + ')').append(innertext);
                        $tabItemh2.attr('aria-controls', 'tab_item-' + (itemCount));
                        itemCount++;
                    });
    
                    //Assigning the 'aria-controls' to Tab items
                    var count = 0,
                        $tabContent;
                    $respTabs.find('.resp-tab-item').each(function () {
                        $tabItem = $(this);
                        $tabItem.attr('aria-controls', 'tab_item-' + (count));
                        $tabItem.attr('role', 'tab');
    
                        //First active tab                   
                        $respTabs.find('.resp-tab-item').first().addClass('resp-tab-active');
                        $respTabs.find('.resp-accordion').first().addClass('resp-tab-active');
                        $respTabs.find('.resp-tab-content').first().addClass('resp-tab-content-active').attr('style', 'display:block');
    
                        //Assigning the 'aria-labelledby' attr to tab-content
                        var tabcount = 0;
                        $respTabs.find('.resp-tab-content').each(function () {
                            $tabContent = $(this);
                            $tabContent.attr('aria-labelledby', 'tab_item-' + (tabcount));
                            tabcount++;
                        });
                        count++;
                    });
    
                    //Tab Click action function
                    $respTabs.find("[role=tab]").each(function () {
                        var $currentTab = $(this);
                        $currentTab.click(function () {
    
                            var $tabAria = $currentTab.attr('aria-controls');
    
                            if ($currentTab.hasClass('resp-accordion') && $currentTab.hasClass('resp-tab-active')) {
                                $respTabs.find('.resp-tab-content-active').slideUp('', function () { $(this).addClass('resp-accordion-closed'); });
                                $currentTab.removeClass('resp-tab-active');
                                return false;
                            }
                            if (!$currentTab.hasClass('resp-tab-active') && $currentTab.hasClass('resp-accordion')) {
                                $respTabs.find('.resp-tab-active').removeClass('resp-tab-active');
                                $respTabs.find('.resp-tab-content-active').slideUp().removeClass('resp-tab-content-active resp-accordion-closed');
                                $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');
    
                                $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').slideDown().addClass('resp-tab-content-active');
                            } else {
                                $respTabs.find('.resp-tab-active').removeClass('resp-tab-active');
                                $respTabs.find('.resp-tab-content-active').removeAttr('style').removeClass('resp-tab-content-active').removeClass('resp-accordion-closed');
                                $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');
                                $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').addClass('resp-tab-content-active').attr('style', 'display:block');
                            }
                        });
                        //Window resize function                   
                        $(window).resize(function () {
                            $respTabs.find('.resp-accordion-closed').removeAttr('style');
                        });
                    });
                });
            }
        });
    })(jQuery);
    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Do you mean which tab should display when the page opens?

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By default it opens to the first tab..

    I want to use a link to make a specific tab open.. I guess something like --> www.domainname.com/page.html#tabid

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Could you post a link to a page where I could see this in action?

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://webtrendset.com/demo/easy-res...abs/Index.html

    Not a lot of info on how to further advance the script.. ie linking to specific tabs across the url..

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    The currently active tab has the class of "resp-tab-active", you can use this to create be able to link to a specific tab.

    First add links to the tab handles:

    HTML Code:
    <ul class="resp-tabs-list">
      <li><a href="#tab1">Responsive Tab-1</a></li>
      <li><a href="#tab2">Responsive Tab-2</a></li>
      <li><a href="#tab3">Responsive Tab-3</a></li>
    </ul>
    Then we need to get the hash from the url and apply the correct class:

    Code JavaScript:
    var hash = window.location.hash,
        lis = $("ul.resp-tabs-list > li");
    lis.removeClass("resp-tab-active");
    $("a[href='" + hash + "']").addClass("resp-tab-active");

    I made a demo page to show it working.

    Link to tab one
    Link to tab two
    Link to tab three

    I hope that helps you.

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Thinking about it, you should probably check for the existence of a hash first, before removing any classes.
    That way, you can still link to index.html (as opposedto index.html#tab1)

    e.g. (untested)

    Code JavaScript:
    var hash = window.location.hash,
        lis = $("ul.resp-tabs-list > li");
     
    if (hash){
      lis.removeClass("resp-tab-active");
      $("a[href='" + hash + "']").addClass("resp-tab-active");
    }

  8. #8
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think I have a handle in it..

    A couple of glitches.. The first tab doesn't load as default if no handle is sent across the url and <a href="./index.html#tab2">OUR STAFF</a> doesn't reload page at all if clicked while on the index page, its seems like it has broken the css now too.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by _matrix_ View Post
    The first tab doesn't load as default if no handle is sent across the url
    See my comment above.

    Quote Originally Posted by _matrix_ View Post
    <a href="./index.html#tab2">OUR STAFF</a> doesn't reload page at all if clicked while on the index page, its seems like it has broken the css now too.
    Can you post a link to your page?

  10. #10
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by _matrix_ View Post
    <a href="./index.html#tab2">OUR STAFF</a> doesn't reload page at all if clicked while on the index page
    I see what you mean - basically that the url doesn't change from http://www.yoursite.co.nz/Navigation/ to http://www.yoursite.co.nz/Navigation/#tab2
    This only seems to happen intermittently, which is a bit weird and will be something to do with how the plugin handles the clicks.

    If this is important to you, then change your code thus:

    Code JavaScript:
    $('#horizontalTab').easyResponsiveTabs({
      type: 'default',        
      width: 'auto',
      fit: true 
    });
     
    var hash = window.location.hash,
        lis = $("ul.resp-tabs-list > li"),
        anchors = $("ul.resp-tabs-list").find("a");
     
    lis.removeClass("resp-tab-active");
    $("a[href='" + hash + "']").addClass("resp-tab-active");
     
    anchors.click(function(){
      window.location.hash = $(this).attr("href");
    });

    Quote Originally Posted by _matrix_ View Post
    its seems like it has broken the css now too.
    You'll have to adapt the CSS to suit (now that you have added anchor tags to the tab handles)

    E.g.

    Code CSS:
    li.resp-tab-item > a { text-decoration: none; }

  12. #12
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I cant seem to sort it with the code above.. Where should I be placing that code? In the easyResponsiveTabs.js or replace script at footer of the index.html file?

    Thanks

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by _matrix_ View Post
    Where should I be placing that code? In the easyResponsiveTabs.js or replace script at footer of the index.html file?
    The latter.

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Sorry for the delay in getting back to you.

    Place this code just before the closing body tag of your page and you will see what I am trying to demonstrate:

    HTML Code:
    <script>
      $("a[href='#tab2']").click(function(){
        $("a#bg2").trigger("mouseover");
      });
    </script>
    To trigger the effect, you will then have to click on the "Our Staff" tab when one of the menus other than "Student Info" is showing.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •