Image Replacement Gallery
For both personal and work-related projects I have, time and time again, needed a simple image replacement script. Of course when starting out we first look for examples and tutorials from books and other websites. This is when I stumbled across the ‘Image replacement gallery’ snippet from Web Designer Wall.
After implementing their code, I still felt it was lacking slightly. Therefore I decided to develop the code further to give a cleaner finish and put jQuery’s animation to good use.
I felt the code should, once a thumbnail is clicked:
- Fade out the existing enlarged image
- Change the
srcattribute of the enlarged image - Amend the
altandtitlevalues of the enlarged image (based on thealtof the thumbnail) - Remove
class="current"from all thumbnails and add to the click thumbnail - Fade in the new enlarged image
For added effect, in the demo I have including a loading graphic which is displayed when the enlarged image is faded out. This is achieved by adding a background image to the enlarged image’s div
This may seem like just another Image Replacement Gallery script, but it can act as a great building block for more complicated galleries and once that I use often on my own projects.
View the finished code below or try out the demo.
The Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | $(function() { // When a thumbnail is clicked $(".gallery .thumbnails a").click(function() { // Get enlarged image attributes var largeImg = $(".gallery .feature img"); var largeImgPath = $(this).attr("href"); var largeImgTitle = $(this).children("img").attr("alt"); var largeThumbnail = $(this); // Fade out enlarged image $(largeImg).fadeOut('slow', function() { // Change the attributes of the enlarged large image $(largeImg).attr({ src: largeImgPath, alt: largeImgTitle, title: largeImgTitle }).load(function() { // Once image is loaded: // Remove class="current" from all thumbnails and add to clicked thumbnail $(".gallery .thumbnails").children().removeClass("current"); $(largeThumbnail).addClass("current"); // Fade in enlarged image $(largeImg).fadeIn('slow'); }); }); return false; }); }); |
Mark Boas
I was looking for something like this to use on this site : http://paolobenvenuti.com
Thanks!
adrian
WOW,
I am a newcomer to js and jquery and I too had stumbled across the Web Designer Wall example while looking for a portfolio gallery for my site.
I have modified the script to include a hover on the thumbnails and was starting to look at some way of fading in and out the large images.-
$(document).ready(function(){
$(“h5″).append(‘‘)
$(function() {
$(‘#navrollover img’).animate({“opacity”: .7 });
$(‘#navrollover img’).hover(function() {
$(this).stop().animate({ “opacity”: 1 });
}, function() {
$(this).stop().animate({ “opacity”: .7 });
});
});
$(“.thumbs a”).click(function(){
var largePath = $(this).attr(“href”);
var largeAlt = $(this).attr(“title”);
$(“#largeImg”).attr({ src: largePath, alt: largeAlt });
$(“h5 em”).html(” ” + largeAlt + “”); return false;
});
});
I was also using the variable alt data to provide a description of each large image.
I currently have a working example at – http://rosa.actewagl.net.au/graphic_design_c1_p1C.html
Is it possible to add the thumb hover event and variable alt data function to your new gallery?? Because it is much better!
Ben
Hi Adrian,
Thanks for the comment! I have e-mailed you in regards to updating the code with the requested additional features.
greg
Hi Ben
Love the tutorial! Quick question, how hard would it be to expand this example to automate the switching of the images, say every 5 seconds? I’m struggling a little bit with making this work in my working example.
Roman
Hallo für alle!
Mir hat es sehr gefallen, ich werde wahrscheinlich genau eure Version von Img Replacement Gallery installieren. macht weiter so
lucien
hello,
nice script !! very usefull, thanks a lot.
just a question, i want to display the caption (image description located on the ALT attribute) on a div#caption p
How to manage that ?
Can you help me to do that ? Would be really nice