Cool Javascript Slideshow with Crossfade

Version 1.2

By Jeff Baker
April 13, 2009
Subscribe to Internet Tips and Tools Feed

This is a test of Cool Javascript Slideshow with Crossfade. An easy way to do a slideshow without using flash. The other nice thing about it is you don't have to specify absolute coordinates for the top and left corner of the DIV's that hold the images. Instead the DIV can fall nicely into the document.

This technique uses two "absolute" positioned IMGs inside of one "relative" positioned DIV. That way the two images that are being used for the crossfade effect are positioned at the top left corner of the "relative" positioned DIV. Some other crossfade javascript scripts require that you use DIV's that are "absolute" positioned.

Features

  • Transitions: Crossfade, Zoom, Wipe and Slide.
  • Photo Effects: Zoomin, Zoomout and Pan.
  • Ability to retrieve photos from your Picasa Web Albums account.
  • Turn description display on or off (Captions).
  • Specify size of slideshow.
  • Tested to work in IE 6.0, Netscape 7.1, Safari 3.0, Firefox 1.5+

There are three editions of the Cool Javascript Slideshow. Click on an edition below or scroll down:

1. Cool Javascript Slideshow Regular Edition

2. Cool Javascript Slideshow Picasa Web Albums Edition

3. Cool Javascript Slideshow PHP Edition

1. Cool Javascript Slideshow Regular Edition

Download the script by right clicking on it and click on "Save As..." coolslide.js

Edit the javascript and change the variables inside such as:


// You may edit the variables below:
var folder = 'images/'; // if your photos are in a different folder specify it here
// example: var folder = 'images/'; 
// or same folder as js file then: var folder = '';
var phwidth = 512; // pictureholder width
var phheight = 384; // pictureholder height
// pictureholder DIV also has the class 'pictureholder' so you can add CSS border etc.
var photosize = 512; // width to show photos at
// Valid sizes are: 32, 48, 64, 72, 144, 160, 200, 288, 320, 400, 512, 576, 640, 720, 800
var seconds = 5; // switch photos n seconds
var randomize_photos = 0; // 0 = Do not randomize photos; 1 = Randomize photos
var display_caption = 1; // 0 = No caption; 1 = Display Picasa Description below photo
var caption_height = 50; // How many pixels high for caption box if on
var caption_border = 1; // Caption border pixels
// captionholder DIV has the class 'captionholder' so you can add CSS
var pan_zoom = 1.3;  // how many times to zoom before panning
var trans = "random"; // default transition between photos
var effect = "random"; // default effect on photos

The trans variable stands for transitions. The following trans are possible:
none, random, crossfade, zoom(from), wipe(from), slide(from)
from = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*slide does not support center)
(**wipe supports left, right, center, top and bottom)

The effect variable is for photo effects. Possible options are:
none, random, zoomin(to), zoomout(to), pan(to)
to = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*pan does not support center)

Copy and paste the following code in your HTML document where you want the slideshow to appear:

For the Photo list make an invisible textarea in the BODY of your HTML document like the one below with each jpg, gif or png listed on a separate line. Make sure the textarea has the id piclist:

or an easier way to make the photolist is to right click on the following batch file and save it to your hard drive in the folder that contains the image files you want in the photolist: photos.bat
It will create a file called photos.txt which will contain the textarea element along with all your images. Then paste the information from photos.txt into the body of your HTML document.

photos.bat contents looks like the following:

You can edit the batch file if you wish. The important line is:

dir /b /od *.jpg *.gif *png >> %filename%

It basically uses the dir command in basic listing mode /b and orders the list by date /od. You can change the sort method to order by name (alphabetic) by replacing /od with /on. See the dir command help for more options. (Click on Start -> Run. Type in cmd and press ENTER. Type in dir /? and press ENTER.)

You may also specify transitions, effects, and time in the TEXTAREA. If you specify these then they override the default settings above. The format is similar to a combination of HTML and Javascript. Just add the code after each image filename and that photo will be effected by the code. Separate the filename and the code with ##
Examples:

time is the amount of seconds to display a photo. Anything else you have in the description including HTML markups will display in the caption box if you have captions turned on.

2. Cool Javascript Slideshow Picasa Web Albums Edition

Download the script by right clicking on it and click on "Save As..." pwaslide.js

Edit the javascript and change the variables inside such as:


