jQuery Sliding Tab Menu for Sidebar Tutorial

Written by Kevin Liew on 09 Jun 2009
155,159 Views • Tutorials


This tutorial, we will be making a jQuery Sliding Tab Menu for sidebar. Previously, I made a tutorial about jQuery Tabbed Interface / Tabbed Structure Menu, if you have not read it yet, check it out. I'm using it for my sidebar at the moment. The differences between both tab menus are, the sliding direction and this time, we are using a little bit different HTML structure and massive change in jQuery script.

We will be using a famous plugin called jQuery.scrollTo. jQuery scrollTo is an amazing plugin and doing sliding menu has never been so easy! Please continue reading, As usual, this tutorial is separated into 3 sections: HTML, CSS and Javascript.


Structure for jQuery Scroll To Tab Menu

Refer to the image above, it's the DIV structure for this tutorial.

#scroller-header: Inside #scroller-header, this is where we will put the links. In the link HREF attribute, we will put in the id of the sub panels (#panel-1, #panel-2, #panel-3 and #panel-4) inside #panel. jQuery will able to scroll it to the correct position according to the ID specified in the HREF.

#scroller-body: We put a background image about 900px height and set the background position to bottom.

#mask: This DIV plays an important role to hide other panels. Adjusting the height and width of #mask will able to display the panel content. Everytime when a link is clicked, jQuery will get the height of the sub-panel and set the height of #mask accordingly.

#panel: Inside #panel, we have 4 DIVs named #panel-1, #panel-2, #panel3, #panel-4. Those are sub-panels, you can place your content inside here, and feel free to add more sub panels.

<div id="scroller-header">
	<a href="#panel-1" rel="panel" class="selected">Link 1</a>
	<a href="#panel-2" rel="panel">Link 2</a>
	<a href="#panel-3" rel="panel">Link 3</a>
	<a href="#panel-4" rel="panel">Link 4</a>
<div id="scroller-body">
	<div id="mask">
		<div id="panel">
			<div id="panel-1">
			<div id="panel-2">
			<div id="panel-3">
			<div id="panel-4">

2. CSS

Structure for jQuery Scroll To Tab Menu

There are some important css:

#scoller-header, #scroller-body : We can change the background images in these ID. For the #scroller-body, the vertical alignment of the background must be bottom.

#mask : Width is the size of a sub-panel, this width must be specified because we will do some calculation based on it.

#panel div : Float set the left so that the DIV will display in a single row. If you want to scroll it horizontally, you can remove it.

body {
	margin:0 20px;
	font:12px arial;

#scroller-header a {
	padding:0 2px;

#scroller-header a:hover {

a.selected {
	text-decoration:underline !important; 
	color:#4b412f !important;

#scroller-header {
	background:url(images/header.gif) no-repeat;
	padding:35px 0 0 15px;

#scroller-body {
	background:url(images/body.gif) no-repeat bottom center;


#mask {
	margin:0 auto;

#panel {


#panel div {


/* extra optional styling for each tab content */
#panel-1 {

#panel-2 {

#panel-3 {

3. Javascript

With the scrollTo plugin, we don't have to worry about the transition and scrolling effect. This is the advantage of using plugin, everything can be so easy.

Basically, jQuery performs the following steps on page load:

  1. Get the height of the first sub-panel and set it to #mask.
  2. Calculate the width of #panel by multiplying #mask width with total of sub-panel (#panel div)
  3. Set the width of sub-panel according to the #mask width. As a result, #mask width must be the same with sub-panels and must be specified in the CSS.

And, jQuery performs the following steps when a link is clicked:

  1. Get the height of the sub-panel. jQuery grabs the sub-panel id from href attribute of the anchor element.
  2. Remove selected class from all the links, and add the selected class to the user has just clicked.
  3. Do the animation on height, Animate the #mask by setting the height of the sub-panel.
  4. After that, run the scrollTo plugin, scroll it to the sub-panel.
  5. Remember to disable the link default behavior by adding 'Return false' in the end.
$(document).ready(function() {	

	//Get the height of the first item
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		//Set class for the selected item
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		//Discard the link default behavior
		return false;


That's it. Make sure you check out the demo and download the source code to play with it. If you have created your own, feel free to drop your link in the comment section to show off! : )

Last but not least, I need your support :) If you like this article, please help me to promote it by adding this post into your bookmark. Or you can subscribe to my RSS for more jQuery tutorial posts! Thanks!

Demo Download
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

forgetme 15 years ago
I can feel how much you worked hard and find out the solution for this tutorial. Thanks a lot for this useful sharing.
mgpyone 15 years ago
That's Gr8.. Cheer !!
ainstushar 15 years ago
You might want to use .stop() to stop the animation if a user clicks on multiple things too fast. With the code above, it will cycle through all, and the user will have to wait. If you use .stop() then it will stop the previous animation in middle and animate to the newest click.

Hope that helps.
Edward 15 years ago
Many thanks for your useful tutorial, a link of this article has been added to www.webmasterclip.com in order to share it with more people.
Jason Straughan 15 years ago
Great tutorial. Added this to http://tutlist.com
P@W 15 years ago
very thank.
seeal 15 years ago
Great tutorial
Angelshark 15 years ago
Love this site.
Tons of great stuff.
Keep up the good work.
Joan Izquierdo 15 years ago
It is possible that this all hidden by default ?
Ajay Kaushik 15 years ago
Thanks a lot for this example. I was looking for same thing for sometime now.
Its a very good example of what i was trying to do. Can you please tell me how to use stop as told by ainstushar. I am trying to do same thing with hover. It works but problem occurs when one hovers too fast on multiple links, Also sometimes it does scroll when one hovers on the link. I simply changed
$('a[rel=panel]').click(function () {
$('a[rel=panel]').hover(function () {
to get the same functionality for hover.

Can you please tell me what should i do to run it smoothly with hover.
kevin Admin 15 years ago
@Ajay: yes, I think it's something like


correct me if im wrong.

@Joan: what do you want to hide?
Ajay Kaushik 15 years ago
Kevin, Thanks for replying But its not working. Can you tell me where should i use that line of code. I used it in hover function but no gain. Can you please provide a sample that works for hover as well.
Joan Izquierdo 15 years ago
I want the menu to load this hidden page, but if the layers are hidden does not work then.

#scroller-header { display:none; }
#scroller-body { display:none; }

The solution is to move up the layers so it does not show, scroll.css:

#scroller-header { position:absolute; top:-900px; margin-left: 0%; margin-right: 0%; display:block; }
#scroller-body { position:absolute; top:-900px; margin-left: 0%; margin-right: 0%; display:block; }

Later, when loading the scroll.js and move from javascript layers down:

$(document).ready(function() {