0

I am using an IMG Map that has many small pictures. When I hover over each image it will display a larger image. How do I do this? I do not mind using javascript, css, etc as long as it works. Please provide an example.

HTML OF IMG MAP

<img src="img.jpg" alt="Main Photo - Please View With Images On"
usemap="#Map" class="center" style="width:900px;" border="0" />
<map name="Map" id="Map">
 <area class="1" shape="rect" coords="4,3,225,150" />
 <area class="2" shape="rect" coords="2,152,221,303" />
 <area class="3" shape="rect" coords="3,303,221,452"  />
</map>

Each class would have a different image to display larger.

4
  • possible repetition of stackoverflow.com/questions/745110/…
    – Exor
    Commented Jun 21, 2012 at 6:35
  • suggest getting a tooltip plugin, easy to find in google. Store the image url in each area in a data attribute, pass it to plugin
    – charlietfl
    Commented Jun 21, 2012 at 6:44
  • @charlietfl - I have tried to use qTip 2 but has an issue, see: stackoverflow.com/questions/11131941/qtip-2-and-images
    – L84
    Commented Jun 21, 2012 at 6:54
  • qtip can do a lot, but there are simpler API's for tooltips also that aren't as difficult to wade through a bazillion options and config settings
    – charlietfl
    Commented Jun 21, 2012 at 6:59

2 Answers 2

2

Here's a way to do it with ImageMapster:

http://jsfiddle.net/zSBL5/

To make this work with its built-in tooltip API you need to do a couple things. First add an attribute to each area like this:

<area data-key="2,all"  shape="rect" coords="0,90,82,170" 
    href="http://www.shelterness.com/pictures/amazing-diy-photo-tiles-1-500x373.jpg" />

Note data-key. This is ImageMapster's "mapKey". The only reason we need it for your situation is to create a single area group called "all" that can be used to show the same tooltip for each area. Each area will have an attribute like this but with a different number, e.g. data-key="3,all", data-key="4,all", and so on.

Then here's the code to bind imagemapster:

$('img').mapster({
    toolTipContainer: $('#tooltip-container'),
    mapKey: 'data-key',
    areas: [ {
        key: 'all',
        toolTip: true
    }],
    showToolTip: true,
    onShowToolTip: function(data) {
        if (this.href && this.href!='#') {
            data.toolTip.html('<img src="'+this.href+'">');
        }
    }});​

Here's what each option means:

1) toolTipContainer should contain the HTML for a template to show the tooltip. In the fiddle, you'll see I added a hidden div with ID "tooltip-container"

2) mapKey is the name of the attribute you added to each area. This attribute can contain one or more comma separated values. Imagemapster groups together areas for highlighting that share the first value, so use different ones if you don't need to group any areas. The 2nd value should be the same for each area, I use this to activate tooltips for all areas.

3) areas is an array of area-specific formatting information. Usually you use this to control how the highlighting effects on each area are shown. In your case I'm just using it to activate tooltips for all areas. The key all matches every area, since they all have that as then 2nd key, and toolTip: true enables tooltips. Normally you would say toolTip: "some text specific to this area:" and that text would just get shown in your default tooltip container. In your case, we want to show an image, and I want to grab the image URL from the area itself, so I need to handle it in a function when the tooltip is shown.

4) showToolTip says enable tooltips for the whole map.

5) onShowToolTip defines a function that gets called whenever a tooltip is shown. From here you can intercept it and change the contents to show the image from that area.

This should be simpler - but the tooltip API in imagemapster is really designed around a very simple model where you just have some hardcoded text for each area. This is a workaround to grab the URL from the the href of each area.

0

I think you want something like the below given fiddle. Code written is very rough & can be optimized. Just check you want this or something else.

Demo: http://jsfiddle.net/3GF6s/3/

2
  • Thanks for the help. It seems to work but has a few glitches. It will flicker on and off if you do not have the mouse positioned properly.
    – L84
    Commented Jun 21, 2012 at 6:57
  • Yup! I know as said code is written roughly. You can style its according to your need.
    – SVS
    Commented Jun 21, 2012 at 7:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.