Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multi image rollovers

Status
Not open for further replies.

mleahy

Technical User
Feb 24, 2003
19
IE
Hi all,

I am trying to create a multi image rollovers where when I rollover an image that image will change plus another image on the page. I can do a basic rollover no probs.

Does anyone know what to do or where to look?

cheers
Marie
 
Marie,

This will do the trick:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

	function changeImages()
	{
		document.getElementById('firstImage').src = 'imageA_Rollver.gif';
		document.getElementById('secondImage').src = 'imageB_Rollver.gif';
	}

//-->
</SCRIPT>
</HEAD>
<BODY>

<IMG SRC=&quot;a.gif&quot; onMouseOver=&quot;changeImages();&quot; ID=&quot;firstImage&quot;>
<IMG SRC=&quot;b.gif&quot; ID=&quot;secondImage&quot;>

</BODY>
</HTML>

When you roll over the first image, both images will change.

I hope this helps!

Dan
 
Dan

Thanks for this you really got me out of a whole. The only thing is the image doesn't change back on mouseout and it doesn't work in netscape. Am I doing something wrong?

cheers
Marie
 

Marie,

The following adds rolloff functionality:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

    function changeImages(rolledOn)
    {
		if (rolledOn)
		{
	        document.getElementById('firstImage').src = 'imageAon.gif';
	        document.getElementById('secondImage').src = 'imageBon.gif';
		}
			else
		{
	        document.getElementById('firstImage').src = 'imageA.gif';
	        document.getElementById('secondImage').src = 'imageB.gif';
		}
    }

//-->
</SCRIPT>
</HEAD>
<BODY>

<IMG SRC=&quot;imageA.gif&quot; onMouseOver=&quot;changeImages(true);&quot; onMouseOut=&quot;changeImages(false);&quot; ID=&quot;firstImage&quot;>
<IMG SRC=&quot;imageB.gif&quot; ID=&quot;secondImage&quot;>

</BODY>
</HTML>

I tested the code, and it works fine in: IE 6, NN 7.1, Opera 7.21, and Mozilla 1.5.

What version of NN were you testing? Maybe 4.x, or something old like that?

Hope this code helps!

Dan
 
Hi Dan,

Thanks for your help last week - much appreciated. Just wondered how I would get the code to work on a couple of image in the one window. Maybe I am doing something wrong but when I rollover one image it effects everything and I wanted to have a couple of separate multi-image rollovers. What could I be doing wrong?

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function changeImages(rolledOn)
{
if (rolledOn)
{
document.getElementById('pic_about').src = 'img/pic_about_inv.jpg';
document.getElementById('button').src = 'img/hm_about.gif';
}
else
{
document.getElementById('pic_about').src = 'img/pic_about.jpg';
document.getElementById('button').src = 'img/x.gif';
}
}
//-->

<!--
function changeImages(rolledOn)
{
if (rolledOn)
{
document.getElementById('pic_rescampus').src = 'img/pic_rescampus_inv.jpg';
document.getElementById('button').src = 'img/hm_rescampus.gif';
}
else
{
document.getElementById('pic_rescampus').src = 'img/pic_rescampus.jpg';
document.getElementById('button').src = 'img/x.gif';
}
}
//-->
</SCRIPT>

Should what I am doing work.
Sorry to be a pest.
Marie :)
 

Marie,

Yes - it probably wouldn't work... You can't have 2 functions with the same name, as the browser gets confused about which one to run.

The following code should do the trick for you. It may not be the most efficient way of handling rollovers, but it's probably easier to understand than others - which probably makes it easier for you or others to add more images to if desired.

Here's the code:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

    function changeImages(imageObj, rolledOn)
    {
		if (imageObj.id == 'pic_about')
		{
	        if (rolledOn)
	        {
	            imageObj.src = 'img/pic_about_inv.jpg';
	            document.getElementById('button').src = 'img/hm_about.gif';
	        }
	            else
	        {
	            imageObj.src = 'img/pic_about.jpg';
	            document.getElementById('button').src = 'img/x.gif';
	        }	
		}

		if (imageObj.id == 'pic_rescampus')
		{
	        if (rolledOn)
	        {
	            imageObj.src = 'img/pic_rescampus_inv.jpg';
	            document.getElementById('button').src = 'img/hm_rescampus.gif';
	        }
	            else
	        {
	            imageObj.src = 'img/pic_rescampus.jpg';
	            document.getElementById('button').src = 'img/x.gif';
	        }	
		}
    }

//-->
</SCRIPT>
</HEAD>
<BODY>

<IMG SRC=&quot;img/pic_about.jpg&quot; onMouseOver=&quot;changeImages(this, true);&quot; onMouseOut=&quot;changeImages(this, false);&quot; ID=&quot;pic_about&quot;>
<IMG SRC=&quot;img/pic_rescampus.jpg&quot; onMouseOver=&quot;changeImages(this, true);&quot; onMouseOut=&quot;changeImages(this, false);&quot; ID=&quot;pic_rescampus&quot;>
<IMG SRC=&quot;img/x.gif&quot; ID=&quot;button&quot;>

</BODY>
</HTML>

Hope this helps!

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top