jQuery Image Gallery/News Slider with Caption Tutorial

Demonstration Download

Introduction

Until this day, jQuery is still blowing me away with its simplicity and robustness. As you can see, I have been concentrated to produce tutorials about it, and I just couldn't help it! Right, this time, we're going to learn how to create a news slider that come with the following features:

  • Slideshow with Image and description/caption
  • previous, next, pause and play buttons
  • On mouse over, pause the slideshow, and play it on mouse out
  • Sliding effect for both gallery panel and excerpt panel
  • Adjustable slideshow speed
  • And, finally, smarter script that will calculate width and height for the slideshow

We're going to use scrollTo jQuery plugin during the development. A huge thumb up and big thank you to Ariel Flesler who created the plugin.

Advertisement

Existing Tutorials

The following is my previous version of image gallery:
Simple JQuery Image Slide Show with Semi-Transparent Caption

And also, the concept that we'll be using for this gallery, it works about the same with the folowing posts, if you want a better illustrated explanations, you can read:
jQuery Sliding Tab Menu for Sidebar Tutorial, or
Create a Vertical, Horizontal and Diagonal Sliding Content Website with jQuery

Explanation

For this slide show, we will combine two sliding panels together, and use the timer (setInterval function) to animate the whole thing. The image below will able to give you more understanding of the html/css layout.

So, there are several important reminders:

  • The size of the #slider should be the same size with the image
  • The total number of excerpt items must be the same with the total number of Images
  • You must set the width and height for #slider, because the script uses it to calculate the width and height for other elements
  • Z-index/ layer order is very important to make sure everything is displayed correctly
  • Buttons can be removed or styled, if you changed the button id, you have to change it in the script as well.

1. HTML

#mask-excerpt is being set to absolute position and z-index to highest so that it will appear on the #gallery. It's possible to take the excerpt out, and make it something like this: DibuSoft mmdv. Remember, total numer of #gallery items must be the same with total number of #excerpt items.

<div id="slider">

	<div id="mask-gallery">

	<ul id="gallery">
		<li><img src="images/pier1.jpg" width="300" height="186" alt=""/></li>
		<li><img src="images/pier2.jpg" width="300" height="186" alt=""/></li>
		<li><img src="images/pier3.jpg" width="300" height="186" alt=""/></li>

	</ul>
	</div>
	
	<div id="mask-excerpt">
	<ul id="excerpt">
		<li>Cras dictum. Maecenas ut turpis. In vitae erat ac orci dignissim eleifend.</li>

		<li>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</li>
		<li>Nunc quis justo. Sed vel ipsum in purus tincidunt pharetra.</li>
	</ul>
	</div>

</div>

<div id="buttons">
	<a href="#" id="btn-prev">prev</a> 
	<a href="#" id="btn-pause">pause</a> 
	<a href="#" id="btn-play">play</a> 
	<a href="#" id="btn-next">next</a>

</div>

2. CSS

This time, CSS is a bit complicated, therefore, I have added line comments to further elaborate it. Refer the to previous illustration, we have to make sure the z-index/layer order are being set correctly, and also float:left for the #gallery so that the items are arranged horizontally. For the #excerpt, we don't have to set the float, because we want it display vertically.

The arrangement of the items will decide the scrolling direction. The arrangment of items are further elabrated in this tutoria - Create a Vertical, Horizontal and Diagonal Sliding Content Website with jQuery


#slider {

	/* You MUST specify the width and height */
	width:300px;
	height:186px;
	position:relative;	
	overflow:hidden;
}

#mask-gallery {
	
	overflow:hidden;	
}

#gallery {
	
	/* Clear the list style */
	list-style:none;
	margin:0;
	padding:0;
	
	z-index:0;
	
	/* width = total items multiply with #mask gallery width */
	width:900px;
	overflow:hidden;
}

	#gallery li {

		
		/* float left, so that the items are arrangged horizontally */
		float:left;
	}


#mask-excerpt {
	
	/* Set the position */
	position:absolute;	
	top:0;
	left:0;
	z-index:500px;
	
	/* width should be lesser than #slider width */
	width:100px;
	overflow:hidden;	
	
}
	
#excerpt {
	/* Opacity setting for different browsers */
	filter:alpha(opacity=60);
	-moz-opacity:0.6;  
	-khtml-opacity: 0.6;
	opacity: 0.6;  
	
	/* Clear the list style */
	list-style:none;
	margin:0;
	padding:0;
	
	/* Set the position */
	z-index:10;
	position:absolute;
	top:0;
	left:0;
	
	/* Set the style */
	width:100px;
	background-color:#000;
	overflow:hidden;
	font-family:arial;
	font-size:10px;
	color:#fff;	
}

	#excerpt li {
		padding:5px;
	}
	