// You may edit the variables below:
var username = "yourpicasa"; // username for your picasa web album account
var album = "youralbum"; // name of album (Folder name)
var phwidth = 512; // pictureholder width
var phheight = 384; // pictureholder height
// pictureholder DIV also has the class 'pictureholder' so you can add CSS border etc.
var photosize = 512; // width to show photos at
// Valid sizes are: 32, 48, 64, 72, 144, 160, 200, 288, 320, 400, 512, 576, 640, 720, 800
var seconds = 5; // switch photos n seconds
var randomize_photos = 0; // 0 = Do not randomize photos; 1 = Randomize photos
var display_caption = 1; // 0 = No caption; 1 = Display Picasa Description below photo
var caption_height = 50; // How many pixels high for caption box if on
var caption_border = 1; // Caption border pixels
// captionholder DIV has the class 'captionholder' so you can add CSS
var pan_zoom = 1.3;  // how many times to zoom before panning
var trans = "random"; // default transition between photos
var effect = "random"; // default effect on photos

The trans variable stands for transitions. The following trans are possible:
none, random, crossfade, zoom(from), wipe(from), slide(from)
from = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*slide does not support center)
(**wipe supports left, right, center, top and bottom)

The effect variable is for photo effects. Possible options are:
none, random, zoomin(to), zoomout(to), pan(to)
to = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*pan does not support center)

Copy and paste the following code in your HTML document where you want the slideshow to appear:

You may also specify transitions, effects, and time in Picasa Web Albums descriptions. If you specify these then they override the default settings above. The format is similar to a combination of HTML and Javascript. Just add the code to a Photo description in Picasa and that photo will be effected by the code.
Examples:

<trans=crossfade> <effect=zoomin(center)> <time=5> The rest of this line becomes a description.

time is the amount of seconds to display a photo. Anything else you have in the description including HTML markups will display in the caption box if you have captions turned on.

3. Cool Javascript Slideshow PHP Edition

Features of the PHP Verison

  • Automatically and dynamically loads images for the slideshow from the folder you specify on the web server.
  • Dynamically resizes the photos to the size specified so the regular size photo does not have to be downloaded.
  • Can retrieve jpg exif photo comments and display them as captions. The comments can also include transition and photo effects.

Download the script by right clicking on it and click on "Save As..." phpslide.zip

Once phpslide.zip is on your local hard drive then decompress phpslide.php from it.

Edit phpslide.php and change the variables inside such as:


$folder = "images/"; // if your photos are in a different folder specify it here
// example: var folder = 'images/'; 
// or same folder as this file then: var folder = './';
$photosize = 512; // width to show photos at
$quality = 70; // image quality; 100 = highest quality and largest file size
// keep the quality at 70 or lower if you are getting image flicker because
// of loading of the image files between transitions
$get_exif = 1; // 0 = Do not load exif description data from jpg's; 1 = load exif descriptions from jpg's

$phwidth = 512; // pictureholder width
$phheight = 384; // pictureholder height
$phheight = 384; // pictureholder height
// pictureholder DIV also has the class 'pictureholder' so you can add CSS border etc.
$seconds = 5; // switch photos n seconds
$randomize_photos = 0; // 0 = Do not randomize photos; 1 = Randomize photos
$display_caption = 1; // 0 = No caption; 1 = Display efix jpg Description below photo
$caption_height = 50; // How many pixels high for caption box if on
$caption_border = 1; // Caption border pixels
// captionholder DIV has the class 'captionholder' so you can add CSS
$pan_zoom = 1.3;  // how many times to zoom before panning
$trans = "random"; // default transition between photos
$effect = "random"; // default effect on photos

The trans variable stands for transitions. The following trans are possible:
none, random, crossfade, zoom(from), wipe(from), slide(from)
from = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*slide does not support center)
(**wipe supports left, right, center, top and bottom)

The effect variable is for photo effects. Possible options are:
none, random, zoomin(to), zoomout(to), pan(to)
to = upperleft, top, upperright, left, center, right, lowerleft, bottom, lowerright
(*pan does not support center)

Copy and paste the following code in your HTML document where you want the slideshow to appear:

You may also specify transitions, effects, and time in jpg exif photo comments. If you specify these then they override the default settings above. The format is similar to a combination of HTML and Javascript. Just add the code to a Photo description in a JPG exif comment and that photo will be effected by the code.
Examples:

<trans=crossfade> <effect=zoomin(center)> <time=5> The rest of this line becomes a description.

time is the amount of seconds to display a photo. Anything else you have in the description including HTML markups will display in the caption box if you have captions turned on.

To Edit JPG exif Comments

You have a few choices for editing exif jpg photo comments. You can download programs that can edit the comments such as irfanview. In Irfanview open the jpg photo and then click on Image -> Information -> Comment. Then write a comment in the window and click on Write.

You may also use Windows XP to edit a jpg comment. In your images folder right click on the jpg filename and click on Properties. Click on the Summary tab. If it says << Simple click on that so that it says Advanced >>. There you will find a comments field. Click in it and write your comment, including any photo transitions or effects you want. Then click OK.

