Andrick Borderguard & Ketchup Co.
code.junkie: js image roll
 [ A really simple JavaScript image button rollover ]

Yah, there are 4,000.6 different versions of this out there -- but I've always been partial to this one. I think I wrote it about 300 years ago when dinosaurs still roamed the web, and it's held me in good stead ever since. I use it a lot. It might be in the fossil record.

the code:
In the header [or your .js file], you'll need this function:

<script type="text/javascript" language="JavaScript">
<!--

function normRoll(imgname, imgsrc) {
	imgsrc = "images/" + imgsrc;
	document.images[imgname].src = imgsrc;
	return true;
}

//-->
</script>

Then, let's look at the href for the image you want to roll:

<a href="http://andrick.com"
onMouseover="normRoll('home','home_on.gif');"
onMouseout="normRoll('home','home.gif');">
<img src="img/home.gif" alt="roll over me" name="home" width="145" height="30" border="0">
</a>


No big huhu, right? First variable is the image name, second is what you'd like to change it to. That second line in the function points it where you need to go, since most of us tend to organize image directories -- into, say, one.

Here's a working example:

roll over me

considerations
  • Nope, no preloader. And I don't really care, frankly. Most of the world we design for is off the 22.8 modem standard folks; as long as you're using optimized GIFs or small JPGs [which is what this is meant for] the enduser won't notice.
  • Want to make your life easy? Name the 'on' state for the image 'home.gif' as 'home_on.gif'. Seems silly to mention it, but there you go.
  • You can, of course, use it to control other images on a page as well. Just point the imgname variable where you want it to go.
Home About: T.E.D. Andrick About: Our Philosphy About: The Name Portfolio: Web Design Portfolio: Template Design Portfolio: Identity Design Portfolio: Print Design Clients/Projects: Client List Clients/Projects: Projects List Clients/Projects: Special Projects More: Filter.Junkie Thin Chrome Gold Text Infamous MacOS 'Gel' Buttons More: Code.Junkie YoungPup's dropdown menus The IE6 PNG display hack A really simple JavaScript image button rollover A not-simple CSS image button rollover Sliding DIVs Fading DIVs The IE/ActiveX Control hack Flash and JavaScript variables Flash & XML text marquee The gerenic emailer The beauty of the dynamic header & footer The generic picture viewer More: Links Contact: Employment Contact: Analog Space