Solved

OnMouseover display random image

Posted on 1997-07-09
1
289 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now