Forum Moderators: open

Message Too Old, No Replies

Mouseovers/ Image swapping

multiple images

         

pcguru333

9:07 pm on Mar 26, 2002 (gmt 0)

10+ Year Member



Can someone explain to me how to create a mouseover that will work cross browser?

I want the most stripped down simplified script. I am trying to learn JavaScript and when I search on mouseover scripts I see such a wide array and I don't understand the mechanics of scripting enough to interpret which is best for my use.

I have 6 buttons that simply need to swap on mouseover.

Thanks

MikeFoster

10:22 pm on Mar 26, 2002 (gmt 0)

10+ Year Member



Here's a simple script that I use often. This example has 2 buttons - home and about.

Notice the calls to newImg(), they pass a name. The image files are assumed to be that name concated with "_over.gif" and "_out.gif". The images are assumed to be in the folder "images". The calls to newImg() also pass an identifying number.

In the onmouseover and onmouseout events in the A tags, call "rollImg()". You must pass it two arguments - first, a 1 to indicate a "mouseover", or a 0 to indicate a "mouseout"; the second argument is the identifying number passed to newImg().

One more assumption... the "name" of each IMG tag must be "btnImg0", "btnImg1", etc. where the number corresponds to the identifying number passed to newImg().

Simple, but it works for me ;-)


<html>
<head>
<script type='text/javascript'>
var overImg = new Array(), outImg = new Array();
newImg('home', 0);
newImg('about', 1);
function newImg(name, idx) {
overImg[idx] = new Image();
overImg[idx].src = 'images/' + name + '_over.gif';
outImg[idx] = new Image();
outImg[idx].src = 'images/' + name + '_out.gif';
}
function rollImg(over, n) {
var i = document.images['btnImg' + n];
i.src = over ? overImg[n].src : outImg[n].src;
}
</script>

</head>
<body>
<a href='#' onmouseover="rollImg(1,0)" onmouseout="rollImg(0,0)">
<img name='btnImg0' border='0' src='images/home_out.gif'>
</a>
<a href='#' onmouseover="rollImg(1,1)" onmouseout="rollImg(0,1)">
<img name='btnImg1' border='0' src='images/about_out.gif'>
</a>
</body>
</html>

txbakers

10:27 pm on Mar 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use this simple one:

function over(img) {
img.src=img + "o.gif";
}

function out(img) {
img.src=img + ".gif";
}

You name your images as follows: reg.gif, rego.gif
one for regular, one for over.

Then, in the HTML:

<a href="page.htm" ><img src="smile.gif" onMouseover="over(this)" on Mouseout = "out(this)" name="smile"></a>

That's all there is to it. You name your image files with the "o" for over, then you name the image tag the name of the file as well.

pcguru333

2:00 pm on Mar 27, 2002 (gmt 0)

10+ Year Member



Thank you both for your help

MikeFoster

4:04 pm on Mar 29, 2002 (gmt 0)

10+ Year Member



txbakers had a great idea about using only a name - so I couldn't resist rewriting my script:


<html>
<head>
<script type='text/javascript'>

rollInit('home', 'links', 'about');

function rollInit() {
var i, n, dir = 'images/', ext = '.gif';
if (!window.rollArray) window.rollArray = new Array();
for (i=0; i<arguments.length; ++i) {
n = arguments[i];
rollArray[n+'r'] = new Image();
rollArray[n+'r'].src = dir + n + '_over' + ext;
rollArray[n+'t'] = new Image();
rollArray[n+'t'].src = dir + n + '_out' + ext;
}
}
function rollImg(over, name) {
document.images[name].src = rollArray[name + (over ? 'r':'t')].src;
}
</script>

</head>
<body>
<a href='#' onmouseover="rollImg(1,'home')" onmouseout="rollImg(0,'home')">
<img name='home' border='0' src='images/home_out.gif'>
</a>
<a href='#' onmouseover="rollImg(1,'links')" onmouseout="rollImg(0,'links')">
<img name='links' border='0' src='images/links_out.gif'>
</a>
<a href='#' onmouseover="rollImg(1,'about')" onmouseout="rollImg(0,'about')">
<img name='about' border='0' src='images/about_out.gif'>
</a>
</body>
</html>

Rhys

12:15 pm on Mar 31, 2002 (gmt 0)

