You can see the working demo here.
Carousel is a way to present content in the form of slideshow on web page. Here I’m showing how easily you can create a horizontal carousel using jQuery. You can play with the given code and easily create vertical carousel, circular carousel, auto-scrolling carousel etc.
I’m using jQuery for animation. So, you need jQuery JavaScript library for it. You can download latest jQuery JavaScript library from jquery.com.
So, how it work.
Your basic structure of HTML for Carousel should be like this:-
<div id="my_carousel">
<ul>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
<li>some content...</li>
.....
</ul>
</div> |
What you need to do before applying “my_carousel” ID to your Carousel DIV.
1. Put any HTML content which you want to display in your LI’s.
2. Create CSS for your Carousel DIV.
3. You also need two images, or button to scroll your content in left and right direction.
4. Assign “btnprev” class to your left button or image.
5. Assign “btnnext” class to your right button or image.
6. Assign “my_carousel” ID to you Carousel DIV.
How JavaScript Code work:-
So, when browser load the page JavaScript look for “my_carousel” ID in HTML and apply all the require attributes and events to your DIV, UL, and LI.
Important JavaScript variables and their purpose:-
step – How many LI’s you want to scroll on click of left and right button.
current – Index or Number of left most visible LI in carousel.
maximum – Total number of LI in carousel.
visible – Number of visible LI’s.
speed – Animation speed.
liSize – Width of one LI in pixels.
carousel_height – Carousel DIV height.
ulSize – Width of UL. You can calculate it like this liSize * maximum.
divSize – Width of CArousel DIV. You can calculate it like this liSize * visible.
Assign require attributes to DIV, UL, and LI.
$('#my_carousel').css("width", divSize+"px").css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative"); $('#my_carousel ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute"); |
Here we changed the width, height, visibility, overflow, and position of Carousel DIV. The Carousel DIV overflow = hidden property is very important here, through it we are just showing the require(visible) LI’s which we want to show at a time. We just hide the remaining LI’s.
We also changes the width, left and position of UL.
Assign Click event to left and right buttons/images
Left button/image
$('.btnprev').click(function() { if(current - step < 0 || current - step > maximum - visible) {return; } else { current = current - step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); |
So, we have assigned a click event on previous button. So, we are first checking the index of current LI. If the current LI is the first LI we will not do anything. We will simply return from the function. If the current LI is not the first LI then scroll the content of UL to left hand side by using the jQuery “animate” function. For more information about animate function click here. And we also changed the current to current left most most visible LI.
Right button/image
$('.btnnext').click(function() { if(current + step < 0 || current + step > maximum - visible) {return; } else { current = current + step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); |
Similarly we assigned the clicked event on next button and followed the same logic what we used for previous click. We just changed it slightly to check last LI when we click on next button. If the current is last LI we will do nothing.
Complete JavaScript Code:-
<script language="javascript"> $(function() { var step = 2; var current = 0; var maximum = $('#my_carousel ul li').size(); var visible = 2; var speed = 200; var liSize = 331; var carousel_height = 161; var ulSize = liSize * maximum; var divSize = liSize * visible; $('#my_carousel ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute"); $('#my_carousel').css("width", divSize+"px").css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative"); $('.btnnext').click(function() { if(current + step < 0 || current + step > maximum - visible) {return; } else { current = current + step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); $('.btnprev').click(function() { if(current - step < 0 || current - step > maximum - visible) {return; } else { current = current - step; $('#my_carousel ul').animate({left: -(liSize * current)}, speed, null); } return false; }); }); </script> |

#1 by Pedro Rogério - February 16th, 2010 at 03:28
Very good, I’ve always been looking for a simple way to make a Carousel with jQuery without using plugins but could not find. Your example is very simple and it works perfectly. Thank you.
#2 by Matt Zimmermann - July 12th, 2010 at 22:54
Hi Anil,
Excellent tutorial, thank your for this. I have been looking for a non-bloated way to accomplish this and you made it very easy to understand.
I have two questions:
1. How can I make this auto-scroll like you mentioned?
2. Is there anyway to make it a circular carousel so it is “infinite”?
Thank you,
Matt
#3 by Andy - October 5th, 2010 at 19:51
Is there a way to link directly to a specific panel?
#4 by John Jarratt - November 8th, 2010 at 20:43
Many thanks for an excellent program. It works wonderfully for images, but I cannot make it work for other content such as text or – and this is what I’m really after – hyperlink references. Can you help, please?
Thanks.