Tabs Version 2
November 5th, 2006
- Category:
- JavaScript,
- Web Development,
- jQuery
Since the first release of my jQuery Tabs plugin I constantly worked on it – which is most notable if you take a look at the demo page – but until now I have never officially released a new version. Since I made a little API change recently and because I think the plugin is pretty mature now, I hereby announce version 2 of the jQuery Tabs plugin. I’m going to describe all changes and additions since version 1.
Licensing
First of all I changed the licensing for the plugin. The plugin is available under the the same double licensing scheme as jQuery itself is (MIT/GPL). Thus that should be no problem at all, still I think it’s worth mentioning.
Keep track
The (guaranteed) latest version of the plugin is also available from jQuery’s plugin repository. Check it out from svn://jquery.com/plugins with your Subversion client of choice.
Packer
Tabs is packable with Dean Edward’s JavaScript Compressor. I fixed errors that occured in the packed script (e.g. a missing semicolon) and replaced Conditional Compilation with checks for jQuery.browser.msie. In fact, the script in the downloadable ZIP file is already packed. The download contains an uncompressed file as well for those of you who like to have a look into the source code.
Documentation
I added a complete inline documentation, which can be found in the uncompressed source file.
Bookmarkability and the back button
Tabs are truly bookmarkable. Clicking on a tab changes the hash in the URL. If you use Tabs together with my newly created History/Remote plugin the back (and forward) button is fixed as well. If you use Tabs together with my newly created History/Remote plugin, tabs are truly bookmarkable, e.g. clicking on a tab changes the hash in the URL of the browser, as well as support for the back (and forward) button is enabled. Tabs will auto-detect its presence. Supported browsers are Firefox, Opera, Safari and Internet Explorer. History/Remote is contained in the download.
API change
To pass in the tab to be initially activated you no longer specify it with an “on” option. Instead you pass the number as first argument to the tabs() function. That’s the way it was in the very beginning and I found passing in the initial tab as {on: 2} a little too verbose:
$('#container').tabs(2); // start with second tab
$('#container').tabs(2, { /* options */ });
If a hash in the URL of the page refers to one fragment (tab container) of a tab interface, this parameter will be ignored and instead the tab belonging to that fragment will be activated. If it is omitted it defaults to 1. But because of the bookmarkability of a tab such usage is limited and more or less deprecated anyway. You can always control the active tab on page load by having the appropriate hash in the URL.
Optional settings
All of the following options are key/value pairs in an object literal that is passed to tabs() as an argument . For example:
$('#container').tabs({ fxFade: true, fxAutoHeight: true});
It doesn’t matter if you omit the initial tab parameter or not:
$('#container').tabs(2, { fxFade: true, fxAutoHeight: true});
Effects
fxFade: true
Boolean flag indicating whether fade in/out animations are used for tab switching. Can be combined with fxSlide. Will overrule fxShow/fxHide. Default value: false.
fxSlide: true
Boolean flag indicating whether slide down/up animations are used for tab switching. Can be combined with fxFade. Will overrule fxShow/fxHide. Default value: false.
fxSpeed: 'fast'
A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds (e.g. 1000) to run an animation. Default value: "normal".
fxShow: {height: 'show', opacity: 'show'}
An object literal of the form jQuery’s animate function expects for making your own, custom animation to reveal a tab upon tab switch. Unlike fxFade or fxSlide this animation is independent from an optional hide animation. Default value: null. See animate.
fxHide: {height: 'hide', opacity: 'hide'}
An object literal of the form jQuery’s animate function expects for making your own, custom animation to hide a tab upon tab switch. Unlike fxFade or fxSlide this animation is independent from an optional show animation. Default value: null. See animate.
fxShowSpeed: 'fast'
A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds (e.g. 1000) to run the animation specified in fxShow. Default value: fxSpeed.
fxHideSpeed: 'fast'
A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds (e.g. 1000) to run the animation specified in fxHide. Default value: fxSpeed.
fxAutoHeight: true
Boolean flag that if set to true causes all tab heights to be constant (being the height of the tallest tab). Default value: false.
There’s one more thing about these effects I would like to point out: Even if you use animations, all the tab contents remain fully printable. This is not granted due to how animations work and you will hardly find that taken into account out there.
Other options
callback: function(tabToShow, tabToHide) {
// do something
}
A function to be executed upon tab switch. If animations are used this function is invoked after the animations are completed. The function gets passed two arguments: the first one is the DOM element containing the content of the clicked tab (e.g. the div), the second argument is the one of the tab that gets hidden. Alternatively the former element can also be refered to with the this keyword in the body of the callback function. Default value: null.
selectedClass: 'a-css-class'
The CSS class attached to the li element representing the currently selected tab. Default value: "tabs-selected".
hideClass: 'a-css-class'
The CSS class used for hiding inactive tabs. A class is used instead of display: none in the style attribute to maintain control over visibility in other media types than screen, most notably print. Default value: "tabs-hide".
tabStruct: 'div>div'
A CSS selector or basic XPath expression reflecting a nested HTML structure that is different from the default single div structure (one div with an id inside the overall container holds one tab’s content). If for instance an additional div is required to wrap up the several tab containers such a structure is expressed by "div>div":
<div id="container">
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#section-2">Section 2</a></li>
<li><a href="#section-3">Section 3</a></li>
</ul>
<div>
<div id="section-1">
...
</div>
<div id="section-2">
...
</div>
<div id="section-3">
...
</div>
</div>
</div>
Recap: The default HTML structure the plugin relies on looks like this:
<div id="container">
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#section-2">Section 2</a></li>
<li><a href="#section-3">Section 3</a></li>
</ul>
<div id="section-1">
...
</div>
<div id="section-2">
...
</div>
<div id="section-3">
...
</div>
</div>
Triggering a tab
I have also added a function to activate a tab programmatically (”from outside”), as if the tab itself were clicked:
$('#container').triggerTab(2);
That would activate the second tab for an interface that was created in #container. Such a function comes in handy if you need rotating tabs for instance.
189 Comments
Comments feed
Comment by Matt Bourne, November 6th, 2006 at 3:52 pm
Author Comment
Comment by Klaus, November 6th, 2006 at 8:57 pm
Comment by Lv, November 7th, 2006 at 2:18 am
Comment by Merric, November 7th, 2006 at 3:26 pm
Comment by Merric, November 7th, 2006 at 6:06 pm
Author Comment
Comment by Klaus, November 11th, 2006 at 3:47 pm
Comment by Geert Baven, November 13th, 2006 at 10:53 am
Comment by Armando, November 13th, 2006 at 8:01 pm
Author Comment
Comment by Klaus, November 13th, 2006 at 9:33 pm
Comment by Dmitry, November 15th, 2006 at 8:59 pm
Author Comment
Comment by Klaus, November 16th, 2006 at 8:56 am
Pingback by Klaus Hartl - Stilbüro : Accessible, unobtrusive JavaScript tabs with jQuery, November 16th, 2006 at 5:56 pm
Comment by Daniel, November 16th, 2006 at 6:52 pm
Author Comment
Comment by Klaus, November 17th, 2006 at 3:25 am
Comment by Dmitry, November 18th, 2006 at 11:05 pm
Comment by Dmitry, November 18th, 2006 at 11:08 pm
Comment by ek, November 19th, 2006 at 6:37 pm
Comment by Darren Ferguson, November 22nd, 2006 at 12:39 pm
Author Comment
Comment by Klaus, November 22nd, 2006 at 1:33 pm
Comment by Raziel, November 23rd, 2006 at 2:32 am
Comment by Guillermo, November 23rd, 2006 at 9:09 pm
Author Comment
Comment by Klaus, November 24th, 2006 at 12:29 am
Comment by Nici, November 25th, 2006 at 9:15 am
Comment by Nici, November 25th, 2006 at 9:43 pm
Comment by Raziel, November 27th, 2006 at 5:51 pm
Pingback by This Cow Is Purple, November 27th, 2006 at 6:03 pm
Comment by Raziel, November 28th, 2006 at 12:11 am
Comment by Dmitry, November 28th, 2006 at 9:17 am
Comment by taote, November 28th, 2006 at 12:27 pm
Comment by Anej, November 28th, 2006 at 4:29 pm
Comment by Marc McHale, November 28th, 2006 at 5:30 pm
Comment by Guillermo, November 28th, 2006 at 7:33 pm
Author Comment
Comment by Klaus, November 28th, 2006 at 10:23 pm
Comment by Guillermo, November 29th, 2006 at 4:40 pm
Author Comment
Comment by Klaus, November 30th, 2006 at 12:35 am
Author Comment
Comment by Klaus, November 30th, 2006 at 2:06 pm
Comment by Guillermo, November 30th, 2006 at 6:47 pm
Comment by Knalpiep, December 3rd, 2006 at 10:26 pm
Author Comment
Comment by Klaus, December 3rd, 2006 at 10:36 pm
Author Comment
Comment by Klaus, December 3rd, 2006 at 10:39 pm
Comment by Knalpiep, December 3rd, 2006 at 11:47 pm
Comment by stylo~, December 4th, 2006 at 7:48 pm
Author Comment
Comment by Klaus, December 5th, 2006 at 8:47 pm
Comment by stylo~, December 6th, 2006 at 8:09 am
Author Comment
Comment by Klaus, December 6th, 2006 at 11:36 am
Author Comment
Comment by Klaus, December 6th, 2006 at 11:44 am
Comment by stylo~, December 7th, 2006 at 3:31 am
Author Comment
Comment by Klaus, December 7th, 2006 at 2:20 pm
Comment by stylo~, December 8th, 2006 at 8:27 am
Author Comment
Comment by Klaus, December 8th, 2006 at 9:47 am
Author Comment
Comment by Klaus, December 8th, 2006 at 11:08 am
Comment by stylo~, December 10th, 2006 at 9:00 am
Comment by Eric, December 11th, 2006 at 10:25 am
Author Comment
Comment by Klaus, December 11th, 2006 at 11:29 am
Author Comment
Comment by Klaus, December 11th, 2006 at 12:33 pm
Comment by stylo~, December 11th, 2006 at 4:33 pm
Comment by stylo~, December 11th, 2006 at 4:43 pm
Comment by Eric, December 12th, 2006 at 9:27 am
Comment by Abba Bryant, December 20th, 2006 at 6:08 am
Comment by Steven G, December 24th, 2006 at 3:28 pm
Comment by Matei "Ambient.Impact" Stanca, December 27th, 2006 at 3:53 pm
Comment by Shane Graber, December 30th, 2006 at 2:45 pm
Comment by Shane Graber, December 30th, 2006 at 2:47 pm
Comment by Adrian Voicu, January 7th, 2007 at 12:16 pm
Comment by Raphael Martins, January 8th, 2007 at 5:51 pm
Comment by Catalin Luntraru, January 9th, 2007 at 1:34 am
Author Comment
Comment by Klaus, January 9th, 2007 at 10:01 pm
Comment by C Foss, January 12th, 2007 at 6:36 pm
Comment by C Foss, January 12th, 2007 at 6:43 pm
Comment by jason winn, January 18th, 2007 at 6:44 pm
Author Comment
Comment by Klaus, January 24th, 2007 at 6:23 pm
Comment by Achaiah, January 25th, 2007 at 4:01 pm
Author Comment
Comment by Klaus, January 25th, 2007 at 4:11 pm
Comment by Achaiah, January 25th, 2007 at 8:40 pm
Comment by khelll, January 27th, 2007 at 5:58 pm
Author Comment
Comment by Klaus, January 27th, 2007 at 6:58 pm
Comment by manafta, January 28th, 2007 at 2:54 pm
Comment by C Foss, January 29th, 2007 at 4:57 pm
Comment by Jim, January 30th, 2007 at 3:59 pm
Author Comment
Comment by Klaus, January 30th, 2007 at 6:39 pm
Comment by peat, January 31st, 2007 at 7:07 pm
Comment by Dada, February 1st, 2007 at 1:59 am
Author Comment
Comment by Klaus, February 1st, 2007 at 11:49 am
Comment by Stylemo, February 2nd, 2007 at 11:01 am
Author Comment
Comment by Klaus, February 2nd, 2007 at 2:37 pm
Comment by Stylemo, February 5th, 2007 at 3:47 am
Comment by Manuel, February 5th, 2007 at 2:25 pm
Author Comment
Comment by Klaus, February 5th, 2007 at 3:10 pm
Comment by Manuel, February 6th, 2007 at 7:53 am
Comment by Manuel, February 6th, 2007 at 8:22 am
Comment by vargaso, February 12th, 2007 at 11:16 pm
Author Comment
Comment by Klaus, February 13th, 2007 at 12:06 pm
Author Comment
Comment by Klaus, February 13th, 2007 at 12:18 pm
Author Comment
Comment by Klaus, February 13th, 2007 at 12:36 pm
Comment by Mike, February 13th, 2007 at 9:59 pm
Author Comment
Comment by Klaus, February 13th, 2007 at 10:08 pm
Comment by Mike, February 13th, 2007 at 10:14 pm
Comment by Tony, February 14th, 2007 at 9:43 am
Author Comment
Comment by Klaus, February 14th, 2007 at 10:23 am
Comment by Tony, February 14th, 2007 at 3:41 pm
Comment by Mark, February 19th, 2007 at 9:23 pm
Author Comment
Comment by Klaus, February 19th, 2007 at 10:07 pm
Comment by Mark, February 19th, 2007 at 10:16 pm
Pingback by Tabs 2.0, un plugin para jQuery | aNieto2K, February 20th, 2007 at 10:58 am
Pingback by Webmaster Libre | links for 2007-02-20, February 21st, 2007 at 2:28 am
Comment by Mark, February 21st, 2007 at 3:08 pm
Comment by Jo, February 21st, 2007 at 11:42 pm
Author Comment
Comment by Klaus, February 22nd, 2007 at 11:29 am
Comment by Hagen, February 23rd, 2007 at 5:00 am
Author Comment
Comment by Klaus, February 23rd, 2007 at 10:57 am
Comment by Zachary, February 23rd, 2007 at 7:51 pm
Author Comment
Comment by Klaus, February 23rd, 2007 at 8:09 pm
Comment by Hagen, February 23rd, 2007 at 9:56 pm
Author Comment
Comment by Klaus, February 24th, 2007 at 3:07 pm
Comment by sebastián, February 24th, 2007 at 11:51 pm
Comment by Tony, February 26th, 2007 at 7:38 pm
Comment by Igor, February 26th, 2007 at 8:13 pm
Author Comment
Comment by Klaus, February 26th, 2007 at 10:27 pm
Author Comment
Comment by Klaus, February 26th, 2007 at 10:37 pm
Comment by Gunner, March 7th, 2007 at 7:49 pm
Comment by Gunner, March 7th, 2007 at 8:39 pm
Author Comment
Comment by Klaus, March 7th, 2007 at 8:47 pm
Comment by Gunner, March 7th, 2007 at 10:05 pm
Comment by Maniquí, March 7th, 2007 at 11:03 pm
Author Comment
Comment by Klaus, March 8th, 2007 at 10:38 am
Author Comment
Comment by Klaus, March 8th, 2007 at 10:41 am
Pingback by My Retirement Project » Re-Design Update 2, March 10th, 2007 at 8:33 pm
Comment by Mark Overmeer, March 10th, 2007 at 9:42 pm
Comment by Daniel Kinney, March 11th, 2007 at 11:57 pm
Comment by Cutter, March 12th, 2007 at 2:47 am
Author Comment
Comment by Klaus, March 12th, 2007 at 10:39 am
Comment by Cutter, March 12th, 2007 at 7:47 pm
Author Comment
Comment by Klaus, March 12th, 2007 at 9:20 pm
Comment by Cutter, March 13th, 2007 at 2:37 am
Comment by Talat, March 14th, 2007 at 8:05 pm
Author Comment
Comment by Klaus, March 14th, 2007 at 9:44 pm
Comment by Cutter, March 15th, 2007 at 4:01 am
Comment by Talat, March 15th, 2007 at 7:55 am
Author Comment
Comment by Klaus, March 15th, 2007 at 9:25 am
Comment by dariusz, March 15th, 2007 at 11:42 am
Comment by Hatem Belhasine, March 16th, 2007 at 4:35 pm
Author Comment
Comment by Klaus, March 16th, 2007 at 6:05 pm
Comment by Hatem Belhasine, March 16th, 2007 at 6:22 pm
Comment by Matthew Lieder, March 17th, 2007 at 4:31 pm
Author Comment
Comment by Klaus, March 17th, 2007 at 5:06 pm
Comment by Matthew Lieder, March 18th, 2007 at 1:10 am
Comment by Weavil, March 27th, 2007 at 6:44 pm
Comment by Travis Dahl, March 29th, 2007 at 8:23 pm
Comment by Rikard, April 3rd, 2007 at 4:03 pm
Comment by Matthew Lieder, April 3rd, 2007 at 6:22 pm
Author Comment
Comment by Klaus, April 3rd, 2007 at 6:30 pm
Comment by Thomas D, April 4th, 2007 at 4:23 pm
Author Comment
Comment by Klaus, April 4th, 2007 at 4:41 pm
Comment by Fabrizio, April 6th, 2007 at 11:31 am
Comment by Pat, April 16th, 2007 at 10:15 pm
Comment by Matt Richards, April 17th, 2007 at 12:52 pm
Comment by Tom, April 17th, 2007 at 8:27 pm
Comment by eax, April 20th, 2007 at 4:16 pm
Author Comment
Comment by Klaus, April 21st, 2007 at 9:40 am
Comment by Matt Richards, April 23rd, 2007 at 12:42 pm
Comment by line24, April 23rd, 2007 at 1:59 pm
Comment by alex, April 25th, 2007 at 9:26 am
Comment by Yuriy, May 1st, 2007 at 6:10 pm
Comment by Craig, May 10th, 2007 at 1:57 pm
Comment by Chris, June 19th, 2007 at 10:46 am
Comment by Mauro, June 20th, 2007 at 9:43 pm
Pingback by Lost in Anywhere » Tabs Version 2, June 21st, 2007 at 5:45 am
Comment by gene, July 3rd, 2007 at 5:30 am
Comment by Nathan Waters, July 6th, 2007 at 6:54 am
Comment by Marjan, July 31st, 2007 at 12:55 pm
Comment by Lisa Yau, August 8th, 2007 at 6:32 pm
Comment by Stuart, August 9th, 2007 at 4:13 pm
Author Comment
Comment by Klaus, August 9th, 2007 at 6:17 pm
Comment by Fabio, August 18th, 2007 at 4:32 pm
Comment by Fabio, August 18th, 2007 at 4:34 pm
Comment by Scott Rod, August 22nd, 2007 at 3:46 pm
Comment by Tariq, August 28th, 2007 at 11:34 pm
Comment by Diego Alto, August 30th, 2007 at 6:44 pm
Comment by dpaessler, August 31st, 2007 at 12:02 pm
Pingback by Os 40 Melhores Plugins da jQuery para facilitar a vida do Desenvolvedor Web | PedroMenezes.com, September 11th, 2007 at 4:56 am
Pingback by 10 Über cool jQuery Plugins | Web Development With Invisible Window {iW}, September 25th, 2007 at 6:47 pm
Comment by Seth, September 26th, 2007 at 7:59 pm
Comment by Seth, September 26th, 2007 at 8:10 pm
Comment by mixey, October 3rd, 2007 at 1:17 pm
Comment by Paymon, October 17th, 2007 at 4:42 pm
Comment by Mesa, October 18th, 2007 at 7:00 am
Author Comment
Comment by Klaus, October 18th, 2007 at 10:42 am
Comment by Mesa, October 22nd, 2007 at 8:46 am
Author Comment
Comment by Klaus, October 23rd, 2007 at 12:25 pm