Animated tabbed content with jQuery

74

A lot of websites on the Internet have tabbed content now a days. The problem I find with most of them is that most of the time they can be quite dull. For my new looks I created a container which has the ability to switch content through tabs, but with an animation. This tutorial will show you how to create your own tabbed content step by step.

tabbedpost

Download
Tabbed Content zip-archive
Example
View the Tabbed Content in action!

In this tutorial I am going to show you some web development techniques that can be used in your next projects. This will cover some CSS, HTML and jQuery tricks.

The sources of this tutorial are downloadable here: http://www.gayadesign.com/scripts/tabbed/tabbedContent.zip

Example of the outcome is found here: http://www.gayadesign.com/scripts/tabbed/

First thing we need is some basic HTML layout to fit what we are going to make.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Tabbed content</title>
		<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />

		<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js'></script>

		<link href='css/tabbedContent.css' rel='stylesheet' type='text/css' />
		<script src="js/tabbedContent.js" type="text/javascript"></script>
	</head>

	<body>

	</body>
</html>

I already included jQuery and other files in the head section. We’ll be making these files step by step in the next few minutes.

Next thing we need to do is create the structure of the document. What we need is a container with the tabs and a container which the content will that switch if we change tabs.
As a nice feature to enhance this all we’ll use a tab background that will move to the choice the user makes. While this tab moves, the content will also change to the selected tab.

For this we need a container containing the tabs and the background, a container containing the content and container that prevents the content to be shown prematurely.

Take a look at the following code:

<div class='tabbed_content'>
	<div class='tabs'>
		<div class='moving_bg'>
			&nbsp;
		</div>
		<span class='tab_item'>
			Tab one
		</span>
		<span class='tab_item'>
			Tab two
		</span>
		<span class='tab_item'>
			Tab three
		</span>
		<span class='tab_item'>
			Tab four
		</span>
	</div>

	<div class='slide_content'>
		<div class='tabslider'>

			<!-- content goes here -->

		</div>
	</div>
</div>

It’s time to do a little styling to the tabs section and the overall container. Note that in the next part I am going to use an image which is included in the source archive. This will enhance the looks of the tabbed container.

This following CSS will make the tabs appear as tabs:

.tabbed_content {
	background-color: #000000;
	width: 620px;
}

.tabs {
	height: 62px;
	position: relative;
}

.tabs .moving_bg {
	background-color:#7F822A;
	background-image:url(../images/arrow_down_green.gif);
	background-position: bottom left;
	background-repeat: no-repeat;
	left: 0;
	padding: 15px;
	padding-bottom: 29px;
	position: absolute;
	width: 125px;
	z-index: 190;
}

.tabs .tab_item {
	display: block;
	float: left;
	padding: 15px;
	width: 125px;
	color: #ffffff;
	text-align: center;
	z-index: 200;
	position: relative;
	cursor: pointer;
}

Note that the tab container has a position of relative to make the moving background position to it. Now we can easily move the moving background using the left property.

In the next part we are going to add the first few lines of Javascript to the tabbedContent.js file to create a mouseover effect on the tab items.

Use this code as a skeleton:

var TabbedContent = {
	init: function() {	

	},

	slideContent: function(obj) {

	}
}

$(document).ready(function() {
	TabbedContent.init();
});

This code is will execute the init function once the page is loaded.

Now we need to make is so that the moving background moves to the tab item which is hovered on. Implement the following code:

$(".tab_item").mouseover(function() {

	var background = $(this).parent().find(".moving_bg");

	$(background).stop().animate({
		left: $(this).position()['left']
	}, {
		duration: 300
	});

	TabbedContent.slideContent($(this));

});

This will make all the items with the class tab_item behave as if it was a tab. It also moves the moving background to the right position, matching the left property with that of the tab.

TabbedContent.slideContent($(this)); will make the content switch in a few steps. We’ll get to that.

Next up is creating the content and it’s CSS. The content goes in the place we put .

<ul>
	<li>
		This matches the first tab
	</li>
</ul>
<ul>
	<li>
		This matches the second tab
	</li>
</ul>
<ul>
	<li>
		This matches the third tab
	</li>
