Javasript photo rotation fade-out

First, I should say that I don’t know Javascript. Someone has asked me to fix this rotating image feature.

The fade in works fine but there is no fade out—or at least it’s very abrupt. I would like the fade-out to be the same as the fade-in.

Does anyone know how I can adjust this Javascript file to fix that?

Any help would be much, much appreciated!

Thank you.

Here’s the link.

Like you, I don’t know JS well enough to fix this, but if all else fails, you could try out jQuery Cycle, which is a very reliable alternative to this. It is a bit heavier on code (as you have to link to the jQuery library—preferably on Google) but it works nicely.

Here is the Cycle home page.

I was trying to avoid redoing it but I might have to go that route if I don’t find a fix for it.


I think the problem is in the example script, which you have used. When you visit that example page (the ‘updated’ one), the first sequence of images are uncontrolled, after which they seem to come together and cross-fade OK. Anyway, I think I found the problems in the init function, and your adjusted page is below. If you are interested, significant changes are marked with double asterisks. A few minor/cosmetic changes were also made. I haven’t checked your CSS, but make sure all your slideshow images have position: absolute.

Those lines which toggle the display property between ‘block’ (for the active image pair) and ‘none’ (for any passive images) are not really required, so they are commented out. The script is using opacity to control what the viewer sees, so there seemed little point in also setting the images to display:none while they are passive, when the viewer can’t see them anyway as their opacities are zero.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="_css/reuscher.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#rotator {
  width: 1000px;
  margin: 0 auto;
  height: 332px;

<div id="rotator">
  <img src="_images/home/home-photo1.jpg" alt="" />
  <img src="_images/home/home-photo2.jpg" alt="" />
  <img src="_images/home/home-photo3.jpg" alt="" />

<script type="text/javascript">
Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
Rewrite of old code found here:
Updated here:


var d=document,
    imgs = new Array(),

// **This function needs to be global - called by both functions below
function setOpacity(obj) {
  if(obj.xOpacity>1.0) {
    obj.xOpacity = 1.0;
  } = obj.xOpacity/1.01; = 'alpha(opacity=' + (obj.xOpacity*100) + ')'; = obj.xOpacity;

function so_init(){
  if(!d.getElementById || !d.createElement) return;
  css = d.createElement('link');

  imgs = d.getElementById('rotator').getElementsByTagName('img');
  for(var i=1;i<imgs.length;i++) {
    imgs[i].xOpacity = 0;
    setOpacity(imgs[i]); 	// ** required addition
  // imgs[0].style.display = 'block';
  imgs[0].xOpacity = 1.0;
  setOpacity(imgs[0]);  	// ** required addition

function so_xfade() {
  cOpacity = imgs[current].xOpacity;
  nIndex = imgs[current+1]?current+1:0;
  nOpacity = imgs[nIndex].xOpacity;


  // imgs[nIndex].style.display = 'block';
  imgs[current].xOpacity = cOpacity;
  imgs[nIndex].xOpacity = nOpacity;


    // imgs[current].style.display = 'none';
    current = nIndex;
  else {

Without them, though, the images appear one atop another, so with overflow hidden on the container images 2 and 3 don’t show.

Hmmm … thanks for mentioning that … I didn’t look through the CSS file, but just mentioned (at end of my first paragraph) the need to absolutely position the images. But having just checked, the existing CSS does not have that, so it will need the following addition:

#rotator img { position: absolute; }

… so as to ensure all the images do sit atop one-another and can cross-fade. I did simplify the div styling for this example, but the cross-fading works perfectly.

This script does actually work by having all the images stacked one-atop-the-other. All but 2 of the images, at any one time, have opacities of zero, hence are invisible. The other 2 are the currently cross-fading images. That’s why I couldn’t see an additional need for the display toggling - the opacity is doing all the work. Anyway, I did check it in several browsers, and those 3 commented display-setting lines make no visible difference to the cross-fading, whether they are in or out.

Nice work, lnhicks. Very impressive. :slight_smile:

I love it. While I slumber you’ve solved all my problems. :slight_smile:

It works!

Thank you both.

If you are after a really small script for doing all sorts of fancy image transitions with javaScript then take a look at the one written by Brothercake at