Click To Launch Showreel

jQuery carousel

Download
View

Carousel is a looping carousel jQuery plugin that allows for a simple, yet effective carousel effect harnessing the power of the jQuery library combined with CSS and allowing for graceful degradation.

The plugin works by scrolling through the elements held inside a container, once it reaches the end of that list, it takes the contents of that container and appends it to the end of it, thus making the container double the length with its contents repeated twice. Once you get to the end of the newly enlarged container, it does the same thing again – hence making the infinite carousel loop.

The code (demo, jQuery and CSS) are bundled with this package. Let's start with the front end.

Unfortunately, due to time constraints, the actual HTML used to mark up the carousel is fairly ridged. I.e, you can't deviate from it much without modifying the js file, although with some cleaver CSS trickery, you can customise the view quite considerably should you need to.

The HTML markup is as follows:

HTML

						<div id="slider_wrapper">
							<a href="#" id="prev">Previous Button</a>
							<a href="#" id="next">Next Button</a>
							<div id="slider_container">
								<div id="slider">
									<p>First Element</p>
									<p>Second Element</p>
									<p>Third Element</p>
									<p>Fourth Element</p>
									<p>Fifth Element</p>
								</div><!-- slider -->
							</div><!-- slider_container -->
						</div><!-- slider_wrapper -->
					

NOTE: All IDs / class names are entirely variable. You can change them to whatever you please.

The slider_wrapper element is the element used to contain the carousel. It also serves as the mask for the carousel. It has a CSS property of 'overflow:hidden' so anything outside of the predefined width range for this element will be hidden from the users view. This must be set to display:block; and position:relative;.

Within that, you have two links with the ID's 'prev' and 'next' (again, entirely variable). These are the links used to control the motion of the slider (left / right). It is a good idea to position these elements absolutely.

The next element is a div with the ID of 'slider_container'. This acts as a container for the content that will be scrolled through. It needs to be set to position:relative;.

Next is a div with the ID of 'slider', this is the DIV that actually moves. This needs to be floated left, but you also need to give it a left position, even though it will be 0: 'left:0px;'. It is set with a very large width. In the example, I have 5 paragraph tags. Each P tag is set to have a width of 645px. The slider DIV must have the same width as element width X number of elements. I have 5 elements, each at 645px, so the slider DIV width is set to 3225px. This is VERY important.

Inside the slider element is the content that you wish to scroll through. These must be floated left. The don't have to be P tags, they can be pretty much anything you want, providing they are assigned a width and floated left (paragraphs, links, spans, divs, list items etc).

The CSS is commented throughout so there isn't much need to detail it much more in this document, although you can modify the properties (width etc), you need to keep the properties that have been stressed in this document (overflows, floats, displays and positions).

CSS

						#slider_wrapper {
							width:700px;
							margin:0;
							padding:0 0 0 20px;
							display:block;		/* Important */
							position:relative;	/* Important */
							overflow:hidden;	/* Important */
						}

						#slider_container {
							height:50px;		/* Important (although height doesn'''t have to be 50px, but it does need a height) */
							margin:0;
							padding:0;
							position:relative;	/* Important */
						}

						#slider {
							margin:0;
							padding:0;
							left:0px;
							position:relative;	/* Important */
							float:left;			/* Important */
							width:3225px;		/* Important */
						}

						#slider p {
							float:left;			/* Important */
							width:645px;		/* Important */
							text-align:center;
							margin:0;
							padding:0;
						}

						#prev {
							top:0;
							left:0;
							position:absolute;	/* Important */
							z-index:100;		/* Important */
						}

						#next {
							top:0;
							right:0;
							position:absolute;	/* Important */
							z-index:100;		/* Important */
						}
					

jQuery

						(function($){
							$.fn.carousel = function(options) {
								// default variables
								var defaults = {
									incrementor: 645,
									next_button: '#button_next',
									prev_button: '#button_prev',
									speed: 300
								};
								
								// combine predefined variables and user-passed variables
								var options = $.extend(defaults, options);
								
								return this.each(function() {
									// click handler for the next button
									$(options.next_button).click(function() {
										// assign the parent DIV to _element variable for ease of use
										var _element = $(this).next('div').find('div:first');
										// check to see if the element is currently animated. if it is, do nothing. this is to prevent width issues
										if($(_element + ':animated').size() == 0) {
											// get _elements left value
											var arr = _element.css('left').split('px');
											// deduce incremntor value from the left property
											var new_left = arr[0] - options.incrementor;
											// remove the 'px' from the end of the elemnts width value and store result to variable
											var arr = _element.css('width').split('px');
											// set width variable to first key of the array generated from the split
											var width = arr[0];
											// if the new left position will be less than or equal to the max width (inverted to minus)
											if(new_left <= ((width<0) ? width * -1 : width * -1)) {
												// get the elements width
												var arr = _element.css('width').split('px');
												var nwidth = arr[0];
												// set a new width
												_element.css('width', parseInt(nwidth) + parseInt(nwidth) + 'px');
												// append content
												_element.append(_element.html());
											};
											// animate
											_element.animate({left: new_left + 'px'}, {queue: false, duration: options.speed});
										};
										return false;
									});
									// click handler for prev button
									$(options.prev_button).click(function() {
										// assign the parent DIV to _element variable for ease of use
										var _element = $(this).parent().find('div').eq(1);
										// check to see if the element is currently animated. if it is, do nothing. this is to prevent width issues
										if($(_element + ':animated').size() == 0) {
											// get the width
											var arr = _element.css('left').split('px');
											// calculate new width
											var new_left = parseInt(arr[0]) + parseInt(options.incrementor);
											var arr = _element.css('width').split('px');
											var width = arr[0];
											// if the new left postiion is less than zero
											if(new_left <= 0) {
												// animate
												_element.animate({left: new_left + 'px'}, {queue: false, duration:options.speed});
											};
										};
										return false;
									});
								});
							};
						})(jQuery);
					

To instantiate the carousel, you will need to call the following line in the <head> of your page, after you have included the jQuery library and the carousel plugin.

$('#slider_wrapper').carousel();

The plugin accepts 4 arguments:

incrementor: The amount you want to shift the slider in each singular motion. This needs to be the same amount as the width of your elements inside the slider DIV (in the example: 645). Default value:645.

next_button: The ID (or class name) of the next button. Default value: button_next.

prev_button: The ID (or class name) of the previous button. Default value: button_prev.

speed: The speed (in milliseconds) of each transition effect. Default value: 300.

If you have an application of the carousel plugin that uses the same default values (incrementor:645, next_button: button_next, prev_button: button_prev and speed: 300) then you only need to write:

$('#slider_wrapper').carousel();

However, if your properties are different, you will need to write the following:

$('#slider_wrapper').carousel({incrementor: 500, next_button: '#next', prev_button: '#prev'});

The very first part of the instantiation of the plugin $('#silder_wrapper') will depend on the ID of the DIV wrapping the carousel. If you have a DIV with a different ID, just change it when you instantiate the plugin. I.e.

$('#my_div_name).carousel();

And that's really about it. The download link at the top container a demo and all the relavent files. Aswell as a compressed and uncompressed version. Enjoy. Any questions ask or email me.

YouTube

Ok, I bought a Peavey 6505 head about a month ago and was completely blown away by it's tone ever since. This thing is amazing. Sorry for going on a bit with the sound demos but once I hear this thing I just can't stop playing!

Send me an e-back

What's an eback? It's like a callback, but without the call. Complete the form below to drop me an e-mail and I'll e-mail you back as soon as I can.



eback »

Bring Down IE6!