</ul>
<ul>
	<li>
		This matches the fourth tab
	</li>
</ul>

And the matching CSS:

.tabbed_content .slide_content {
	overflow: hidden;
	background-color: #000000;
	padding: 20px 0 20px 20px;
	position: relative;
	width: 600px;
}

.tabslider {
	width: 5000px;
}

.tabslider ul {
	float: left;
	width: 560px;
	margin: 0px;
	padding: 0px;
	margin-right: 40px;
}

.tabslider ul a {
	color: #ffffff;
	text-decoration: none;
}

.tabslider ul a:hover {
	color: #aaaaaa;
}

.tabslider ul li {
	padding-bottom: 7px;
}

Note that the content has two containers. One is for the overall wrapping and the other for making the content move as we hover over the items.

The top container has an overflow of hidden, which makes any content that goes out of it’s limits hidden. The container below that will hold the content that will be hidden.

Now we are going to make the content scroll to the right position as we did with the tabs.

It’s time to fill up the slideContent function:

var margin = $(obj).parent().parent().find(".slide_content").width();
margin = margin * ($(obj).prevAll().size() - 1);
margin = margin * -1;

$(obj).parent().parent().find(".tabslider").stop().animate({
	marginLeft: margin + "px"
}, {
	duration: 300
});

The margin needed for the slider to move is calculated in this function. It has been made so that you can change the widths of several containers in the result, it will scroll to the right position automatically.

Take another looks at the source for references.

Good luck with making your own tabbed content in your future projects.

Articles like this one

If you liked this article you can add this post to:


 

