Create a Custom Content Slider with jQuery

Written by Kevin Liew on 08 Jun 2010
186,959 Views • Tutorials


I'm writing this tutorial in an Airport, heading back to my home country for a short holiday. It was a long day and had a farewell lunch with my fellow colleague. He got a better job offer, resigned and moved on. Well, that's life, had a great time working with him accross different projects, no doubt about it, I learnt heaps of stuff from him. I'm pretty sure I'll miss his daily quotes, jokes and whinging.

Alright, let's get into the tutorial

This time, we are going to make a custom content slider. I implemented this in my recent project and I'm pretty happy about it. My colleague spiced it up a little bit with some nice buttons. The way it works is quite simple, we will have a UL list with 3 buttons and each of the button has a link with a reference to the right panel. How it scroll to the correct panel? It uses jQuery scroll-To plugin. Basically you just have to pass the ID of the panel to the scroll-To plugin, and it will sroll the content to the correct position.

For more information about the plugin, please visit the scrollTo plugin website

Custom Content Slider


HTML is a little bit long this time. Basically, it has two sections:

  • UL List: This is the list of buttons, you can add as many as you want, or style the layout the way you want.
  • Slider: Slider is where we put all the content for the panels, in this case, we have 3 panels with unique ID panel-1, panel-2 and panel-3. We hide the panel with overflow hidden set in the Mask Div.
<div id="hero-slider">
		<li><a href="#" rel="#panel-1" class="active">Item 1</a></li>
		<li><a href="#" rel="#panel-2">Item 2</a></li>
		<li><a href="#" rel="#panel-3">Item 3</a></li>
	<div class="mask">
		<div class="slider-body">
			<div class="panel" id="panel-1">
				<h2>Title 1</h2>
			<div class="panel" id="panel-2">
				<h2>Title 2</h2>
			<div class="panel" id="panel-3">
				<h2>Title 3</h2>
	</div> <!-- .mask -->
	<div class="clear"></div>
</div> <!-- #hero-slider -->

2. CSS

The only tricky part of the CSS would be the mask. You have to make sure it has overflow set to hidden to hide panels. Also, the dimension of the panel and mask must be the same.

#hero-slider {
	border:1px solid #ccc; width:450px;
	margin:0 auto;

#hero-slider .mask { 
	margin:15px 0 0 10px; 

#hero-slider .panel { 

#hero-slider ul {
	padding:15px 15px 0 15px; 
	border-right:1px solid #dedede; 

#hero-slider ul li {
	margin:10px 0;

#hero-slider ul a {
	text-decoration: underline;

#hero-slider a {
	background: url(button.png) no-repeat 0 0;

#hero-slider ul {
	background-position: -75px;