10+ Year Member



I have to jump in on this one, my favourite beef is using fat scripts for rollovers.
Here is a dead simple rollover code.
<a href="a_file.htm" onmouseover="img1.src='dot2.gif';" onmouseout="img1.src='dot.gif';">
- where img1 is the name of the image tag and dot and dot2 are the roll over images. E.G <IMG name=img1 src="dot.gif" width=30 border=0>.
A second link on the same page would refer to img2, a third to img3, and so on.
Viola - NO scripts required - this is elegantly simple, and seems to work on different browsers!!

tedster

2:01 pm on Mar 31, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have very stripped down code there - I can't help but comment on two things:

1. The "onmouseover" inside the anchor tag IS a script. Turn off javascript and you will not get a new image on rollover.

2. You are not pre-loading the mouseover-state image into the cache - that takes a script in the HEAD. So that image only gets called from the server when the user mouses over the area. Perhaps not noticeable on a local network or broadband connection, but a definite time lag on a dial-up.

In fact, one of my pet peeves is rollovers that don't preload the images. The page feels really broken when you first use it.

However, I'm on your side - I also hate inflated mouseover scripts, which are usually generated by WYSIWYG editors.

Rhys

12:11 am on Apr 1, 2002 (gmt 0)

10+ Year Member



Right on Tedster-

<one of my pet peeves is rollovers that don't preload the images>

I usually preload the rollover image with an invisible <img src="dot2.gif" height=2 width=2 border=0> tag at the foot of the page. I thought this trick was beyond the scope of the original question, so didn't include it.

<Turn off javascript and you will not get a new image on rollover>

Does anybody?? The page still displays well without the script working.
Cheers - Rhys

tedster

12:33 am on Apr 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rhys: preload the rollover image with an invisible <img src="dot2.gif" height=2 width=2 border=0> tag at the foot of the page.

Right on. If you can tolerate those little dots, or accommodate them into your look some how, that's one way to go. In fact, I'll bet you could create a special class for images in css that hides them in their pre-load spot.

Tedster: Turn off javascript and you will not get a new image on rollover.
Rhys: Does anybody?? The page still displays well without the script working.

That's true - an important fact about rollovers is that they can degrade very nicely. I was only making the point that this method DOES use some javascript, so that this was clear for all readers.

And sure, people do disable javascript for several reasons. Depends on the site's demographic, but it can be 10% or more.

pcguru333

5:41 pm on Apr 4, 2002 (gmt 0)

10+ Year Member



Just a quick update:

I was able to develop my own OnMouseOver scripts to preload images and swap images. I used ideas from a variety of sources (including scripts from this thread). I dunno if it is the most stripped down version, but I understood the logic to make it work.

Below is the script I used in the Head:

<script type='text/javascript'>
if (document.images) {
img1on = new Image();
img1on.src = "images/Home_Button.gif";
img2on = new Image();
img2on.src = "images/Newsletter_Button.gif";
img3on = new Image();
img3on.src = "images/Mission_Button.gif";
img4on = new Image();
img4on.src = "images/Hunting_Button.gif";
img5on = new Image();
img5on.src = "images/Fishing_Button.gif";
img6on = new Image();
img6on.src = "images/Homestay_Button.gif";

img1off = new Image();
img1off.src = "images/Home_Button_onMouseOver.gif";
img2off = new Image();
img2off.src = "images/Newsletter_Button_onMouseOver.gif";
img3off = new Image();
img3off.src = "images/Mission_Button_onMouseOver.gif";
img4off = new Image();
img4off.src = "images/Hunting_Button_onMouseOver.gif";
img5off = new Image();
img5off.src = "images/Fishing_Button_onMouseOver.gif";
img6off = new Image();
img6off.src = "images/Homestay_Button_onMouseOver.gif";
}

function roll (x, y) {
if (document.images) {
img = new Image;
img.src = y;

document.images[x].src = img.src;

}
}

</script>

and in the body I used this for each link/imageswap:

ex:
<a href=
"index.html" onmouseover="roll (4, 'images/Home_Button_onMouseOver.gif')"
onmouseout="roll (4, 'images/Home_Button.gif')"><img src=
"images/Home_Button.gif" width="140" height="45" alt=
"Home_Button.gif (1K)" border="0" /></a>