Tabs plugin revised
June 18th, 2006
- Category:
- JavaScript,
- Web Development,
- jQuery
I have revised the jQuery tabs plugin, so that it conforms to the jQuery plugin construct. To create the tab interface you now have to call the tabs function in a jquerish manner:
$('#container').tabs();
Therefore one can use jQuery’s powerful capabilities to retrieve the element holding the underlying HTML (good!) and of course chain methods (good!):
$('body>div:eq(1)').tabs().fadeIn('slow');
Other Changes
To provide some additional options the tabs function can take an optional argument which has to be an object literal. To activate a certain tab you have to write:
$('#container').tabs({on: 2}); // second tab is on
To further improve accessibility the plugin now uses classes instead of the hide/show functions (these do apply inline styles) for hiding inactive tab content. The content is then still visible in other media types then screen, e.g. print. This was reported on the jQuery mailing list.
Thus one must include the following rule in a style sheet:
@media projection, screen {
.tabs-hide {
display: none;
}
}
In case you wonder: projection is aimed at Opera in full screen mode.
Next Steps
I will add more possible options. These came to my mind so far:
-
adjustHeight: trueThe height of each tab’s content will be adjusted to the largest one,
so that you do not get annoying changes of the viewport height when switching tabs. This can be a bit irritating. -
fade: 'speed'The plugin will use
fadeIn/fadeOutfor switching the tab content with the given speed. -
slide: 'speed'Same as above, but the
slideDown/slideUpfunctions are used for switching. -
Both effects may be combined as well.
Here is the JavaScript source:
// tabs - jQuery plugin for accessible, unobtrusive tabs by Klaus Hartl
// v 1.0
// http://stilbuero.de/tabs/
// Free beer and free speech. Enjoy!
$.fn.tabs = function(options) {
// basic stuff
var ON_CLASS = 'on';
var OFF_CLASS = 'tabs-hide';
// options
var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on - 1 : 0;
return this.each(function() {
$(this).find('>div').not(':eq(' + on + ')').addClass(OFF_CLASS);
$(this).find('>ul>li:eq(' + on + ')').addClass(ON_CLASS);
var container = this;
$(this).find('>ul>li>a').click(function() {
if (!$(this.parentNode).is('.' + ON_CLASS)) {
var re = /([_\\-\\w]+$)/i;
var target = $('#' + re.exec(this.href)[1]);
if (target.size() > 0) {
$(container).find('>div:visible').addClass(OFF_CLASS);
target.removeClass(OFF_CLASS);
$(container).find('>ul>li').removeClass(ON_CLASS);
$(this.parentNode).addClass(ON_CLASS);
} else {
alert('There is no such container.');
}
}
return false;
});
});
};
The sample moved here (download zipped files).
57 Comments
Comments feed
Pingback by Klaus Hartl - Stilbüro : Accessible, unobtrusive JavaScript tabs with jQuery, June 18th, 2006 at 7:05 pm
Comment by cdnnick, June 20th, 2006 at 4:58 am
Author Comment
Comment by Klaus, June 20th, 2006 at 7:59 pm
Comment by Jökull Sólberg Auðunsson, June 21st, 2006 at 12:19 pm
Pingback by Jennifer Grucza » Archive » jQuery makes Javascript fun, June 22nd, 2006 at 2:01 am
Author Comment
Comment by Klaus, June 22nd, 2006 at 2:27 pm
Comment by Jennifer Grucza, June 28th, 2006 at 10:31 pm
Author Comment
Comment by Klaus, June 28th, 2006 at 11:00 pm
Comment by DrydenMaker, July 7th, 2006 at 12:17 am
Comment by Lukas, July 31st, 2006 at 3:38 pm
Author Comment
Comment by Klaus, August 1st, 2006 at 10:58 am
Comment by Harvey Ramer, August 3rd, 2006 at 5:26 am
Author Comment
Comment by Klaus, August 3rd, 2006 at 1:29 pm
Comment by gib, August 6th, 2006 at 11:44 pm
Comment by gib, August 7th, 2006 at 12:45 am
Comment by Nicolas, August 16th, 2006 at 8:46 am
Author Comment
Comment by Klaus, August 17th, 2006 at 9:43 pm
Comment by Original Sin, August 18th, 2006 at 8:30 pm
Pingback by jQuery: più lo uso e più mi piace | .:: Maurizio Pelizzone ::., August 21st, 2006 at 1:01 pm
Comment by DrydenMaker, August 21st, 2006 at 10:37 pm
Comment by Neil, September 1st, 2006 at 3:57 pm
Author Comment
Comment by Klaus, September 1st, 2006 at 5:45 pm
Comment by Kit Churchill, September 12th, 2006 at 5:00 pm
Comment by Eric, September 15th, 2006 at 7:57 am
Author Comment
Comment by Klaus, September 16th, 2006 at 7:09 pm
Comment by Zohar, September 21st, 2006 at 3:21 am
Comment by Adrienne Travis, October 6th, 2006 at 3:17 pm
Comment by marc, October 7th, 2006 at 11:57 pm
Author Comment
Comment by Klaus, October 8th, 2006 at 4:52 pm
Comment by marc, October 8th, 2006 at 6:31 pm
Comment by marc, October 17th, 2006 at 7:36 pm
Author Comment
Comment by Klaus, October 18th, 2006 at 12:05 am
Comment by marc, October 18th, 2006 at 10:14 pm
Pingback by Yorch » Blog Archive » Mejorar interfaces Web, October 20th, 2006 at 9:10 pm
Comment by joon, October 22nd, 2006 at 5:52 pm
Comment by kilian, October 25th, 2006 at 1:17 am
Author Comment
Comment by Klaus, October 25th, 2006 at 11:01 am
Comment by Jeff Rader, October 29th, 2006 at 5:35 am
Author Comment
Comment by Klaus, October 29th, 2006 at 2:49 pm
Comment by Jeff Rader, October 29th, 2006 at 8:48 pm
Comment by Jeff Rader, October 30th, 2006 at 6:47 pm
Author Comment
Comment by Klaus, October 30th, 2006 at 10:36 pm
Comment by Jeff Rader, November 1st, 2006 at 4:53 am
Comment by gsarwohadi, November 1st, 2006 at 1:34 pm
Author Comment
Comment by Klaus, November 1st, 2006 at 10:56 pm
Comment by gsarwohadi, November 2nd, 2006 at 3:01 pm
Comment by gsarwohadi, November 2nd, 2006 at 3:03 pm
Author Comment
Comment by Klaus, November 2nd, 2006 at 8:40 pm
Pingback by Klaus Hartl - Stilbüro : Tabs Version 2, November 6th, 2006 at 11:20 am
Author Comment
Comment by Klaus, December 6th, 2006 at 11:39 am
Comment by Laurent Chardin, January 19th, 2007 at 8:16 pm
Comment by Laurent Chardin, January 21st, 2007 at 1:46 pm
Comment by Rik Logtenberg, February 4th, 2007 at 2:47 am
Comment by phil, April 27th, 2007 at 9:51 am
Comment by phil, April 27th, 2007 at 9:57 am
Comment by Alexandre, August 14th, 2007 at 4:21 am
Comment by alvaro montilla, September 27th, 2007 at 4:45 pm