.panel h2 {
	padding:15px 0 0 0;

.panel p {

.clear {clear:both}

3. Javascript

Javascript is short and simple, I have added comments to explain every line of the javascript

//append click event to the UL list anchor tag
$('#hero-slider ul a').click(function () {
	//reset all the items
	$('#hero-slider ul a').removeClass('active');
	//set current item as active
	//scroll it to the right position
	$('.mask').scrollTo($(this).attr('rel'), 300);
	//disable click event
	   return false;		


That's it, a simple script that will able to make your website more interactive. Most importantly, it able to help you use the space wisely and display information in an interesting way. It's simple, and I believe it will be pretty handy for your upcoming web projects.

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.

Mark 12 years ago
How do I remove the margin on page load from panel 1? It starts off about 20px too low.
Kevin Liew Admin 12 years ago
it has a class called .panel h2, I think it has 15px top padding applied.
Jake 12 years ago
Is there anyway to make this wider? I got the initial content box to the width I'd like, but the text just stays there, as if the content box was still small.
adam 12 years ago
How can I make each icon different per icon?
Kevin Liew Admin 12 years ago
Just add a class for each of the link, and style it accordingly.
Jon 12 years ago
Great script, really easy to use and customise...One problem though -

If I refresh the page while viewing panel 3 (for example), the tab button resets to the first tab however the panel infomation remains as panel 3 (whereas it should reset to panel 1).

I've styled all of my tabs differently so this refreshing issue means the information becomes misleading.

I know this is unlikely to be noticed by users but if there is a solution it would be the icing on the 'tabbed content' cake.

It's probably something simple but my java script knowledge is also.....simple
Kevin Liew Admin 12 years ago
you can put this on page load:

$('.mask').scrollTo('#panel-1', 0);

to make sure it always start from panel 1.
David 12 years ago
Hi Kevin,

First of all, thanks for making this tutorial and sharing the scripts with all of us. I modified the slider to make the icon into purely hyperlinks and make the boxes bigger. This works well on all other browsers except Internet Explorer and I am using IE7 to test it. The problem is the hyperlinks (used to be icons) are not appearing on IE7. Can anyone please highlight to me which part of my script went wrong?

Thank you.
Krez 12 years ago
Hi Kevin! Thank you so much for writing this plugin, it works beautifully! I'm having trouble getting the active class to change correctly though. Whenever I click on another element in the list it seems to make it active but doesn't delete the active class from the previous selection. For example, on page load item 1 is active. If I click on item 2, its class will become active too. But item 1 is still active. This happens with all items on the list.

I thought it might have been something I changed in the css, but I downloaded the files all over again and it still does it. I can't tell any difference between the downloaded files and your demo page.

It happens with both current versions of Chrome and Firefox, not tested in IE yet.

Other than that, your slider works great. Definitely the easiest to use I've seen so far!

Thanks in advance!

Kevin Liew Admin 11 years ago
hmm, that kinda weird. It does have the line of code that remove all the active classes before it appends it to the current selected item. Does my demo work correctly? If you changed your class, you will need to change the javascript as well. Must have missed something tiny :)
Jessica 11 years ago
Hello! Love the slider! I made it into a larger content slider on the site I'm currently working on. I moved the tabs over to the right and converted them to images. It all works wonderfully! Only problem I have is when I click on a new tab, it scrolls the browser window up to the start of the content in the panel. This hides the header and the navigation. It's still able to scroll back up to access it, but I have a feeling our picky client wouldn't been to keen with it.
Kevin Liew Admin 11 years ago
Could be the margin top or padding top of the panel. Try to apply border on the panel, you will able to see the reason why it hides the heading.
Charley Hankins 11 years ago
Great script! I have one little problem tho. When a button is selected it doesn't go back to it's deselected state when another button is selected. All three buttons end up as selected. I haven't altered any code yet. You can see it here:
Kevin Liew Admin 11 years ago
That's weird, it shouldn't display all in selected state. In the script, it clears all the selected state before make the active one selected:

$('#hero-slider ul a').removeClass('active');
Volkan Algin 11 years ago
in index.html file replace : $('.customBlock ul a').removeClass('active'); with $('#hero-slider ul a').removeClass('active');
ganesh kharche 11 years ago
how to make auto scroll this
MUXLIMO 11 years ago
Im sorry Kevin.. i tried it on my blogspot template.. but i failed.. bcos im a newbie here..
Would You please gimme some more detail tutorial for my blog? I tried here:
Thanks in advance :)
Yogesh 11 years ago
Nice tutorial and great work Kevin. I want to just know how to make it to scroll horizontally. I want it in one of my current project. I need a horizontal content slider with previous and next arrow keys to slide the content. I don't want to use any plugin for this. Like yours i need a clean code to achieve this. Your help will be greatly appreciated.
Kevin Liew Admin 11 years ago
Horizontally, you need to make sure .slider-body width = total of slides * slide's width.

Then, you need to set #hero-slider .panel float:left.

with that, the panel will align horizontally. For the scrolling part, don't have to worry about it, scrollTo plugin will scroll it by itself as long as you get the arrangement right.
Onie Ross 11 years ago
I know and hope this will help me in my subjects in C++ and for future, ^^
Lev 11 years ago
You are the my king of Jquery Sliders and the MAIN - the explanation of them !!!