tags:

views:

62

answers:

2
    <a id="imageurl" name="imageurl"><img src="Rotating1" id="Rotating1" border="0" alt="Rotating1" name="Rotating1" /></a>
<script language="javascript" type="text/javascript">

   function RotateImages(Start)
  {
      var a = new Array("1.jpg","2.jpg","3.jpg", "4.jpg");
      var c = new Array("url1", "url2", "url3", "url4");
      var b = document.getElementById('Rotating1');
      var d = document.getElementById('imageurl');
      if(Start>=a.length)
          Start=0;
      b.src = a[Start];
      d.href = c[Start];
      window.setTimeout("RotateImages(" + (Start+1) + ")",3000);
  }

  RotateImages(0);

  </script>
+1  A: 

The return value of setTimeout can be passed to cancelTimeout to stop the rotation. You might save it to a variable, then cancel the timeout on MouseIn and restart it on MouseOut.

Sparr
That is sounding good, but I am incapable of implementing it, do you have the time to post a code snippet or example? I really know nothing of javascript, this is borrowed code that I have only modified to be xhtml 1.0 transitional for a site I am hand coding.
Mike
+1  A: 

Change your code as Follows:

<a id="imageurl" name="imageurl"><img src="Rotating1" id="Rotating1" border="0" alt="Rotating1" name="Rotating1" onmouseover="StopRotation();" onmouseout="RotateImages();"/></a>
<script language="javascript" type="text/javascript">

  var currentImage = -1;
  function RotateImages()
  {
    var a = new Array("1.jpg","2.jpg","3.jpg", "4.jpg");
    var c = new Array("url1", "url2", "url3", "url4");
    var b = document.getElementById('Rotating1');
    var d = document.getElementById('imageurl');

    currentImage++;

    if(currentImage>= a.length)
      currentImage=0;

    b.src = a[currentImage];
    d.href = c[currentImage];

    rotator = window.setTimeout("RotateImages(" + (currentImage+1) + ")",3000);
  }

  function StopRotation()
  {
    window.clearTimeout(rotator);
  }

  RotateImages();

</script>
  • added Function "StopRotation"
  • added onmouseover and onmouseout-Events to the Image Tag
  • Rotation continues at the current Image.
Dratir
Wow, perfect! The only other thing I could ask at this point would be that the rotation continues instead of resetting to the first image. Otherwise this will be what I use on the site.
Mike
i changed the code...
Dratir
Thank you so much, you have helped me immensely, I set var currentImage = 0 to -1 instead so it starts on the first specified image in the array.
Mike
ah, yeah, i thougth about this aswell.Test it and dont't forget to accept the solution if it works! :-)
Dratir