.clear {
	clear:both;	
}

3. Javascript

We've created a function called newsslider. Each time you call it, it will scroll to the next/prev item. This function accepts a parameter called "prev", if you set it to 1/true, it scrolls to previous item, otherwise, it will scroll to the next item.

To animate the news slider, we use a timer called setInterval function, and you can set the speed by assigning the interval value in miliseconds. It's that simple. Right, the following is the script and as usual, I have put comments in every lines to assist you. :)

	
$(document).ready(function() {

	//Speed of the slideshow
	var speed = 5000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-gallery, #gallery li').width($('#slider').width());	
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsslider(0)', speed);	
	
	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Pause the slidershow with clearInterval
	$('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	$('#btn-play').click(function () {
		run = setInterval('newsslider(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsslider(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsslider(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsslider(0)', speed);	
		}
	); 	
	
});


function newsslider(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
	var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');
	
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);		
	$('#mask-excerpt').scrollTo(next_excerpt, 800);					
	
}

4. Pagination Addon Mod

Due to popular demand, I added a function called "goto" which allow you to go to a specific item. The following is the changes list:

  • Added a function called "goto"
  • In the html, you need to add "item class" to the image list and excerpt list. For example, both first image and description must have the same class name called: item1.
  • Added link list for the numbering.

Demonstration

Javascript

Add two functions into the javascript: The first one is for the link, and the second function is to navigate the slider.

	
//Put this inside $(document).ready()
//For link/number button
$('#links a').click(function () {
	//stop the slide show
	clearInterval(run);
		
	//go to the item
	goto('.' + $(this).attr('rel'));	
		
	//resume the slideshow
	run = setInterval('newsscoller(0)', speed);	
	return false;

});
	


//Add this function after newslider function
function goto(item) {
	$('#mask-gallery').scrollTo(item, 800);		
	$('#mask-excerpt').scrollTo(item, 800);	
	$(item).addClass('selected');					
}


HTML

Due to time constraint, the javascript won't generate the Link by itself, so you will have to add the links. This is how you should add the code:

  • #gallery list : the first item should call item1, second, item2 and so on.
  • #excerpt list : the first item should call item1, second, item2 and so on.
  • #links : the first link should have an attribute rel set to item1, second, item2 and so on.
	
<div id="slider">

	<div id="mask-gallery">
	<ul id="gallery">
		<li class="item1"><img src="images/pier1.jpg" width="300" height="186" alt=""/></li>
		<li class="item2"><img src="images/pier2.jpg" width="300" height="186" alt=""/></li>
		<li class="item3"><img src="images/pier3.jpg" width="300" height="186" alt=""/></li>
	</ul>
	</div>
	
	<div id="mask-excerpt">
	<ul id="excerpt">
		<li class="item1">Cras dictum. Maecenas ut turpis. In vitae erat ac orci dignissim eleifend.</li>
		<li class="item2">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</li>
		<li class="item3">Nunc quis justo. Sed vel ipsum in purus tincidunt pharetra.</li>
	</ul>
	</div>

</div>

<div id="buttons">
	<a href="#" id="btn-prev">prev</a> 
	<a href="#" id="btn-pause">pause</a> 
	<a href="#" id="btn-play">play</a> 
	<a href="#" id="btn-next">next</a>
</div>
<div id="links">
	<a href="#" rel="item1">1</a>
	<a href="#" rel="item2">2</a>
	<a href="#" rel="item3">3</a>
</div>


<div class="clear"></div>

Conclusion

So, here you go, you've just learnt how to create a news slider for your website. It isn't that hard after breaking it into smaller chucks. I hope it helps you to learn more about jQuery.

Last but not least, I need your support :) If you like this article, please help me to promote it by adding this post to your bookmark, subscribe to my RSS for more jQuery tutorial and design inspiration posts! AND also, you can BUY ME A DRINK (link in the footer) to motivate me and keep me awake! Thanks!

Demonstration Download

AdvertisementOur incredible deals of 640-553 and free db2 certification tutorials make your success certain for the final 1z0-052 dumps exam and you can get 642-617 dumps & OG0-093.

Show Some Love, Spread This Post!



131 comments