*Note: Windows XP and Irfanview write jpg exif comments in different places on the jpg file. If you write a regular exif comment with Irfanview and also a Windows XP JPG comment then the Windows XP comment will be displayed only. However, if you leave the Windows XP comment blank then the regular exif comment from Irfanview or other software will be displayed.

Bug Fixes and versions

5/30/09 ver. 1.2. Converted all html tags to lowercase for better compatibility with doctype strict. Added px to end of width, height, top and left styles for compatibility with xhtml.

4/20/09 ver. 1.12c for PHP Edition. Fixed bug where if there is quotes in exif comment or exif title then we need to slash them. Addslashes.

4/14/09 ver. 1.12b for PHP Edition. Had forgot to put width and height for image in imagecreatetruecolor if the set width was bigger than actual image width.

4/13/09 ver. 1.12 Now allows random to be passed as trans or effect in photo description.

4/13/09 ver. 1.11 Fixed possible security problem where the script allowed any function to be passed as trans or effect from a photo description. But now the script checks to make sure it is a real trans, effect and direction.

4/12/09 ver. 1.1 Fixed bug where effect = or trans = with spaces did not work. Now it removes the spaces (Ex: effect=zoomin(center))

4/08/09 ver 1.0 Cool Javascript Slideshow created

Back to www.seabreezecomputers.com
Subscribe to Internet Tips and Tools Feed

View Comments

User Comments

There are 20 comments.

Displaying first 25 comments.

1. Posted By: WayNerd1 - wgrebe@handcrafthomes.net - April 16, 2009, 11:07 am
I tried your coolslide program. I'm having trouble with it though. I am running it in test mode and it shows the "LOADING" progress bar and then the var details of each slide, but the slide itself does not display. Any thoughts?
Thanks!




Slideshow_04.jpg
Slideshow_05.jpg
Slideshow_06.jpg
Slideshow_07.jpg
Slideshow_08.jpg
Slideshow_09.jpg
Slideshow_10.jpg
Slideshow_11.jpg
Slideshow_12.jpg
Slideshow_13.jpg
Slideshow_14.jpg
Slideshow_15.jpg


2. Posted By: bobbel - - May 18, 2009, 3:34 am
Hi,
I downloaded and run your php-version but the pan effect does not work. The picture should move from right to left with pan(left), shouldn't it?
Regards,
bobbel

3. Posted By: shirl - - May 19, 2009, 8:23 pm
hi, i also have no luck in getting the javascript to work

just the loading line then nothing..
please help


4. Posted By: Jeff - - May 27, 2009, 5:25 pm
I would need to see examples of you web pages in order to diagnose the problems. One person did send me an example and the problem was that they put coolslide.js in a js folder and called it from there. This will not work because then coolslide.js will think that the images are in the js folder or a subfolder of js. You should keep coolslide.js in your main folder along with the HTML document that calls it.

5. Posted By: jb - - June 26, 2009, 6:04 am
works great thanks!

question, if i wanted to modify the acceptable photo sizes how would I do that?

thanks,
jb

