0

I have a map like who is mapped by coords:

<img id="mapImage" src="https://i.imgur.com/Y7HuHDQ.png" usemap="#image-map">

<map name="image-map">

        <area class="tooltip" target="" alt="USA" title="USA" href="" coords="110,140,182,141,186,148,198,146,198,140,214,139,224,134,234,143,221,150,213,154,205,156,205,170,199,174,194,181,193,188,162,182,158,171,149,173,145,179,141,170,124,168,121,173,112,168,105,158" shape="poly">
        
         <area target="" alt="MEXICO" title="MEXICO" href="" coords="124,172,137,171,141,178,152,173,160,181,162,204,179,199,179,211,157,212,173,216,146,204,136,196,125,188" shape="poly">
         
         <area target="" alt="JAPAN" title="JAPAN" href="" coords="705,106,716,106,721,146,687,184,667,175" shape="poly">
         
         <area target="" alt="GERMANY" title="GERMANY" href="" coords="418,111,436,108,433,124,418,124" shape="poly">
         
</map>

I want to set information tooltip on hover with css like this but it no works:

<style>
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;

    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
</style>

I want it to set information like:

Country: USA Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173

How can I achieve that? Regards

2
  • Country: USA Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173 Where does this information come from? It does not seem to be anywhere in the code you posted (except for the country) Commented May 29, 2018 at 1:30
  • It´s just an example, I want to achieve tooltip thing @CertainPerformance Commented May 29, 2018 at 2:32

1 Answer 1

1

Here is my solution, Its not a perfect solution, but it will give you a starting point.

Open the snippet in full screen

I am adding mouseover event listener to each area, Then on hover, I fetch their coordinates

var coordinates = this.getAttribute('coords').split(',') 

Here this means the area element

After getting the cordinates, I apply the first and last value of coords to left and top value of the tooltip.

var areas = document.getElementsByTagName('area');
var tooltip = document.getElementById('tooltip');
for (var i = 0; i < areas.length; i++) {
  areas[i].addEventListener("mouseover", updateTooltip)
}

function updateTooltip() {
  tooltip.innerHTML = this.getAttribute('data-text');
  var coordinates = this.getAttribute('coords').split(',')
  console.log(coordinates[0], coordinates[coordinates.length - 1]);
  tooltip.style.left = coordinates[0] + 'px';
  tooltip.style.top = coordinates[coordinates.length - 1] + 'px';
}
.tooltip {
  position: absolute;
  z-index: 9999;
  border: 1px solid black;
  min-width: 100px;
  max-width: 150px;
  background: white;
}

area {
  position: relative;
}

.parent{
position:relative;
}
<div class="parent">
<img id="mapImage" src="https://i.imgur.com/Y7HuHDQ.png" usemap="#image-map">

<map name="image-map">

        <area target="" data-text="USA Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173" alt="USA" title="USA" href="" coords="110,140,182,141,186,148,198,146,198,140,214,139,224,134,234,143,221,150,213,154,205,156,205,170,199,174,194,181,193,188,162,182,158,171,149,173,145,179,141,170,124,168,121,173,112,168,105,158" shape="poly">
        
         <area target="" data-text="Mexico Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173"  alt="MEXICO" title="MEXICO" href="" coords="124,172,137,171,141,178,152,173,160,181,162,204,179,199,179,211,157,212,173,216,146,204,136,196,125,188" shape="poly">
         
         <area target="" data-text="Japan Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173"  alt="JAPAN" title="JAPAN" href="" coords="705,106,716,106,721,146,687,184,667,175" shape="poly">
         
         <area target="" data-text="Germany Shops: Manchester,TN - Num: 6621418372 WIXOM ,MI - Num:662728173"  alt="GERMANY" title="GERMANY" href="" coords="418,111,436,108,433,124,418,124" shape="poly">
      <div class="tooltip" id="tooltip"></div>   
</map>

</div>

Sign up to request clarification or add additional context in comments.

1 Comment

NICE!, and is possible to close tooltip when mouse is outside tooltip or object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.