david Sat, 15th October 2011 Kevin, Once again, thank you. Please don't stop doing this, you are really inspiring me, and your work motivates me to want to learn more. Please keep it coming. Your simplicity and 'teaching' skills are exemplary
Reply
David Sat, 15th October 2011 Hi Kevin. Thank you so much. Your demonstration and ability to make it understandable and implementable is superb, especially for people who are still inexperienced at development.

? Is there a way you can move the mask-excerpt so that it lies on the bottom of the picture, not on the side as it currently is, it would make reading easier, especially if the description is long?

Thank You
Reply
Kevin Liew Sun, 16th October 2011 Hi David,

Thanks for your kind words! :) I will keep it up with somemore tutorials.

For the excerpt, you need to style this #mask-excerpt and #excerpt. Set the width to 100%, set the height also.

Then, remove "#mask-excerpt, #excerpt li" from this line in the js section.
$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());

that should be it.
Adrian Tue, 11th October 2011 Two questions; how do I centre the image slide show in a html page and secondly, and a big second, how do I make it small for a mobile screen, say a HTC device
Reply
Kevin Liew Thu, 13th October 2011 Hi Adrian, wrapped the slideshow with another div, you will need to set the width and set margin:0 auto. That will align it to center.

Make it small, I think you will need to restyle it for mobile device.
Eric P Brown Fri, 23rd September 2011 Love the tutoral I got everything but the vertical scrolling caption to work

http://romcosales.netfirms.com/romcosales.com/Silder-Entrance-2011v1.html

Any Suggestions as to what I screwed up
Reply
Niels Thu, 8th September 2011 Kevin,

I want to have a pager at the bottom of the slideshow. Is this even possible to script this? or someone has already done this?

By pager I mean a second navigation, that you can select a slide by clicking 1,2 or 3?

Thnx
Reply
Kevin Liew Thu, 8th September 2011 Hi Niels, please refer to section 4.
Gramie Tue, 16th August 2011 Hi,

I'm new to website design and clueless about Javascript; I can't write and I barely understand any of the keystrokes and why they're used.

This may seem stupid, but where do I enter the Javascript? I entered the code in with the html but it doesn't work. I was able to complete your break-down tutorial on html and css but the Javascript eludes me.

Please can you help?
Reply
Kevin Liew Wed, 17th August 2011 You need to wrap your javascript within <script type="text/javascript"></script> tag. Usually, they put it inside <head>.

However, the best practise is to link it externally. Same thing, using <script type="text/javascript" src="your-javascript-file.js"></script>, then create a file your-javascript.js (can be any name, just end it with .js extension), you can put all your javascript inside it.
dsentered Thu, 28th July 2011 Hey,
thanx for tutorial. i would like to know is it possible to stop slider after 3rd loop ( 3rd cycle ). If possible then please let me how can we make it happen. I am using this slider in this site http://dsentered.com/clientdemo/ticketwipe/ and i need to stop slider after 3rd cycle.

