nonion
nonion

Reputation: 832

Text on top of image map

Currently, I have an image with a image map. Is there a way for me to write some JavaScript code that will use the area name as a variable to display what is actually on it? All the text and area names are inside a data array already.

For example, if this area is named Area1, it'll have the text Text1 inside, and etc.

Why would I want to do this? It's easier for me to print it out with the names on it.

Edit: Example of image map

<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap">

<map name="planetmap">
  <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
  <area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
  <area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>

Upvotes: 1

Views: 7462

Answers (1)

dm4web
dm4web

Reputation: 4652

Not very precise, but it can be a good basis for further improvement

http://jsfiddle.net/6amd2842/1/

HTML:

<div id="map">
    <img src="http://placehold.it/145x126" width="145" height="126" alt="Planets" usemap="#planetmap">
</div>

<map name="planetmap">
    <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun" data-name="Sun">
    <area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury"  data-name="Mercury">
    <area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus" data-name="Venus">
</map>

CSS:

#map {
    position:relative
}
.map_title {
    position:absolute;    
}

JQ:

$(function() {

    $('area').each(function(){
        var txt=$(this).data('name');
        var coor=$(this).attr('coords');
        var coorA=coor.split(',');
        var left=coorA[0];
        var top=coorA[1];

        var $span=$('<span class="map_title">'+txt+'</span>');        
        $span.css({top: top+'px', left: left+'px', position:'absolute'});
        $span.appendTo('#map');
    })

})

Upvotes: 4

Related Questions