Forum Moderators: open
There's no need build an array when you're only using one rollover image. And no need to use code that can handle three image states when you only use two. And no need for a complex "swap" function.
With the importance SEs place on the content to code ratio, it can be helpful to trim away all excess. With that in mind, this is what I use for basic rollover code. image1.gif is the main image and image2.gif is the rollover state.
HEAD
over=new Image(ww,hh)
over.src="image2.gif"
BODY
<a href="url.html" onMouseOver="spot.src=over.src" onMouseOut="spot.src='image1.gif'"><img src="image1.gif" name="spot" width="ww" height="hh"></a>
If this can be simplified any further, I'm very interested.
(edited by: tedster at 4:47 pm (utc) on Feb. 14, 2002)
<body onload="dynAnimation()" language="Javascript1.2">
<a onmouseover="document['fpAnimswapImgFP1'].imgRolln=document['fpAnimswapImgFP1'].src;document ['fpAnimswapImgFP1'].src=document['fpAnimswapImgFP1'].lowsrc;" onmouseout="document ['fpAnimswapImgFP1'].src=document['fpAnimswapImgFP1'].imgRolln" href="javascript:void(0)"><img border="0" src="x.gif" id="fpAnimswapImgFP1" name="fpAnimswapImgFP1" dynamicanimation="fpAnimswapImgFP1" lowsrc="x1.gif"></a>
Also noted, I believe you mentioned this Tedster, it uses "javascript" instead of the more acceptable "text/javascript"
When using WYSIWYG I just use it to spare me writing HTML, and indentation to save more space
Just a comparison, note the difference!
I like the idea of wrapping the .src change into a function. When there's lots of rollovers, that eliminates some decent file size.
Note: your code above works fine for Explorer, but other browsers don't all support an onMouseOver event handler unless it's inside an anchor tag.
<script language="JavaScript">
<!---
function MouseOver(name1,name2,img1,img2) {
eval("document." + name1 + ".src = '" + img1 + "'");
eval("document." + name2 + ".src = '" + img2 + "'");
}
function MouseOut(name1,name2,img1,img2) {
eval("document." + name1 + ".src = '" + img1 + "'");
eval("document." + name2 + ".src = '" + img2 + "'");
}
//--->
</script>
And this is what you do with the anchor element
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<a href="#" onMouseover="MouseOver('imgname1','imgname2','images/new_img1.jpg','images/new_img2.jpg'); return false;" onMouseout="MouseOut('imgname1','imgname2','images/original_img1.jpg','images/original_img2.jpg'); return false;">
<img name="imgname1" src="images/original_img1.jpg" border="0" width="225" height="295">
</a>
</td>
<td>
<a href="#" onMouseover="MouseOver('imgname2','imgname1','images/new_img3.jpg','images/new_img4.jpg'); return false;" onMouseout="MouseOut('imgname2','imgname1','images/original_img2.jpg','images/original_img1.jpg'); return false;">
<img name="imgname2" src="images/original_img2.jpg" border="0" width="225" height="295">
</a>
</td>
</tr>
</table>
I was originally trying to write the function like so
function MouseOver(name1,name2,img1,img2) {
document.name1.src = img1;
document.name2.src = img2;
}
But it kept giving me errors. So I found a site that said that I had to use the eval() function so that it could render the code correctly for the borwser to read. It worked. :)
<A HREF="url.htm" ONMouseOver="document.ITEM.src='image_on.gif'" onMouseOut="document.ITEM.src='image_off.gif'"><IMG SRC="image_off.gif" WIDTH=WW HEIGHT=HH BORDER=0 NAME="ITEM"></A>
This code doesn't require anything in the Head of the document.
I need some help, though, does anyone know of a way to link a rollover image that is an image map? I want to use two links for the rollover image (the original doesn't require any links). If you have any ideas, please help!
<map name="FPMap1">
<area href="" onMouseOver="document.ITEM.src='images/imageNr1.jpg'" onMouseOut="document.ITEM.src='images/imageNr0.gif'"
shape="rect" coords="0, 0, 91, 69" alt="link alt-text 1">
<area href="" onMouseOver="document.ITEM.src='images/imageNr2.jpg'" onMouseOut="document.ITEM.src='images/imageNr0.gif'"
shape="rect" coords="135, 0, 209, 71" alt="link alt-text 2">
</map>
<IMG SRC="images/imageNr0.gif" WIDTH="210" HEIGHT="222" BORDER=0 NAME="ITEM" usemap="#FPMap1">
I've been trying to get two different image rollovers to work in an absolutely positioned div in NN4. I've been able to get it to work in every other browser I've tested or just NN4 but not both.
Ideally, I'd like the bulk of the code to be in the head because I'm using the rollovers on every page.
Thanks for your help.
If that's taken care of, check through the basics with an eye to typos, like missing some single or double quotes, or changing the spelling of the name attribute somewhere in the code.
The mouse event behavior is enclosed in double quotes, so the image names within must be enclosed in single quotes.
ROLLOVER BASICS
1. Rollover images downloaded into the cache
2. Each <img> tag is given both a name attribute and a src attribute. The src attribute defines the original image for that position: src="main_image.gif"
3. onMouseover (inside the anchor tag) brings in the rollover image: onMouseover= "name_1.src='rollover_image.src'"
4. onMouseout (again, inside the anchor tag) changes things back: onMouseout="name_1.src='main_image.gif'"
I've gotten help on another forum where I was told that NN4 needs to pass the div id which works but then it won't work in any other browser.
Here is the original download sample from [dhtmlnirvana.com...] in which the only change I've made is to absolutely position the div. The positioning causes NN4 to not work and declare a javascript error. I've tried every other script I can find and the result is always the same when the rollovers are contained in an absolutely positioned div. Any idea what I can do to make it or another script work?
<html>
<head>
<title>Image Swap Test</title>
<script language="JavaScript">
img1 = new Image();img1.src = "img.jpg";
function swapIt(img, src) {
document.images[img].src = src;
}
</script>
<style type="text/css">
<!--
#eddie {
position: absolute; top: 200px; left: 200px;
}
-->
</style>
</head>
<body bgcolor="#408080">
<a href="#" onmouseover="swapIt('img1', 'img.jpg')" onmouseout="swapIt('img1', 'offimg.jpg')">
<img id="img1" name="img1" src="offimg.jpg" border="0">
</a>
<br>
<br>
<a href="#" onmouseover="swapIt('bad1', 'img.jpg')" onmouseout="swapIt('bad1', 'offimg.jpg')">
<img id="bad1" name="bad1" src="offimg.jpg" border="0">
</a>
<br>
<br>
<div id="eddie">
<a href="#" onmouseover="swapIt('blah1', 'img.jpg')" onmouseout="swapIt('blah1', 'offimg.jpg')">
<img id="blah1" name="blah1" src="offimg.jpg" border="0">
</a>
</div>
</body>
</html>
[members.rogers.com...]
I'm thinking something like:
if (document.layers)...
else if (document.all)...
But I don't know enough about JavaScript to put it together. Or if it is even possible.
Thanks for your help.
I'm using the style of rollover that I mentioned in the start of this thread - that is, my onMouseover and onMouseout do not call a JS function declared from the head. They just reassign the .src for each image from within the anchor tag.
It’s near the bottom of the page under “DOM image rollover”.
[fo3nix.pwp.blueyonder.co.uk...]
I think I'm in compliance with the URL rule, if not, sorry in advance.
In an external .js file, I put something like this:
var btnList='home about products order contact'.split(' '), btn=new Array();
function Button(name){
this.on=new Image();
this.off=new Image();
this.on.src='buttons/'+name+'-on.gif';
this.off.src='buttons/'+name+'-off.gif';
}
for(var i=0; i<btnList.length; i++) btn[btnList[i]]=new Button(btnList[i]);
function rollOn(id){
document[id].src=btn[id].on.src;
}
function rollOff(id){
document[id].src=btn[id].off.src;
}
Why create an array of objects? Well, it preloads the images, that's why.
The HTML looks like this:
<a href="sales.html" onmouseover="rollOn('sales');" onmouseout="rollOff('sales');"><img src="buttons/sales-off.gif" width="xx" height="xx" alt="Sales" border="0" name="sales" /></a>
The JavaScript is easy to edit if I want to add or remove a button; I just have to change the first line. The HTML is trickier... unless I use a PHP include or something. Using an external .js file means that it is cached, so the site as a whole is a bit faster.