SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: Jquery tabs

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    724
    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,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that's where I have it, but it appears to be reacting in the same manor..

    Its really odd, when you access it via a link, the tab is correctly highlighted, but the wrong info displays in the tab. When you click the tap, it shifts to the left, the style of the text changes and the correct info displays..

  15. #15
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This script is taking to much time to make work, I have tried another version: http://os.alfajango.com/easytabs/

    Seems to work, just need to use css to match the look of original version..

  16. #16
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems to be working great.. Next thing is to try and the menus that popup over the images to have the correct menu displaying dependant on which page is open..

    Would that be possible?

    Thanks

  17. #17
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    Quote Originally Posted by _matrix_ View Post
    Seems to be working great..
    Good news

    Quote Originally Posted by _matrix_ View Post
    Next thing is to try and the menus that popup over the images to have the correct menu displaying dependant on which page is open..

    Would that be possible?
    I would guess so.
    Try $(yourSelector).trigger("mouseover")

  18. #18
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm haven't really got a handle on it yet.. The js code for image menu is:

    Code:
    		$(function() {
                    /* position of the <li> that is currently shown */
                    var current = 0;
    				
    				var loaded  = 0;
    				for(var i = 1; i <4; ++i)
    					$('<img />').load(function(){
    						++loaded;
    						if(loaded == 3){
    							$('#bg1,#bg2,#bg3').mouseover(function(e){
    								
    								var $this = $(this);
    								/* if we hover the current one, then don't do anything */
    								if($this.parent().index() == current)
    									return;
    
    								/* item is bg1 or bg2 or bg3, depending where we are hovering */
    								var item = e.target.id;
    
    								/*
    								this is the sub menu overlay. Let's hide the current one
    								if we hover the first <li> or if we come from the last one,
    								then the overlay should move left -> right,
    								otherwise right->left
    								 */
    								if(item == 'bg1' || current == 2)
    									$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(-266px 0)"},300,function(){
    										$(this).find('li').hide();
    									});
    								else
    									$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(266px 0)"},300,function(){
    										$(this).find('li').hide();
    									});
    
    								if(item == 'bg1' || current == 2){
    									/* if we hover the first <li> or if we come from the last one, then the images should move left -> right */
    									$('#menu > li').animate({backgroundPosition:"(-800px 0)"},0).removeClass('bg1 bg2 bg3').addClass(item);
    									move(1,item);
    								}
    								else{
    									/* if we hover the first <li> or if we come from the last one, then the images should move right -> left */
    									$('#menu > li').animate({backgroundPosition:"(800px 0)"},0).removeClass('bg1 bg2 bg3').addClass(item);
    									move(0,item);
    								}
    
    								/*
    								We want that if we go from the first one to the last one (without hovering the middle one),
    								or from the last one to the first one, the middle menu's overlay should also slide, either
    								from left to right or right to left.
    								 */
    								if(current == 2 && item == 'bg1'){
    									$('#menu .sub'+parseInt(current)).stop().animate({backgroundPosition:"(-266px 0)"},300);
    								}
    								if(current == 0 && item == 'bg3'){
    									$('#menu .sub'+parseInt(current+2)).stop().animate({backgroundPosition:"(266px 0)"},300);
    								}
    
    								
    								/* change the current element */
    								current = $this.parent().index();
    								
    								/* let's make the overlay of the current one appear */
    							   
    								$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition:"(0 0)"},300,function(){
    									$(this).find('li').fadeIn();
    								});
    							});
    						}	
    					}).attr('src', 'images/'+i+'.jpg');
    				
    							
                    /*
                    dir:1 - move left->right
                    dir:0 - move right->left
                     */
                    function move(dir,item){
                        if(dir){
                            $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},200);
                            $('#bg2').parent().stop().animate({backgroundPosition:"(-266px 0)"},300);
                            $('#bg3').parent().stop().animate({backgroundPosition:"(-532px 0)"},400,function(){
                                $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                            });
                        }
                        else{
                            $('#bg1').parent().stop().animate({backgroundPosition:"(0 0)"},400,function(){
                                $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
                            });
                            $('#bg2').parent().stop().animate({backgroundPosition:"(-266px 0)"},300);
                            $('#bg3').parent().stop().animate({backgroundPosition:"(-532px 0)"},200);
                        }
                    }
                });

  19. #19
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 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
  •