Jump to content

JavaScript:Tutorial, MouseOver Image Change

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Introduction:-
In this tutorial I will show you how to change the image when the user puts the cursor over an image and then change it back when the user puts the cursor away from the image.

Solution:-
Make These Two Functions Between The <head> tags:-


<SCRIPT language="JavaScript">


if (document.images)

 {

     img1on= new Image(100,100);

     img1on.src="img2.jpg";  


     img1off= new Image(100,100);

     img1off.src="img1.jpg";

 }


function change(imgName)

 {

   if (document.images)

      {

         imgOn=eval(imgName + "on.src");

         document[imgName].src= imgOn;

      }

 }


function changeback(imgName)

 {

   if (document.images)

      {

        imgOff=eval(imgName + "off.src");

        document[imgName].src= imgOff;

      }

 }


</SCRIPT>


And Make This code between the <body> Tags:-


<A onMouseover="change('img1')" onMouseout="changeback('img1')">

<IMG SRC="img1.jpg" name="img1" width="100" height="100"></A>


Save your work and add these 2 images into your directory to see the effect ;)
Posted Image

Posted Image

Explanation:-
You think it is needed? I think its self explanatory. But if you have any problems.. just ask ;)

A Preview:-
Posted Image

Conclusion:-
As Always Feedback is welcome and the full source is attached!!

Attached Files



#2
sahar88

sahar88

    Newbie

  • Members
  • Pip
  • 5 posts
it's cool ... thank you...

#3
cvirus

cvirus

    Newbie

  • Members
  • Pip
  • 2 posts
Very good tutorial, nice for a web site.

#4
cryptokyle

cryptokyle

    Newbie

  • Members
  • PipPip
  • 11 posts

if (document.images)

 {

     img1on= new Image(100,100);

     img1on.src="img2.jpg";  


     img1off= new Image(100,100);

     img1off.src="img1.jpg";

 }

i need help with understanding this portion of the code.:o

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
It is creating a new image that is 100x100 (size) and the src is img2.jpg. Similar to saying:

<img src="img2.jpg" width="100" height="100">