Register now or login here to start promoting your blog and your favourite articles.
jQuery Photo Slide Show with Slick Caption Tutorial Revisited
16 Dec 2009 - 152 Comments
A revisit from my previous famous Image slide show with semi-transparent caption jQuery tutorial. I have fixed bugs and enhanced this image slide show to be more efficient, w3c compliant and more semantic. Quick! Learn how to create a slide show with cool sliding caption effect using jQuery.
Author: kevin | Source: queness
Demonstration Download

Introduction

If you have been reading my website from the start you would have read this tutorial:

Simple jQuery Image Slide Show with Semi Transparent Caption

It's a very famous post because it has been showcased in several major web design blogs so many times and I received a lot of traffic from it. Thanks guys. I get a lot of comment and as I replied to those comments and do some quick fixes, I realized that this script has a caption bug and inefficient and not up to standard (oh well, that's the time when I was start picking up jQuery :)), so I have decided to do revisit to solve all those problems discovered by readers. I rewrote the html structure and modify the script.

Mission Objectives:

  • Restructure the HTML, more semantic
  • Fix the caption bug, it displays the next caption too fast
  • W3C standard compliant, using appropriate attribute to store title and description
  • Cross Browser - Chrome, Safari, IE6, 7 and Firefox. For IE8 you need to use correct DocType, it has an issue with the caption sliding up and down.

However, one thing though, it needs javascript, if your browser is javascript disabled, sorry, there is no graceful degradation for this script, it will just display the first item.

1. HTML

Yes, I'm using list this time :) This is how it should have been afterall. No more REL attribute, we wil be using TITLE Attribute to store the heading and ALT attribute to store the description. For the caption elements, it will be added using jQuery. So, this is it - a clean and simple HTML code.

<ul class="slideshow">
	<li class="show"><a href="#"><img src="images/s1.gif" width="450" height="200" title="Slide 1" alt="Short Description"/></a></li>
	<li><a href="#"><img src="images/s2.gif" width="450" height="200" title="Slide 2" alt="Short Description"/></a></li>
	<li><a href="#"><img src="images/s3.gif" width="450" height="200" title="Slide 3" alt="Short Description"/></a></li>
</ul>

2. CSS

CSS code is rewritten completely. I guarantee it's cross browser compliant. They all look identical and it works great :) I did learn a lot of CSS technique through this blog. We all can improve our skills through tips, tricks, trials and errors. Read these posts to learn more about CSS:


body {
	font-family:arial;	
	font-size:12px;
}

ul.slideshow {
	list-style:none;
	width:450px;
	height:200px;
	overflow:hidden;
	position:relative;
	margin:0;
	padding:0;
	
}	

ul.slideshow li {
	position:absolute;
	left:0;
	right:0;
}

ul.slideshow li.show {
	z-index:500;	
}

ul img {
	border:none;	
}

#slideshow-caption {
	width:450px;
	height:70px;
	position:absolute;
	bottom:0;
	left:0;	
	color:#fff;
	background:#000;
	z-index:500;
}

#slideshow-caption .slideshow-caption-container {
	padding:5px 10px;	
	z-index:1000;	
}

#slideshow-caption h3 {
	margin:0;
	padding:0;	
	font-size:14px;
}

#slideshow-caption p {
	margin:5px 0 0 0;
	padding:0;
}

3. Javascript

We will going to use call back function to display caption. This will solve the "Caption Is Appearing Before The Next Slide Syndrome" :). Not too much of changes in javascript but I do added a feature though. On mouse over the slide will pause, and resume it back on mouse out. I think it's a good touch.

$(document).ready(function() {		
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(2000);

});

