Compact JavaScript image rotator

by Steve W 18. March 2010 12:53

In setting up our video training and landing page, we needed to come up with a slick little photo / image rotator without resorting to AJAX. Javascript is great in that it offers cross browser functionality and is heavily supported. It didn’t take long to find some direction and get us on the path of coding our own little compact image rotator.

 

First, add the following to your ASP page somewhere in the <BODY> tag:

   1: <script type="text/javascript">
   2:     // =======================================
   3:     // set the following variables
   4:     // =======================================
   5:     // Set slideShowSpeed (milliseconds)
   6:     var slideShowSpeed = 10000; // 10 seconds per slide
   7:     // Duration of crossfade (seconds)
   8:     var crossFadeDuration = 3;
   9:     // Specify the image files
  10:     var Pic = new Array(
  11:         'Images/vidshots/vidbar1.gif',
  12:         'Images/vidshots/vidbar2.gif',
  13:         'Images/vidshots/vidbar3.gif',
  14:         'Images/vidshots/vidbar4.gif'
  15:     );
  16:     // to add more images, just continue
  17:     // the pattern
  18:     // =======================================
  19:     // do not edit anything below this line
  20:     // =======================================
  21:     var t;
  22:     var p = Pic.length;
  23:     var preLoad = new Array();
  24:     for (i = 0; i < p; i++) {
  25:         preLoad[i] = new Image();
  26:         preLoad[i].src = Pic[i];
  27:     }
  28:     var j = 0;
  29:     function runSlideShow() {
  30:         if (document.all) {
  31:             document.images.SlideShow.style.filter = "blendTrans(duration=2)";
  32:             document.images.SlideShow.style.filter = "blendTrans(duration=crossFadeDuration)";
  33:             document.images.SlideShow.filters.blendTrans.Apply();
  34:         }
  35:         if (j > (p - 1)) j = 0;
  36:         document.images.SlideShow.src = preLoad[j].src;
  37:         if (document.all) {
  38:             document.images.SlideShow.filters.blendTrans.Play();
  39:         }
  40:         j = j + 1;
  41:         if (j > (p - 1)) j = 0;
  42:         t = setTimeout('runSlideShow()', slideShowSpeed);
  43:     }
  44: </script>

There is a section of code that defines the Pic array. In this example, we have 4 images in our Images/vidshots directory. You can add or subtract as you see fit.

 

Now you need to find the spot on your page where you want the image rotation to occur. You can set this in a DIV, Panel, or a table. For this example, we will use a table.

   1: <table border="0" cellpadding="0" cellspacing="0">
   2:     <tr>
   3:     <td id="VU" height="83" width="300">
   4:     <img src="1.jpg" name="SlideShow" width=300 height=83></td>
   5:     </tr>
   6: </table>
Important: Do NOT change the name of the image tag “SlideShow,” since the Javascript code we put in earlier looks for this value. Adjust the height and width variables to your image sizes. The source of the default image, “1.jpg”, can simply be a temporary graphic that shows while the real first image is being downloaded. This happens so fast with smaller graphics that you could set it to a bogus value and the user will never see the annoying ‘X’ (image not found).

 

Next, modify your <BODY> tag to include an onLoad override:

   1: <body onload="runSlideShow()">

A couple of things to keep in mind… The fading variables control the length of time it takes a slide to fade out and the next one to fade in. This only works (currently) in IE8 and not Firefox. Firefox simply flips to the next image in the array without any type of fade.


Comments (1) -

design courses
design courses Australia
9/3/2010 11:20:28 PM #

Actually, there is a section of code that defines the Pic array. In this example, we have 4 images in our Images/vidshots directory.

Reply

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading



Recent Comments

Comment RSS

Month List

Who's Watchin' Me?


Over 10,000 costumes and accessories from HalloweenMart




BooksFREE




Decade Awards: Personalized Photoballs


Krill D3 Antioxidant Formula