Register now or login here to start promoting your blog and your favourite articles.
Create a Custom Content Slider with jQuery
8 Jun 2010 - 21 Comments
In this tutorial, we are going to create a custom content slider. The concept behind it is pretty simple, when you click on a button, it uses jQuery scroll-To plugin to scoll the content and display the right content.
Author: kevin | Source: queness
Demonstration Download

Introduction

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

1. HTML

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">
	<ul>
		<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>
	</ul>
	<div class="mask">
		<div class="slider-body">
			<div class="panel" id="panel-1">
				<h2>Title 1</h2>
				<p>Paragraph</p>
			</div>
			<div class="panel" id="panel-2">
				<h2>Title 2</h2>
				<p>Paragraph</p>
			</div>
			<div class="panel" id="panel-3">
				<h2>Title 3</h2>
				<p>Paragraph</p>
			</div>
		</div>
	</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 {
	text-align:left; 
	background-color:#efefef; 
	border:1px solid #ccc; width:450px;
	-moz-border-radius:10px;
	-webkit-border-radius:10px;
	margin:0 auto;
	font-family:arial;
}

#hero-slider .mask { 
	float:left; 
	width:300px; 
	height:280px; 
	margin:15px 0 0 10px; 
	overflow:hidden;
}

#hero-slider .panel { 
	width:300px; 
	height:280px; 
	text-align:left;
}

#hero-slider ul {
	margin:0; 
	padding:15px 15px 0 15px; 
	list-style:none; 
	float:left; 
	border-right:1px solid #dedede; 
	height:285px;
}

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

#hero-slider ul a {
	outline:none; 
	text-decoration: underline;
	display:block;
	width:75px; 
	height:74px; 
	text-indent:-999em;	
}

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

#hero-slider ul a.active {
	background-position: -75px;
}

.panel h2 {
	padding:15px 0 0 0;
	color:#0058a9;
}

.panel p {
	color:#666;
}

.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
	$(this).addClass('active');	
		
	//scroll it to the right position
	$('.mask').scrollTo($(this).attr('rel'), 300);
		
	//disable click event
	   return false;		
		
});

Conclusion

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.

Demonstration Download

Copyright & Usage

The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.

Share This Post to Support Me! :)


Comments

Nerf on 13 Aug 2010 says:
My slider is not working ... dont get the problem... anyway you could help ?

all pieces of code are same .. hmh jquery files are in the folder.. and css also... wot "could"go wrong?
peter on 20 Jul 2010 says:
Great looking slider.

Just curious - would you be able to do the same thing using .animate()?
krissy on 14 Jul 2010 says:
@Kevin

thanks for replying. It seems there was a problem with conflicting jquery libraries begin called for different functions.

Im a noob to this. but I have tried the custom jquery variable names and had no luck. If you have any insight on making multiple jquery functions work that would be appreciated.

GREAT TUTORIAL!!! CHEERS
Manhattam on 12 Jul 2010 says:
Oh no, I'm really sorry, it works like a charm. I'm a stupid
Manhattam on 12 Jul 2010 says:
Sorry for the repeated line of code.
Manhattam on 12 Jul 2010 says:
Thanks Kevin, but it didn't work. :-(
my code is
<script type="text/javascript">
$(document).ready(function () {

$('#hero-slider ul a').click(function () {
$('.mask').scrollTo('#panel-1', 0);
//reset all the items
$('.mask').scrollTo('#panel-1', 0);
$('#hero-slider ul a').removeClass('active');

//set current item as active
$(this).addClass('active');

//scroll it to the right position
$('.mask').scrollTo($(this).attr('rel'), 300);

//disable click event
return false;

});

});
</script>
kevin on 10 Jul 2010 says:
@krissy: I have tested it with IE 7, it works perfectly.

@manhattan: ok, u need to scroll to the original position on page load. Put this line inside $(document).ready()

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

Try that, so it scroll to the first item on page load.
Manhattan on 10 Jul 2010 says:
Thanks Kevin for this awesome article, I think there is a problem, when you refresh the page, the slider does not reset ! (Tested in FF, IE)
how to reset slider on refresh?
Thanks
krissy on 7 Jul 2010 says:
There is an issue with this jquery loading inside internet explorer 7.
Any ideeas why or whats happening?

here is a link to my site. thanks.
http://dcglamstudio.com
kevin on 4 Jul 2010 says:
@dave: so I guess it work? :)


Leave a comment

Subscribe RSS Subscribe RSS, Keep yourself updated with queness latest posts!
Pixel Crayons

Buy wholesale computers directly from China at DHgate.com

Discover the tools to build your e-Commerce Site with Netfirms

Buy China Products from Made-in-China.com

Cocktail Dresses

SmartPhone Cell Phone

Wholesale electronics

VPS Hosting - cPanel virtual servers from Host Color

Web Hosting Rating

Buy WOW Gold

  •  
  •  
  •  
  •  
  •  

Community News

Recent Comments

Random Posts


View all posts and news Back to top

About the Author

A web designer and developer who is passionate and keen on contributing to the web development industry. Feel free to say hi to me, or follow me on twitter.

Kevin Liew