thanks in advance.
Reply
htk Fri, 22nd July 2011 Hi everybody,
how I can do another look for selected numeric link? For example by adding a class to the link. But how I can do that?
Reply
Laura Fri, 24th June 2011 I love the appearance of your slider and was going to use it. However, when I downloaded the javascript (copied and pasted it into a new Dreamweaver 5.5 js page -- it said there was a syntax error in the following section ($(document).ready(function() { //Speed of the slideshow var speed = 5000; ) and that it might not work properly. Since I don't know how to write javascript code, was unable to use it. It may have been something I did, but thought you would like to know about what DW reports. Best wishes
Reply
Kevin Liew Mon, 27th June 2011 Have you tried it on browser? DW can be acting weird sometimes. If it didn't work, try the zip file version.
marc Tue, 21st June 2011 Hello,
actually the pictures will slip or move from the right side to the left. how can i change it from left to right?
sorry for my bad english
regards
marc
Reply
Kevin Liew Wed, 22nd June 2011 that's tricky, the scrolling is made by scrollTo plugin.. the javascript in this tutorial is too simple, to achieve that, you will require a rewrite.
Antony Sun, 25th September 2011 You can the left to right slide by recursively calling the .prev() element instead of calling .next().

Here is my code(modified):

<script type="text/javascript">
var FORWARD = 0;
var REVERSE = 1;
var _speed_timer = 2000;
var _speed_transition = 1000;

$(document).ready(function(){
$("#mask-gallery #gallery li").width($("#slider").width());
$("#gallery").width($("#slider").width() * $("#gallery li").length);
$("#gallery,#mask-gallery,#gallery li").height($("#slider").height());

//setInterval('scrollImages('+ FORWARD +')',50);
var run = setInterval('scrollImages('+ FORWARD +')',_speed_timer);
$("#gallery li:first").addClass('selected');

$('#btn-previous').click(function(){
//alert('previous');
scrollImages(REVERSE);
clearInterval(run);
run = setInterval('scrollImages('+ REVERSE +')',_speed_timer);
return false;
});


});

function scrollImages(command){
var current_image = $("#gallery li.selected").length > 0 ? $("#gallery li.selected") : $("#gallery li:first");
var next_image;

if(command == FORWARD){
next_image = current_image.next().length > 0? current_image.next():$("#gallery li:first");
}
else if(command == REVERSE){
next_image = current_image.prev().length > 0? current_image.prev():$("#gallery li:last");
}

$("#gallery li").removeClass('selected');
next_image.addClass('selected');

$("#mask-gallery").scrollTo(next_image,_speed_transition)
}
</script>

PS:This modified code works better because it resets the timer for every button action. I faced some haphazard scrolling when timers where no reset.
Jacob Sun, 19th June 2011 This is an amazing script, however I do have a simple question that will hopefully be simple to answer. What if I want more than one on a page? I changed all the CSS IDs and stuff to keep them separate, but when I loaded up the page the controls only worked on one box, and only that box animated, and it stops working all together when you try to use the controls.
Reply
Kevin Liew Wed, 22nd June 2011 could be the id, id have to be unique, in this case, you might want to change it to class... or, duplicate the script and use different id.
Matt Wed, 8th June 2011 Hi ! I've got an issue with the script on Google Chrome... If you take a look at my homepage (www.ateliersnowflake.com), the slider works correctly (I made the choice not to display captions). But If I open an application on my computer, and go back to the website 2 minutes after, the slider becomes crazy (rolls really fast). No problem on my other browsers...
Reply
Kevin Liew Tue, 14th June 2011 something to do with the animate method, the browser queue all the animations when you switched away from the browser, and once you get back to it, it executes the whole queue.... unfortunately, im still looking for solution for this matter...
Paul Tue, 6th September 2011 Hi Kevin,

Any solution to this matter found?

Thanks!
Kevin Liew Thu, 8th September 2011 From jQuery:
Because of the nature of requestAnimationFrame(), you should never queue animations using a setInterval or setTimeout loop. In order to preserve CPU resources, browsers that support requestAnimationFrame will not update animations when the window/tab is not displayed. If you continue to queue animations via setInterval or setTimeout while animation is paused, all of the queued animations will begin playing when the window/tab regains focus. To avoid this potential problem, use the callback of your last animation in the loop, or append a function to the elements .queue() to set the timeout to start the next animation.

Possible fix:
http://stackoverflow.com/questions/4430277/jquery-animate-when-div-loses-focus

Sorry, I haven't have any time to play with it, but read more about .queue(). Let me know if it's help.
behlul Tue, 7th June 2011 hi thanks for the share,

everything works, however I want to put this whole thing right in the middle of my page. but it is floating to the left.anyone?
Reply
Kevin Liew Tue, 14th June 2011 make a div to wrap around it, set width and apply margin:0 auto to the new div you have just created.
Yanike Thu, 12th May 2011 Issue: The slider doesn't pause when you click it after pausing and playing it prior. If you click twice or more on play, the slider will speed up faster and there's no way to stop it with pause. Also, when playing it would be nice to stop the slider when you select to go prev or next.
Reply
Mo Wed, 4th May 2011 Nice one, Is it allowed to use it on my homepage?
Reply
Kevin Liew Sun, 15th May 2011 Yes, you can do whatever you want with it.
Raph Thu, 24th March 2011 Great ! just need to add scrollTo plugin to make work it, but very simple and light.
thanks
scrollTo plugin : http://plugins.jquery.com/project/ScrollTo
Reply
Michael Wed, 2nd March 2011 Images on the list in gallery are not arrangged horizontally but vertically even if I use float: left in #gallery li. Can you help with that?
Reply
Kevin Liew Thu, 3rd March 2011 I think, you probably forget to change the width of #gallery.
denny Fri, 4th February 2011 Hi, height of my images is 614 px and I am having problem with scrolling the text. Any suggestion?
Reply
Ale Sat, 22nd January 2011 Hey Thanks a lot!
Reply
jack Sat, 18th December 2010 That's a wonderful trick
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement