Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-29-2012, 03:28 AM   PM User | #1
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Simple JQuery fadein/fadeout BUT with captions

I found so many simple JQuery galleries where you click a thumb
and the <div> above them gets loaded with the full-size image.
There is a fade-out, fade-in effect and it works fine.

But here's the problem ....
Under the full-size image, a caption needs to appear. The caption
is a full sentence that describes the photos.

I can't find anything anywhere that does the fade-out fade-in AND
changes the caption as well.

Here is the site I'm working on ... http://www.salliefindlay.com/2d.php

This one has no fade-in fade-out effect, because if I do that, the
caption won't change. I'm just calling a function to change the
img src and to change the inner.HTML of another div (for the caption).

Does anyone know where there is an example of what I'm doing ...
exactly like that, but adding the fade-out fade-in effect?

This is driving me bananas. I don't want LightBox, or PrettyPhoto,
just a simple script. Thanks.
mlseim is offline   Reply With Quote
Old 09-29-2012, 11:51 AM   PM User | #2
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
I've done exactly that on my wife's site here:

http://www.angelabarrow.co.uk/gallery.php

with this script:

Code:
$(document).ready(function(){ 

$("#gallery_thumbnails img").click(function() {
		//read the new values for the image and the title
		newTitle = $(this).attr("alt");									
		newImage = $(this).attr("src").replace("/thumbnails","");
		
		if (newImage != $(".topImage").attr("src")){//don't fade in and out if it's the same image - webkit gets upset
		
			//
			$('.topImage').after('<img class="newImage" width="334" height="500" src="'+newImage+'" alt="'+newTitle+'" title="'+newTitle+'"/>');
		
			//fade the top image out, replace and fade the new one in
			$(".topImage").fadeOut(700,function() {
				$(".topImage").remove();
				$(".newImage").removeClass('newImage').addClass('topImage');
			}) ;
			
			//fade the caption out, replace and fade the new one in
			$("#gallerycaption").fadeOut('fast',function() {
			$("#gallerycaption").html(newTitle);
			$("#gallerycaption").fadeIn('fast');
			});
			return false;
		}
})

});
I'm holding the caption as the alt tag on the thumbnail.
The script reads the alt tag, finds the new image from the thumbnail src, inserts it behind the original image and then fades the original image out to give a crossfade effect, then fades the caption out, changes it, and fades it back in again.

It's a couple of years since I wrote this and it could probably be tidied up a bit now, but it does what you want, I think.
SB65 is offline   Reply With Quote
Old 09-29-2012, 06:03 PM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Which of the four .js files are required for that part of your script?

<script type="text/javascript" src="/javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/javascript/switchGalleryImage.js"></script>
<script type="text/javascript" src="http://simonbattersby.com/javascript/jquery.easing.1.3.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js" type="text/javascript"></script>

So far, I've included:

<script type="text/javascript" src="/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/js/switchGalleryImage.js"></script>

Not sure if I still need to have the other two. And 1.8.2 is the newer version 1.4.2


.

Last edited by mlseim; 09-29-2012 at 06:07 PM..
mlseim is offline   Reply With Quote
Old 09-29-2012, 07:50 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Look whether it’s working. If it is you know that you don’t need the other scripts.

However, by the order of inclusion I can tell you that switchGalleryImage.js is most likely not depending on the scripts that come after it. And also, I see nothing in that script that would indicate that jQuery UI or the easing plugin is used.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-29-2012, 11:13 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
SB65 and Stephan ...

Thanks.
I got it working OK. (but not perfect).
I'm mean OK because I haven't figured-out how to 'fadein' after an 'addClass'.

Here's my test page:
http://www.salliefindlay.com/6d.php

It now fades out OK, and then pops-in fast. Which is fine, but I wish I could
control the fadein.

But, thanks for letting me see your function. I modified it for my purposes, but
it got me on the right path.

Stephen, as you know ... PHP is my forte, not javascripting [sigh]



