Create a Thumbnail Gallery with Slick Heading and Caption Effect with jQuery

Written by Kevin Liew on 05 Aug 2009
223,506 Views • Tutorials

Introduction

This is a simple jQuery tutorial, but can be extremely useful in frontend design. It's an elegant way to put more content in your website, but only display it when the user wants to see it. It keeps your website clean, but in the same time does not compromise your website content.

You can check out the demonstration and the code. :) enjoy.

1. HTML

It's simple, inside the container - photo class you have three objects, first div is the heading, image in the middle and last div is the caption. You can put links, rating whatever you like inside it.

<div class="photo">
	<div class="heading"><span>Pier</span></div>
	<img data-src="images/pier1.jpg" width="300" height="186" alt="" />
	<div class="caption"><span>Lorem de scua shemf huan rougt ecrit vato de souju kanasa.</span></div>
</div>

2. CSS

One thing you have to know about the CSS is, if you want to position objects within a container, the container must have relative position and the object must have absolute position too. In this case, photo class is the container, heading and caption classes are the objects with absolute position.

So, it's easy, by default, heading and caption classes are hidden with top (heading) set to -50px (the height of the heading) and same thing to caption, but instead of top, it's bottom.

I have shared this CSS tips in my 15+ CSS Tips and Tricks posts

	.photo {
		/* relative position, so that objects in it can be positioned inside this container */
		position:relative;
		font-family:arial;	
		
		/* hide those extra height that goes beyong the size of this container */
		overflow:hidden;
		border:5px solid #000;
		width:300px;
		height:186px;
	}	
	
		.photo .heading, .photo .caption {
			/* position inside the container */
			position:absolute;
			background:#000;
			height:50px;
			width:300px;
			
			/* transparency for different browsers */
			/* i have shared this in my CSS tips post too */
			opacity:0.6;
			filter:alpha(opacity=60);  
		     -moz-opacity:0.6;    
			-khtml-opacity: 0.6;  

		}
		
		.photo .heading { 
			
			/* hide it with negative value */
			/* it's the height of heading class */
			top:-50px;
		}

		.photo .caption { 		
			/* hide it with negative value */
			/* it's the height of bottom class */
			bottom:-50px;		
		}
		

		/* styling of the classes*/
		.photo .heading span {

			color:#26c3e5;	
			top:-50px;
			font-weight:bold;
			display:block;
			padding:5px 0 0 10px;
		}
		
		.photo .caption span{
			color:#999;	
			font-size:9px;
			display:block;
			padding:5px 10px 0 10px;
		}

3. Javascript

Javascript is pretty straight forward, just display the div and hide the div, the only difference between the heading and caption is, the css properties (top and bottom), I added the easing as well to give your more option in choosing the right transition for your image thumbnail gallery.

	
	$(document).ready(function () {

		// transition effect
		style = 'easeOutQuart';

		// if the mouse hover the image
		$('.photo').hover(
			function() {
				//display heading and caption
				$(this).children('div:first').stop(false,true).animate({top:0},{duration:200, easing: style});
				$(this).children('div:last').stop(false,true).animate({bottom:0},{duration:200, easing: style});
			},

			function() {
				//hide heading and caption
				$(this).children('div:first').stop(false,true).animate({top:-50},{duration:200, easing: style});
				$(this).children('div:last').stop(false,true).animate({bottom:-50},{duration:200, easing: style});
			}
		);

	});

Conclusion

This is a simple jQuery tutorial, but can be extremely useful in frontend design. It's an elegant way to put more content on your website, but only display it when the user want it. It keeps your website clean, but in the same time provide all the information to your visitors.

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!

Demo Download
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

50 comments
kevin Admin 15 years ago
Hi Jo, thanks very much for the amendment! really appreciate it! Will remember the rapid fire problem in my future tutorial!
Reply
Alexander Bickov 15 years ago
Nice post for my developers :)
Reply
FireDart 15 years ago
Been looking for something like this.
Reply
Hezi 15 years ago
it ROCKS!
Reply
Jo 15 years ago
Great but you can modify your javascript for :

$('.photo').hover(
function() {
//display heading and caption
$(this).children('div:first').stop(false,true).animate({top:0},{duration:200, easing: style});
$(this).children('div:last').stop(false,true).animate({bottom:0},{duration:200, easing: style});
},

function() {
//hide heading and caption
$(this).children('div:first').stop(false,true).animate({top:-50},{duration:200, easing: style});
$(this).children('div:last').stop(false,true).animate({bottom:-50},{duration:200, easing: style});
}
);
Also, you fix the rapid movement of the mouse
Reply
cri 15 years ago
Great but doesn't work in internet explorer 7
Reply
viswanath 15 years ago
thank u very much..!
Reply
tosin 15 years ago
The other text at the bottom is not showing in the evil browser - I.E 7
Reply
kevin Admin 15 years ago
Hi Wink! Thanks a lot! :) Legendary!

@Jeff, you need to set the top and bottom of the heading and excerpt to 0 and ignore the rest of the javascript. :)
Reply
wink 15 years ago
The IE7 problem can be fixed by adding left:0px; to the caption class.
(so .photo .caption { bottom:-50px; left:0px; }
Reply
jeff 15 years ago
HI;
great script !. I would like the caption to be visible at all times , any advise ?

tks
Reply
jack rudo 15 years ago
great post. I would love to have this included as part of a layout with rows / cols and paging. Have you developed this further?

Thanks
Reply
wink 15 years ago
Hey Kevin, no problem, glad I could help :)
Reply