jQuery Thumbnail with Zooming Image and Fading Caption Tutorial

11 January 2010 Monday

This application was viewed 1103 times




jQuery Thumbnail with Zooming Image and Fading Caption Tutorial

Learn how to build thumbnail gallery with zooming effect and fadein and fadeout caption. This tutorial will walk you through the concept behind it and how to code it.

View the Demo

Introduction

I came across quite a lot of CSS gallery websites that using huge thumbnail to showcase beautiful websites. Some of them are just ordinary thumbnail, but a lot of them have javascript animated caption to convey more information to viewers. In this tutorial, we will learn how to make ordinary thumbnail to something more. This is my second tutorial about thumbnail gallery, if you missed the first one, you might want to read it later - Create a thumbnail gallery with slick heading and caption effect.

1. HTML

  1. <div class="zitem">  
  2.     <a href="#"><img src="1.jpg" alt="Test 1" title="" width="125" height="125"/></a>  
  3.     <div class="caption">  
  4.         <a href="">Test 1</a>  
  5.   
  6.     </div>  
  7. </div>  
  8.   
  9. <div class="zitem">  
  10.     <a href="#"><img src="2.gif" alt="Test 2" title="" width="125" height="125"/></a>  
  11.     <div class="caption">  
  12.         <a href="">Test 2</a>  
  13.   
  14.     </div>  
  15. </div>  
  16.   
  17. <div class="zitem">  
  18.     <a href="#"><img src="3.png" alt="Test 3" title="" width="125" height="125"/></a>  
  19.     <div class="caption">  
  20.         <a href="">Test 3</a>  
  21.   
  22.     </div>  
  23. </div>  

2. CSS

I have added inline comment in the following CSS code. If you want to learn more about CSS, read my previous post 15 CSS Tips and Tricks or, you want to know MORE ABOUT CSS, read my new post - 15 Ways to Optimize CSS and Reduce CSS File Size

  1. .zitem {   
  2.     width:125px;   
  3.     height:125px;      
  4.     border:4px solid #222;     
  5.     margin:5px 5px 5px 0;   
  6.        
  7.     /* required to hide the image after resized */  
  8.     overflow:hidden;   
  9.        
  10.     /* for child absolute position */  
  11.     position:relative;   
  12.        
  13.     /* display div in line */  
  14.     float:left;   
  15. }   
  16.   
  17. .zitem .caption {   
  18.     width:125px;   
  19.     height:30px;   
  20.     background:#000;   
  21.     color:#fff;   
  22.            
  23.     /* fix it at the bottom */  
  24.     position:absolute;   
  25.     bottom:-1px/* fix IE issue */  
  26.     left:0;   
  27.   
  28.     /* hide it by default */  
  29.     display:none;   
  30.   
  31.     /* opacity setting */  
  32.     filter:alpha(opacity=70);    /* ie  */  
  33.     -moz-opacity:0.7;    /* old mozilla browser like netscape  */  
  34.     -khtml-opacity: 0.7;    /* for really really old safari */     
  35.     opacity: 0.7;    /* css standard, currently it works in most modern browsers like firefox,  */  
  36.   
  37. }   
  38.   
  39. .zitem .caption a {   
  40.     text-decoration:none;   
  41.     color:#fff;   
  42.     font-size:12px;    
  43.        
  44.     /* add spacing and make the whole row clickable*/  
  45.     padding:5px;   
  46.     display:block;   
  47. }   
  48.   
  49. img {   
  50.     border:0;   
  51.        
  52.     /* allow javascript moves the img position*/  
  53.     position:absolute;   
  54. }  

3. Javascript

This is a simple jQuery script with hover effect. What we have to do is calculate the width and height and set it to the image.

  1.        
  2. $(document).ready(function() {   
  3.   
  4.     //move the image in pixel   
  5.     var move = -15;   
  6.        
  7.     //zoom percentage, 1.2 =120%   
  8.     var zoom = 1.2;   
  9.   
  10.     //On mouse over those thumbnail   
  11.     $(´.zitem´).hover(function() {   
  12.            
  13.         //Set the width and height according to the zoom percentage   
  14.         width = $(´.zitem´).width() * zoom;   
  15.         height = $(´.zitem´).height() * zoom;   
  16.            
  17.         //Move and zoom the image   
  18.         $(this).find(´img´).stop(false,true).animate({´width´:width, ´height´:height, ´top´:move, ´left´:move}, {duration:200});   
  19.            
  20.         //Display the caption   
  21.         $(this).find(´div.caption´).stop(false,true).fadeIn(200);   
  22.     },   
  23.     function() {   
  24.         //Reset the image   
  25.         $(this).find(´img´).stop(false,true).animate({´width´:$(´.zitem´).width(), ´height´:$(´.zitem´).height(), ´top´:´0´´left´:´0´}, {duration:100});     
  26.   
  27.         //Hide the caption   
  28.         $(this).find(´div.caption´).stop(false,true).fadeOut(200);   
  29.     });   
  30.   
  31. });  

Tags: jquery, tutorial, zoom, image,


jQuery Thumbnail with Zooming Image and Fading Caption TutorialBookmark and Share

You write your thoughts

Please login to post comments here from the right corner.

your add
icon free icon icon set vector icon free catalog catalog template jquery news ticker widget tools tutorial php slider video youtube effects menu thumbnail forms facebook zoom image combo multiselect modal polaroid gallery expand code feedback contact navigation slider tagcloud Skateboard psd templates files badges subscribe panel login temlate file free office holland free template tabs prototype css scriptaculous opacity moo autocomplete wordpress themes design theme button images photoshop fonts font nav pdf logo style high javascript pretty photo jqery lightbox Themes Blog popular blog desing joomla xhtml brushes hair footer admin mootols slide show blackbox new download class first plugin upload browse Page effect modalbox web application urlitooltips sliding boxes captions flatchat chat ajax messaging browser hoverlightbox CSS Dev General Javascript PHP SQL search avatar vector Drupal Joomla Oscommerce javaScript phototype manager ajaxplorer flash xml rotator bookmarking pageflip slideshow poll text component script mediaplayer imagegallery pictograms car poster player mp3 radio onlineradio navigations drop-down collection brush tree textures paper pop-up bumpbox web2.0 iphone film frame twitter page fan mail delete dynamic calender form handbook city social paint timeline clock connect bar sharing accordion quotes ticker photo-effects background 3D media player graphic rss leaf green glossy bubble icon comic clean cool web 2.0 png actionscript ietter rotating Menu animated textured site masking pasword app layer sponge bob popup bubble tooltips horizontal prototip tooltip pack charachters comics blog action icon.psd flavour navi tango gradients istanbul mixed css3 sleek animations program html5 website Layer styles ultimate candy Media icons dropdown snow icon innerfade hover sub tag