.

Last edited by mlseim; 09-29-2012 at 11:15 PM..
mlseim is offline   Reply With Quote
Old 09-30-2012, 09:57 AM   PM User | #6
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
To confirm, you only need jQuery and switchGalleryImage for the fade effect. The easing and UI are used for other purposes on that page. Should have made that clearer.

My code crossfades the images into each other - looks like you want to fade out the old and then fade in the new. Have a try making your code:

Code:
$(document).ready(function(){ 

$("#gallery_thumbnails img").click(function() {
		//read the new values for the image and the title
		newTitle = $(this).attr("alt");
		newCaption = $(this).attr("caption");		
		newImage = $(this).attr("src").replace("T.jpg","P.jpg");
		
		if (newImage != $(".topImage").attr("src")){//don't fade in and out if it's the same image - webkit gets upset
		
			//
			//$('.topImage').after('<img class="newImage" src="'+newImage+'" alt="'+newTitle+'" title="'+newTitle+'" />');
		
			//fade the top image out, replace and fade the new one in
			$(".topImage").fadeOut(500,function() {
				$('.topImage').after('<img class="newImage" src="'+newImage+'" alt="'+newTitle+'" title="'+newTitle+'" />');
				$(".topImage").remove();
				$(".newImage").removeClass('newImage').stop(true).addClass('topImage').hide();
			    $(".topImage").fadeIn('slow');
			}) ;
			

			
			//fade the caption out, replace and fade the new one in
			$("#gallerycaption").fadeOut('fast',function() {
			$("#gallerycaption").html(newCaption);
			
			$("#gallerycaption").fadeIn('fast');
			});
			return false;
		}
})

});
This seems to work OK - see test page here - although it also helps if the large images are preloaded - as otherwise you can see them loading instead of the fade in. I did this on hat page by including this javascript:

Code:
$(document).ready(function () {
    //preload the images
	var images = [<?php echo $allimagestring?>];
	var imagearray = [];
	$(images).each(function(key, value) {
	
		var img = new Image();
		imagearray[key] = $(img).attr('src', value);
	
	
	});
})
where $allimagestring is just a list of all the (large) images.
SB65 is offline   Reply With Quote
Users who have thanked SB65 for this post:
mlseim (09-30-2012)
Old 09-30-2012, 05:42 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Oh my ... that is so nice. Thank you.

I just need to generate the list into the string (that will be easy).
The fade effects look nice. You saved me a lot of time, and I appreciate it.


EDIT:

The site owner originally had made Full and Thumbnail filenames the same
except for T.jpg and P.jpg ... but now, as I suspected, they mixed up filenames.
So I had to figure out a way to know what the full-size filename was.

I ended-up creating my own attributes for the thumbnail <img> tag.
It works perfectly, but as anyone might comment, it's no longer W3C validated.
My made-up attributes are not XHTML nor HTML compliant.
But I don't care, because the doctype is: <!doctype html>

Having worked on this with my desktop PC (20 inch monitor), I was a bit
worried about rendering on iPad. But it renders nice. The layout is fluid,
and the images shrink to fit. I have a few <div>s that don't collapse,
but it is what it is. Such a plain site, but I wore myself out on the
fade effects. SB65 provided the clues I needed.

Here's the final rendering with the fade effects in-place:
http://www.salliefindlay.com



.

Last edited by mlseim; 09-30-2012 at 07:11 PM..
mlseim is offline   Reply With Quote
Old 09-30-2012, 08:06 PM   PM User | #8
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Glad it helped.

If you make your attributes data-photo instead of photo (for example) then they will validate as HTML5. HTML5 allows custom attributes provided they are in the format data-anynameyouwant.
SB65 is offline   Reply With Quote
Old 10-01-2012, 02:52 AM   PM User | #9
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
SB-65 ...

Thanks again! I learn new things everyday.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:48 AM.


Advertisement
Log in to turn off these ads.