function slideShow(speed) {


	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('
  • '); //Set the opacity of all images to 0 $('ul.slideshow li').css({opacity: 0.0}); //Get the first image and display it (set it to full opacity) $('ul.slideshow li:first').css({opacity: 1.0}); //Get the caption of the first image from REL attribute and display it $('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title')); $('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt')); //Display the caption $('#slideshow-caption').css({opacity: 0.7, bottom:0}); //Call the gallery function to run the slideshow var timer = setInterval('gallery()',speed); //pause the slideshow on mouse over $('ul.slideshow').hover( function () { clearInterval(timer); }, function () { timer = setInterval('gallery()',speed); } ); } function gallery() { //if no IMGs have the show class, grab the first image var current = ($('ul.slideshow li.show')? $('ul.slideshow li.show') : $('#ul.slideshow li:first')); //Get next image, if it reached the end of the slideshow, rotate it back to the first image var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first')); //Get next image caption var title = next.find('img').attr('title'); var desc = next.find('img').attr('alt'); //Set the fade in effect for the next image, show class has higher z-index next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000); //Hide the caption first, and then set and display the caption $('#slideshow-caption').animate({bottom:-70}, 300, function () { //Display the content $('#slideshow-caption h3').html(title); $('#slideshow-caption p').html(desc); $('#slideshow-caption').animate({bottom:0}, 500); }); //Hide the current image current.animate({opacity: 0.0}, 1000).removeClass('show'); }

    Updates

    Updated the article and scripts, the link issue is fixed. :) Sorry for the delay.

    Conclusion

    I have made a lot of tutorials, and I think it's good to do a revisit to make it more efficient and solve some of the annoying bugs instead of quick fixes. So, yea, from now on, I will check my previous tutorials and rewrite them. :)

    Like this tutorials? You can express your gratitude by visiting my sponsors on the sidebar, bookmark it and help me to spread this tutorial to our friends! :) Thanks!

    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

    Dan on 1 Sep 2010 says:
    Great job documenting this method out...It's simplicity is really easy to understand. I actually didn't need the caption piece of it though and although all I have to do is make the caption opacity 0.0, I know there is still processing going on from the script so I'm working on disabling that further for now. Thanks again and keep up the great work.
    mIKE on 27 Aug 2010 says:
    Is there any way to call a slide from a click on a button and ther running the slide-show from that slide further?
    Ido on 26 Aug 2010 says:
    @My Nguyen: replace

    $('ul.slideshow li:first').css({opacity: 1.0});

    with

    $('ul.slideshow li').eq(Math.floor(Math.random()*$('ul.slideshow li').size())).addClass('show').css({opacity: 1.0});
    Haris on 26 Aug 2010 says:
    Hello..nice script..
    I am in blogger though..
    I have made everything as it is suppposed to be. but I cannot put the javascript and the images do not change. I have firefox so it should do the trick.

    Jure on 25 Aug 2010 says:
    Very very nice script, simple and working :)

    Can we add number buttons for each slide and script for showing that slide?
    I tried something but I need a little help

    thx
    kevin on 24 Aug 2010 says:
    @Paul: Thanks Paul for pointing out!
    @Luke: Please try paul's solution, it works for some people as well
    @My Nguyen: No, unfortunately, you can't randomize it. If you use PHP to populate the images, you can randomize it using PHP.
    My Nguyen on 24 Aug 2010 says:
    Help: is there a way to randomize the picture order when the slide plays? I'd like to put this on my home page but I would like the viewers to see a different starting image each time they enter. let me know if you guys can help!
    Paul on 20 Aug 2010 says:
    The caption bug seems to be a problem peculiar to IE8 and JQuery rather than this script (which is excellent btw!).
    A solution which worked for me was to download and use the latest version of JQuery from jquery.com (ver. 1.4.2 at the time of writing) and then place:
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    immediately after the <head> tag, which forces IE8 to behave like IE7.
    (Good old IE - always looking backwards to go forwards!).
    Hope that helps!
    Luke on 5 Aug 2010 says:
    Scratch that - I think I may have found a bug. Try putting a slash after the page and running it in IE - seems to bust it.
    Luke on 5 Aug 2010 says:
    Hey guys - awesome work on the script! Was just about the closest thing to what I needed. I've modified it to not be automated, and instead to work with directional buttons (well, almost), but for some reason it isn't working in internet explorer 8 =[ - Works just swell in Firefox though. Can anyone help me out all please? I know it's none of the changes that I've made, because I've tried reverting to the default script, and it just wont work at all. Thanks a lot =]


    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

    Web Hosting Rating

    Buy WOW Gold

    Get your cdl today

    Debt collector review

    •  
    •  
    •  
    •  
    •  

    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