/    Sign up×
Community /Pin to ProfileBookmark

Specialized Javascript Image Rollover

I am trying to make a javascript image rollover feature for my site. The problem is I am using angled images, which makes the rollover difficult. I found a script on O’Reilly’s website which allows for using only 3 images (one for normal state, one for mouseover, and one for mousedown) and using image maps and CSS to control which part of the image is displayed, provding a mousover effect. The only problem is it is set to use rectangle shapes on the image maps, and I need something that can use polygon shapes.

A demo of the code can be seen here :

[url]http://www.oreillynet.com/javascript/2003/07/01/examples/jsdhtmlcb_bonus1_example.html[/url]

The complete code is :


————————————————————————————————

————————————————————————————————

————————————————————————————————

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>
<base href=”http://www.oreillynet.com/javascript/2003/07/01/examples/“>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
<title>Scriptable Imagemaps</title>
<script type=”text/javascript”>
/*****************************************
Scriptable Image Maps by Danny Goodman ([url]www.dannyg.com[/url])
A bonus recipe for readers of O’Reilly’s
“JavaScript & DHTML Cookbook”
This recipe first published at O’Reilly Network ([url]www.oreillynet.com[/url])
For full implementation notes, read the article.
*
**
***************************************/
function initMaps() {
if (document.getElementById) {
var mapIds = initMaps.arguments; // pass string IDs of containing map elements
var i, j, area, areas;
for (i = 0; i < mapIds.length; i++) {
areas = document.getElementById(mapIds[i]).getElementsByTagName(“area”);

for (j = 0; j < areas.length; j++) { // loop thru img elements
area = areas[j];
area.onmousedown = imgSwap; // set event handlers
area.onmouseout = imgSwap;
area.onmouseover = imgSwap;
area.onmouseup = imgSwap;
}
}
}

}

// image swapping event handling
function imgSwap(evt) {
evt = (evt) ? evt : event;
var elem = (evt.target) ? evt.target : evt.srcElement;
var imgClass = elem.parentNode.name;
var coords = elem.coords.split(“,”);
var clipVal = “rect(” + coords[1] + “px ” +
coords[2] + “px ” +
coords[3] + “px ” +
coords[0] + “px)”;
var imgStyle;

switch (evt.type) {
case “mousedown” :
imgStyle = document.getElementById(imgClass + “Down”).style;
imgStyle.clip = clipVal;
imgStyle.visibility = “visible”;
break;
case “mouseout” :
document.getElementById(imgClass + “Over”).style.visibility = “hidden”;
document.getElementById(imgClass + “Down”).style.visibility = “hidden”;
break;
case “mouseover” :
imgStyle = document.getElementById(imgClass + “Over”).style;
imgStyle.clip = clipVal;
imgStyle.visibility = “visible”;
break
case “mouseup” :
document.getElementById(imgClass + “Down”).style.visibility = “hidden”;
// guarantee click in IE
if (elem.click) {
elem.click();
}
break;
}
evt.cancelBubble = true;
return false;

}

</script>
</head>

<body onload=”initMaps(‘menubarMap’)”>

<div style=”position:relative”>
<img id=”menubarUp” src=”/javascript/2003/07/01/graphics/menubarUp.jpg” height=”110″ width=”680″ alt=”menubar” border=”0″ style=”position:absolute; top:0px; left:0px; visibility:visible” usemap=”#menubar” />
<img id=”menubarOver” src=”/javascript/2003/07/01/graphics/menubarOver.jpg” height=”110″ width=”680″ alt=”menubar” border=”0″ style=”position:absolute; top:0px; left:0px; visibility:hidden” usemap=”#menubar” />
<img id=”menubarDown” src=”/javascript/2003/07/01/graphics/menubarDown.jpg” height=”110″ width=”680″ alt=”menubar” border=”0″ style=”position:absolute; top:0px; left:0px; visibility:hidden” usemap=”#menubar” />
</div>
<map id=”menubarMap” name=”menubar”>
<!– onclick set to return false to disable links for this demo; remove for your application –>
<area shape=”rect” coords=”8,22,117,86″ href=”index.html” alt=”new” title=”new” onclick=”return false” />
<area shape=”rect” coords=”120,22,227,86″ href=”products.html” alt=”products” title=”view products” />
<area shape=”rect” coords=”230,22,337,86″ href=”manuals.html” alt=”manuals” title=”download manuals” onclick=”return false” />
<area shape=”rect” coords=”340,22,447,86″ href=”dealers.html” alt=”dealers” title=”find a dealer” onclick=”return false” />
<area shape=”rect” coords=”450,22,557,86″ href=”support.html” alt=”support” title=”get support” onclick=”return false” />
<area shape=”rect” coords=”560,22,667,86″ href=”contact.html” alt=”contact” title=”contact us” onclick=”return false” />
</map>

</body>

</html>


————————————————————————————————

————————————————————————————————

————————————————————————————————

Can anyone help to refromat the code to accept polygon shapes?

Thanks.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@PittimannAug 09.2004 — Hi!Can anyone help to refromat the code to accept polygon shapes?[/QUOTE]As far as the areas are concerned: yes - no problem. It would be easy to modify the function dealing with the coordinate extraction. But to do what? To calculate a rectangle from the polygonal area again.

The code is based on the clip property and in CSS2, there is only one shape for the clipping region allowed: a rectangle. So the entire script cannot be of any service for you.

Depending on what exactly you want, there might be completely different ways to achieve it.

Cheers - Pit
Copy linkTweet thisAlerts:
@hartonauthorAug 09.2004 — Pit, thanks for the reply.

What I am trying to do is make a rollover effect on images that are angled (geometrically speaking they are in the shape of a parallelogram). The images are right next to each other, making it impossible to use standard rollover scripts, as that results in cropping of the corners.

You can see an example here :

http://www.geocities.com/electronicsnation/


Any ideas on how to make this work?
Copy linkTweet thisAlerts:
@PittimannAug 09.2004 — Hi!

Seems your solution should be a normal rollover, nothing special (which would be possible though); If you had 3 rectangular images for each parallelogram, the left and right one consisting of a triangle in the desired color and a second transparent triangle, then for the middle a bigger rectangle with the desired color containing also the text, you would need to do a rollover on each of the three for all of them. This would have to be available for each item. 3 images and triple rollover.

Do you understand what I mean?

Cheers - Pit
Copy linkTweet thisAlerts:
@hartonauthorAug 09.2004 — I just saw a website, www.interland.com, that also uses angled images, and they achieve a rollover effect by simply highlighting multiple images at 1 time (the image, and the image to its left).

While I do understand your post, doing it in the style of Interland's website may be a little easier, or at least less time consuming.

What do you think?

Thanks for your help.

Jason
Copy linkTweet thisAlerts:
@PittimannAug 09.2004 — Hi!

Without looking at their code - that is very close to what I tried to express. Just do it! ?

Cheers - Pit
Copy linkTweet thisAlerts:
@hartonauthorAug 09.2004 — I misread your post the first time around - I see what you meant now. You're right, your solution was the same thing.

Thanks for your help. I will look at their code and make it work for my images.

Thanks again Pit.
Copy linkTweet thisAlerts:
@PittimannAug 09.2004 — Hi!

You're welcome! ?

And maybe, you didn't misread my post, but I miswrote it. English is quite foreign to me.

Good luck - Pit
Copy linkTweet thisAlerts:
@hartonauthorAug 09.2004 — Pit.

I tried an alternative to Interland's code.

It uses image maps and one large image.

The load time is a little high, but I am sure it can be worked on.

What do you think of this :

http://www.geocities.com/electronicsnation/
×

Success!

Help @harton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...