6. Posted By: Jeff - - June 30, 2009, 6:36 pm
Hi Jb. Open coolslide.js or pwaslide.js (depending on which edition you are using and edit the line that looks like:

var photosize = 512; // width to show photos at

Jeff

7. Posted By: whisperinghill - - July 16, 2009, 8:50 am
I just tried the PHP version and love it. Though I tried adding two (2) slide shows to a single page one called chislide.php and the other called yorkslide.php and added the java script with the correct .php. But it only wants to display one slide show, interesting.

Also, is there a way to add a hyperlink to the slide show?

8. Posted By: TJ - - July 19, 2009, 11:25 am
This code works great, except that every 3 or 6th image is seen as invisible. I'm trying to have a simple slideshow play images.. no effects, just transitions. I commented out the loading bar section as it was getting hung up on it.

Can you help?

This is the link:
regenhouse.com/index.html

and your altered code is here:
regenhouse.com/coolslide.js

Thanks!



9. Posted By: Jeff - - July 22, 2009, 12:56 pm
Hi whisperinghill,

Yes, I doubt that two slideshows would work because the function names would be the same, so it wouldn't know what it was dealing with. Do you mean a link by clicking on the slideshow or in a comment for a photo?

I tried putting an anchor tag around the script like this:

<A href="www.google.com">

</A>

But all that did was make the caption box into a link that went to google.com. So you would have to edit the script and you could put a onClick="location.href=' statement for the pictureholder div. The line to change in the phpslide.php version would be line 1325 or line 1225 in the coolslide.js. It currently looks like:

document.write('<div id="pictureholder" class="pictureholder"'+

Change to:

document.write('<div onClick="location.href=\'http://www.google.com\'" id="pictureholder" class="pictureholder"'+

Jeff

10. Posted By: Jeff - - July 22, 2009, 1:05 pm
Hi TJ,

I went to the website but I did not see any slideshow. I took a look at the coolslide.js file you edited and there is one line that you should change:

var trans_amount = 1000; // amount of steps for transitions

1000 is way to large for the amount of steps for transitions. Especially if you are switching photos every 5 seconds. I would put the transition steps down to 20.

Jeff
http://www.seabreezecomputers.com

11. Posted By: whisperinghill - - August 16, 2009, 11:49 am
Evertime I tried to allow the exif comments to be placed in the caption block I get a error.

If I place "$get_exif = 0" then no error, but also no captions in the block

These are obviously key to the comments being displayed.

$get_exif = 1; // 0 = Do not load exif description data from jpg's; 1 = load exif descriptions from jpg's

$display_caption = 1; // 0 = No caption; 1 = Display efix jpg Description below photo


What am I messing up?

Thanks
Bud

12. Posted By: Jeff - - August 16, 2009, 4:06 pm
Hi Bud. Sorry about that. Your PHP was probably not compiled with Exif enabled. Please see the information at:
www.seabreezecomputers.com/gallery/?cat=5&item=2

Jeff
www.seabreezecomputers.com

13. Posted By: Chihiro Uchida - - September 4, 2009, 11:12 am
Hi,

Thank for the nice script!

Although, it is working not for me.

Oh, it works in the Firefox, but only some problem in Internet Explorer.

OK, here is report:

I use small pictures, 300px X 150px.

And I use "Crossfade" and no effects.

And "Quality" is 70

Everytime, new picture appear with crossfade is fine.

But there is a flashing of old picture for one ms.

I try change "Flicker" value from 500, 600 to 1000 and even 3000.

It is the same always, there is flash of old picture when new picture appear.

This only happen in Internet Explorer, in the Firefox it work great.

Please help.

Thank!

14. Posted By: Jeff - - September 5, 2009, 11:13 pm
Hi Chihiro Uchida. Sorry I can't help you with the flickering problem without a link to the site where you have the script running.

15. Posted By: Michael - - December 20, 2009, 10:04 pm
Hi, Jeff,
Just want to say thanks for the script.

Michael


16. Posted By: Fern - - December 27, 2009, 8:47 pm
HI Jeff,

I used your script for PWA content on my site and it works great on regular browsers. On the iphone, it seem like it gets stuck on the first transition and it just loops that one. Feel free to look if you wish: http://www.traceyandfernando.com

thanks!
Fern

17. Posted By: Alex - - January 28, 2010, 7:57 pm
It loads a little slow eventhough I am using the php version but it could just be the size and resolution of my >40 images.

But I want to tell you thanks and amazing work. People like you make the web it is.

18. Posted By: Jeff - - February 7, 2010, 12:50 pm
Hi Fern,

The slideshow script does usually work on the iphone. I'm kind of grasping at straws here, cause I looked at your code and it seems like it should work. But there are couple of things you could try:

1. In your HTML file on line 11 you have . But then you never close the div, instead on line 18 you close a center tag, but the center tag was never created. So you might try changing line 11 to just or you might try changing line 18 to

2. I also noticed that the album you are loading must not be public, because it doesn't show up under your fcorrado account on PWA. And you are using an authkey to access it in the slideshow script. I would recommend trying to load a different album that is set to public and see if iPhone can display it.

Jeff
www.seabreezecomputers.com

19. Posted By: Jeff - - February 7, 2010, 12:53 pm
Hi Alex,

I'm glad you found it useful. I am sorry it is loading slow. As you say, it may be the size of the images. For the web it is usually good practice to resize images to 800x600 at the very largest. Some slideshows only display images at 640x480.

The script loads 5 images in the background before displaying. I found this to be the best strategy for displaying the images for about 5 seconds. However, if you are displaying the images for a longer period of time you might be able to change the amount of images it loads before displaying them. You can easily do this in a function called init_show()

If you are using the php version on line 317 of the script you will see:

for (i = 0; i < pictures.length && i < 5; i++)

Change the 5 to another number like 3 if you would like to only cache about 3 images before it displays the first one.

Jeff
www.seabreezecomputers.com

20. Posted By: thom - - March 7, 2010, 7:53 pm
hi. I've tried to use the PHP solution and am having difficulties.
www.bayvalleycc.org/indextest2.html called phpslide.php exactly as you stated to but the three pictures in my directory do not display.
If you could review and let me know what's going on, I'd really appreciate it. I've tried a few php solutions and none seem to work :( php does work on the server as i've been able to send back text with a test php script.