JoshR
07-08-2003, 03:33 PM
Hi everyone.
I have four thumbnails and a text link. And a bunch of thumbnail images in an array. when you rollover the text link, i want a randomly chosen thumbnail to display a randomly chosen image.
I cant figure out how to make it work on a random image.
i know thats confusing, but if you look at what I have already you should see what i mean:
___________
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>random rollover </title>
<script language="JavaScript"><!--
roller= new Array()
roller[1]= new Image(100,100)
roller[1].src ='../images/thumb_1.jpg'
roller[2]= new Image(100,100)
roller[2].src ='../images/thumb_2.jpg'
roller[3]= new Image(100,100)
roller[3].src ='../images/thumb_3.jpg'
roller[4]= new Image(100,100)
roller[4].src ='../images/thumb_4.jpg'
showthis=0 //prepares the repeat-preventing variable
function RandomSwapOut() {
//the next three lines help prevent repeating images
oldroller = new Array()
oldroller[2] = oldroller[1]
oldroller[1] = showthis
//the next line chooses the number to use in selecting the image
showthis = parseInt((Math.random()*(roller.length-1))+1);
//the next two lines help prevent repeating images
if (showthis == oldroller[1]) {RandomSwapOut();}
if (showthis == oldroller[2]) {RandomSwapOut();}
//the next line replaces the image with the new, random image
document.thumb1.src = roller[showthis].src; return true;
}
function SwapBack() {
document.thumb1.src = "../images/brain_thumb.gif"; return true;
}
// -->
</script>
</head>
<body>
<div>
<p><img src="../images/brain_thumb.gif" name="thumb1" width="100" height="100" border="0" id="thumb1"></a> </a>
<img src="../images/thumb.gif" name="thumb2" width="100" height="100" border="0" id="thumb2"><img src="../images/thumb.gif" name="thumb3" width="100" height="100" border="0" id="thumb3"></a>
<img src="../images/thumb.gif" name="thumb4" width="100" height="100" border="0" id="thumb4"></a>
</p>
</div>
<p>_</p>
<p>_</p>
<p>_</p>
<p>_</p>
<p><a href="www.wherever.com" onmouseover="RandomSwapOut()" onmouseout="SwapBack()">click </a></p>
</body>
</html>
Basically, where it says: document.thumb1.src = roller[showthis].src; return true;
}
I want "thumb1" to be thumbX where X is an integer randomly chosen between one and sixteen (although in this example theres only four thumbs...).
Can anyone help? I'm really new to JS.
Thanks!
I have four thumbnails and a text link. And a bunch of thumbnail images in an array. when you rollover the text link, i want a randomly chosen thumbnail to display a randomly chosen image.
I cant figure out how to make it work on a random image.
i know thats confusing, but if you look at what I have already you should see what i mean:
___________
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>random rollover </title>
<script language="JavaScript"><!--
roller= new Array()
roller[1]= new Image(100,100)
roller[1].src ='../images/thumb_1.jpg'
roller[2]= new Image(100,100)
roller[2].src ='../images/thumb_2.jpg'
roller[3]= new Image(100,100)
roller[3].src ='../images/thumb_3.jpg'
roller[4]= new Image(100,100)
roller[4].src ='../images/thumb_4.jpg'
showthis=0 //prepares the repeat-preventing variable
function RandomSwapOut() {
//the next three lines help prevent repeating images
oldroller = new Array()
oldroller[2] = oldroller[1]
oldroller[1] = showthis
//the next line chooses the number to use in selecting the image
showthis = parseInt((Math.random()*(roller.length-1))+1);
//the next two lines help prevent repeating images
if (showthis == oldroller[1]) {RandomSwapOut();}
if (showthis == oldroller[2]) {RandomSwapOut();}
//the next line replaces the image with the new, random image
document.thumb1.src = roller[showthis].src; return true;
}
function SwapBack() {
document.thumb1.src = "../images/brain_thumb.gif"; return true;
}
// -->
</script>
</head>
<body>
<div>
<p><img src="../images/brain_thumb.gif" name="thumb1" width="100" height="100" border="0" id="thumb1"></a> </a>
<img src="../images/thumb.gif" name="thumb2" width="100" height="100" border="0" id="thumb2"><img src="../images/thumb.gif" name="thumb3" width="100" height="100" border="0" id="thumb3"></a>
<img src="../images/thumb.gif" name="thumb4" width="100" height="100" border="0" id="thumb4"></a>
</p>
</div>
<p>_</p>
<p>_</p>
<p>_</p>
<p>_</p>
<p><a href="www.wherever.com" onmouseover="RandomSwapOut()" onmouseout="SwapBack()">click </a></p>
</body>
</html>
Basically, where it says: document.thumb1.src = roller[showthis].src; return true;
}
I want "thumb1" to be thumbX where X is an integer randomly chosen between one and sixteen (although in this example theres only four thumbs...).
Can anyone help? I'm really new to JS.
Thanks!