Click to See Complete Forum and Search --> : Trying to make a Random Image Slide Show


MorteKing
11-07-2003, 03:58 AM
I know practically nothing about scripting of any kind. I have managed to get a random image script to work, and a slide show script. I would like a slide show that shows images Randomly. So far this is what I have come up with.
------------------------------------------------------------
<script language="JavaScript">
function switchImage(imgName, imgSrc)
{
if (document.images)
{
if (imgSrc != "none")
{
document.images[imgName].src = imgSrc;
}
}
}
// * Dependencies *
// this function requires the following snippets:
// JavaScript/images/switchImage
//
// BODY Example:
// <body onLoad="mySlideShow1.play(); mySlideShow2.play();">
// <img src="originalImage1.gif" name="slide1">
// <img src="originalImage2.gif" name="slide2">
//
// SCRIPT Example:
// var mySlideList1 = ['image1.gif', 'image2.gif', 'image3.gif'];
// var mySlideShow1 = new SlideShow(mySlideList1, 'slide1', 3000, "mySlideShow1");
// var mySlideList2 = ['image4.gif', 'image5.gif', 'image6.gif'];
// var mySlideShow2 = new SlideShow(mySlideList2, 'slide2', 1000, "mySlideShow2");
function SlideShow(slideList, image, speed, name)
{
this.slideList = slideList;
this.image = image;
this.speed = speed;
this.name = name;
this.current = 0;
this.timer = 0;
}
SlideShow.prototype.play = SlideShow_play;
function SlideShow_play()
{
with(this)
{
if(current++ == slideList.length-1) current = 0;
switchImage(image, slideList[current]);
clearTimeout(timer);
timer = setTimeout(name+'.play()', speed);
}
}
var mySlideList1 = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg', 'image9.jpg', 'image10.jpg'];
var mySlideShow1 = new SlideShow(mySlideList1, 'slide1', 5000, "mySlideShow1");
// Example:
// randomImage(['0.gif',50,50,'1.gif',25,25,'2.gif',50,25]);
// * Dependencies *
// this function requires the following snippet:
// JavaScript/Randomizers/randomNumber
function randomImage(imgArr)
{
var imgSrc, imgW, imgH, r;
r = randomNumber(imgArr.length / 3)-1;

imgSrc = imgArr[r * 3];
imgW = imgArr[(r * 3)+1];
imgH = imgArr[(r * 3)+2];

document.write('<IMG SRC='+imgSrc+' WIDTH='+imgW+' HEIGHT='+imgH+'>');
}
function randomNumber(limit){
return Math.floor(Math.random()*limit);
}

</script>

<body onLoad="mySlideShow1.play(); randomImage(['image1.jpg',800,600,'image2.jpg',800,200,'image3.jpg',800,600,'image4.jpg',800,600,'image5.jpg',800 ,600,'image6.jpg',800,600,'image7.jpg',800,600,'image8.jpg',800,600,'image9.jpg',800,600,'image10.jp g',800,600]);">
<img src="image1.jpg" name="slide1">

------------------------------------------------------------
Only the Random Image part works. If I take out the randomNumber function, the Slide Show works. Is it possible to make something like this work? For someone that does not know anything about scripting? It took me a couple days, and a lot of help from a friend to figure out how to make the Slide Show work. It took me most of a day to figure out the randomImage. Most of my problems have been with typos, but I usually have no idea where to put the things in the examples.

Vladdy
11-07-2003, 06:15 AM
Array.prototype.swap = function(index1,index2)
{ var temp = this[index1];
this[index1] = this[index2];
this[index2] = temp;
return;
}

Array.prototype.shuffle = function()
{ for(var i=0; i<this.length; i++)
{ ind1 = Math.floor(Math.random()*this.length);
ind2 = Math.floor(Math.random()*this.length);
this.swap(ind1,ind2);
}
return;
}

MorteKing
11-07-2003, 02:03 PM
Thank you very much for the scripts. I really do appreciate your help.

I am trying to figure it out. I have tried inserting the code into the very beginning of the script. I have tried it right above the last two vars. So far nothing has worked.

I am trying to find something on the net that might give me some example of how these are to be used. I do need to ask, at least I feel I do, a question.

Am I supposed to just add this in, or is this to replace the random scripts, or the whole script? I searched quite a bit before posting on this forum about what I was doing and was not able to find much of anything. I am going to search some. I prefer to figure things out on my own, but this stuff is so foreign to me.

MorteKing
11-09-2003, 10:26 PM
Ok. I have spent a couple of days trying everything I could think of. I do not know much of anything about scripting. I have no idea where to place these codes, whether to use them with the random image and random number scripts, how I am supposed to call the function to make it work. I am just pretty much lost. I am sorry, but I just need a little more help.