50 Comments

  1. evan said: April 26, 2009 at 8:30 pm | Permalink

    Awesome man! I would make the area which determines the slider to move to be the <a> rather than the <div>. If content is too far over to the left then mouse could hit the next sections causing the content to change unwarranted.

    Evan


  2. Gaya said: April 26, 2009 at 8:49 pm | Permalink

    Thanks for the comment Evan!
    You have a point there. It would increase the usability of the whole concept. Thanks for sharing your thoughts.


  3. Mike said: April 26, 2009 at 9:38 pm | Permalink

    Very cool! Though, what happens if the user has JavaScript turned off?


  4. Gaya said: April 26, 2009 at 9:44 pm | Permalink

    @Mike: I have no idea… I guess they wont switch :P


  5. Tyler Jefford said: April 26, 2009 at 11:20 pm | Permalink

    Hey! Very cool effect you have made. I really like content sliders.

    Also going along with @mike If the JavaScript has been disabled then none of the tabs work.

    Have a great day!

    -tjefford


  6. Gaya said: April 27, 2009 at 12:12 am | Permalink

    Thanks for the comment Tyler. Maybe the tabs should be clickable if Javascript is disabled.


  7. neki chan said: April 27, 2009 at 11:03 am | Permalink

    coolllll…
    wanna try it
    thanks :D


  8. Farrhad A said: April 27, 2009 at 8:12 pm | Permalink

    Another uber kewl tutorial on GD :)


  9. Peter Witham said: April 27, 2009 at 9:14 pm | Permalink

    Great example and well executed, thanks for sharing.


  10. Gaya said: April 28, 2009 at 8:57 am | Permalink

    @Neki, Farrhad and Peter:

    Thanks for the comments and good luck with trying it out :D


  11. Drew Douglass said: April 28, 2009 at 9:11 am | Permalink

    The effect is super slick and pretty original, different from most tabs I have seen. Keep up this kind of material Gaya, I really enjoy it!

    As for the flaw with JavaScript being disabled, you could try setting the overflow to hidden with JS and set the default CSS to scroll so the user could scroll through the content. Haven’t tested this, just a quick ide from a glance at the demo and code.

    Great work!


  12. Gaya said: April 28, 2009 at 9:16 am | Permalink

    Hi Drew!

    Thanks for checking out the article and commenting! Much appreciated :D

    As for the fix to the problem; it is a solution worth trying. Maybe even make the tabs scrollable with them. I sometimes wonder how many people don’t use Javascript these days.


  13. Hiro said: May 4, 2009 at 5:28 pm | Permalink

    Sweet tutorial :) Appreciate all the details.


  14. Gaya said: May 4, 2009 at 6:24 pm | Permalink

    No problem Hiro! Stick around for more ;)


  15. Ahmet said: May 7, 2009 at 11:37 am | Permalink

    Thanks for this work. This is great! Good work!


  16. Gaya said: May 8, 2009 at 11:01 am | Permalink

    Thank Ahmet! Have fun with it!


  17. Liviu said: May 12, 2009 at 10:06 pm | Permalink

    Thanks for sharing this script, the idea is great. How about converting it into a jQuery plugin?


  18. Gaya said: May 13, 2009 at 8:35 am | Permalink

    I might make a plugin in the near future :) stay tuned!


  19. Raphael said: May 31, 2009 at 5:41 pm | Permalink

    How do i make it animate when the click of the mouse is done instead of the mouseover function?


  20. Raphael said: May 31, 2009 at 6:13 pm | Permalink

    I´ve just solved this problem I have aked for help just by changing mouseover in the js code to mousedown and it worked perfectly


  21. Gaya said: May 31, 2009 at 8:22 pm | Permalink

    @raphael:
    Good! jQuery is really that easy. Glad you liked it :)


  22. dtr said: June 4, 2009 at 9:17 am | Permalink

    Great stuff mate, nice to see someone this creative. Everything work like a charm, but in firefox there’s a problem when you insert the . Keep the great job.


  23. dtr said: June 4, 2009 at 9:23 am | Permalink

    sorry the image tag


  24. Gaya said: June 5, 2009 at 5:09 pm | Permalink

    Hi dtr,

    Are you inserting the image tag inside a ul? that way it should work.
    Good luck :)


  25. dlv said: June 16, 2009 at 4:00 pm | Permalink

    thanks Gaya, it’s a great menu, it’s what i was looking for… i’m using it in my website and looks great

    thanks for share it!!

    PD: a have little question, when you use the menu and click on a tab link, when page reload, the slide button “.moving_bg” is allways in the first position not showing the new open page.
    There is a way to remember the active page, so “.moving_bg” position match with the open page/category?

    thanks in advance!

    adeux, from argentina


  26. W.Dohmen said: June 19, 2009 at 5:44 pm | Permalink

    Looks really nice, just the kind of thing I was looking for thnx for the tut (y)


  27. Vector Man said: July 31, 2009 at 6:02 am | Permalink

    Hi. I think its not comfortable when content links located far away from his title.


  28. Ravikumar V. said: September 16, 2009 at 8:23 am | Permalink

    really nice creative website design.. :)


  29. Siddharth said: September 24, 2009 at 7:34 am | Permalink

    Thanks for your gr8 work. it’s really gr8;

    i have one question: can we use iframe under any tab? i tried but its not showing correctly, please check and update us with ur gr8 work :-) thanks!

    SiD


  30. Gaya said: September 24, 2009 at 4:49 pm | Permalink

    Have you tried putting the iframe inside a list?
    The script was made to move around ul tags. So try to do:
    ul > li > iframe
    good luck!


  31. moabi said: October 19, 2009 at 9:21 am | Permalink

    a very nice slider, simple, efficient, and no click !!!
    check your slider here
    much thanks from him

    http://www.yifan-cirque.com/bio(graphie).php


  32. khits said: October 29, 2009 at 3:41 am | Permalink

    Hi, i’m trying this slider in my website but it’s not working… I followed every step of the instructions but it seems i’m missing something…

    check: http://www.wordofhope.ph/tabbed


  33. khits said: October 29, 2009 at 3:43 am | Permalink

    Hi, i’m trying to put this slider in my website but it’s not working… I followed every step of the instructions but it seems i’m missing something…Will you please tell me what did I missed.

    check: http://www.wordofhope.ph/tabbed.html


  34. Gaya said: October 29, 2009 at 9:12 am | Permalink

    @moabi: Looks cool dude! Glad you liked it :)

    @khits: I see you are missing the jQuery file + the AnimatedTabs file gives a 404. Fix these first.


  35. oh!curiouser said: October 30, 2009 at 12:22 am | Permalink

    Thanks! I like it very, very much!!!


  36. ukwebguy said: November 26, 2009 at 2:39 pm | Permalink

    This is good stuff! Thank you for sharing. I have just one small problem – it works great with 4 tabs but when I try and extend it to 5 or 6 tabs the content stays withing the original 4 tab width no matter what I do to the content width. Would it be possible to have a few instructions on extending this to more than 4 tabs for those of us who are “CSS challenged”?


  37. Chuck Snyder said: December 1, 2009 at 12:46 am | Permalink

    Wonderful little tool. Wow’d the boss.

    I was having a problem displaying 12 tabs. After adding the 5th tab (I was testing as i added each one), they started to double up.

    It took a few minutes to realize what was happening, the .tabslider in the css file was set to 5000px. Adding up the width of the tabs comes up to quite a bit more than 5000px, so upped it to 10000px and now it works just fine.

    Thanks for the good work.


  38. Chuck Snyder said: December 1, 2009 at 6:38 pm | Permalink

    One problem I’m having, I have a couple of GridViews in seperate tabs, and one of the grids is larger than the other.

    How would I make one tab display wider than the others??


  39. Dre said: December 17, 2009 at 11:50 pm | Permalink

    I am interested in using this with the tabs lined up to the left of the content slider above each other. More like buttons than tabs.

    Any ideas?


  40. Nasiba Ansari said: February 17, 2010 at 4:39 pm | Permalink

    Really nice script. I am using in my site.


  41. Kevin said: March 18, 2010 at 2:50 am | Permalink

    hi Gaya, no matter how much I try to put your animated tab into my blog, i can’t do it. Could you please look at my theme for me, and tell me what I did wrong. I am willing to donate to you if you can make your animated script work into my theme.


  42. sajan kota said: March 25, 2010 at 2:46 pm | Permalink

    Looks kool , execellent use of jquery.


  43. diptekosede said: April 2, 2010 at 5:26 pm | Permalink

    Great example and well executed, thanks for sharing.


  44. Michael said: April 19, 2010 at 11:08 am | Permalink

    Hi, How can you make the content to slide verticaly instead of horizontaly? Thanks


  45. ashish said: April 27, 2010 at 2:43 pm | Permalink

    gr8 work….the sliding effect is running but the content is not working when i change the tab. :(


  46. Cingene said: May 10, 2010 at 3:55 am | Permalink

    I found a Bug.
    When i include the newest jQuery 1.4.2 from Google Apis, then in Opera 10.53 your Script are Problems.
    @gaya: Please test it self with jQuery 1.4.2


  47. Criss said: May 18, 2010 at 9:27 pm | Permalink

    comment 37: setting 10000px doesn’t work for me
    comment 45: I got the same problem, content doesn’t appear at all

    Any ideas how to solve the above?


  48. piterPL said: May 19, 2010 at 2:37 pm | Permalink

    It doesn’t work in IE8 ;/


  49. Criss said: May 27, 2010 at 7:10 pm | Permalink

    I would like to have a link inside a tab to go to another tab, keeping the sliding effect. What code should I add ?


  50. Website Design said: June 12, 2010 at 6:53 am | Permalink

    Really very excellent just imagine many many thanks for posting this.


Leave a Comment

Your email is never shared. Get your own avatar with gravatar! Required fields are marked *

*

*



24 Trackbacks

  1. By Visual-Blast Media on May 4, 2009 at 8:19 pm
  2. By CoryMathews.com » 12 Slick jQuery Plugins on August 17, 2009 at 1:40 pm
  3. By 25+ jQuery Tutorials Roundup | ExtraTuts on August 17, 2009 at 8:33 pm
  4. By Dailyinvention.com on October 10, 2009 at 3:31 am
  5. By 50 Cool CSS Menus, Free Source Codes + Tutorials on October 30, 2009 at 7:44 am
  6. By 15 amazing jquery tabs tutorials | ExtraTuts on December 2, 2009 at 7:06 pm
  7. By Nice Jquery Tabs « anil's Blog on January 29, 2010 at 5:14 pm