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.
------------------------------------------------------------
<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.