Solved

OnMouseover display random image

Posted on 1997-07-09
1
300 Views
Last Modified: 2011-10-03
Hi there,

I have a small JAVA applet running at
http://www.infonie.fr/public_html/germann/Index.htm     (please check),
the mouseover effect is used to display gif files at random.
Is it possible to do the same thing with JAVASCRIPT ?

(Note: the Java applet on my page works fine with Netscape3 but doesn't
work with N4)

Thanks,

Juan
germann@infonie.fr
0
Comment
Question by:Jgermann
1 Comment
 
LVL 5

Accepted Solution

by:
Trevor013097 earned 50 total points
ID: 1268397
Yes it is possible to do exactly the same thing in Javascript.First you must create an array of images which are preloaded and then a random function must pick one of these array entries at random (obviously!!) and then a simple onmouseover with an image displays the results.Here is how to do it:<HEAD>
<TITLE>Random Music</TITLE>
</HEAD>

<SCRIPT LANGUAGE="Javascript">

image1 = new Image();
image1.src = "image1.gif";
image2 = new Image();
image2.src = "image2.gif";
image3 = new Image();
image3.src = "image3.gif";
image4 = new Image();
image4.src = "image4.gif";

var imageArray = new makeArray('image1', 'image2', 'image3', 'image4');


function makeArray(){
this.length = makeArray.arguments.length
for (var i = 0; i < this.length; i++)
this[i + 1] = makeArray.arguments[i]
}

function randNum (num) {
var now = new Date();
var rand = Math.round(num * Math.cos(now.getTime()));
if (rand < 0) rand = - rand; if (rand == 0) rand++;
return rand;
}

var imgsrc = imageArray[randNum(imageArray.length)]

</SCRIPT>
</HEAD>
<BODY>


<A HREF="Your-page.html"
      onmouseover = "document.random.src = imgsrc"
      onmouseout = "document.random.src = imgsrc"
>
<IMG SRC="image1.gif" NAME=random BORDER=0></A>

</BODY>

</HTML>Hope this helps
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question