0

I am working on an image map with a popup textbox. Everything works fine. However, I am not sure how to display the textbox next to the image when I click on a hotspot. Right now, the popup box displays on top of the hotspot.

Here is the link to my code - https://jsfiddle.net/a3gtf8rw/

var myData = {
    "left-eye": {
        "title": "This point A",
        
        "description": "Lorem ipsum A dolor sin amet."
    },
    "mouth": {
        "title": "This point B",
       
        "description": "Lorem ipsum B dolor sin amet."
    },
   
};
var areas         = document.getElementsByTagName('area'),
    bubble        = document.getElementById('myBubble'),
    bubbleContent = document.getElementById('myBubbleContent'),
    bubbleClose   = document.getElementById('myBubbleCloseButton');

// On click of an area, open popup
for(var i=0, l=areas.length; i<l; i++) {
  areas[i].addEventListener('click', openBubble, false);
}

// On click of close button, close popup
bubbleClose.addEventListener('click', closeBubble, false);

function openBubble() {
  var content = myData[this.id];
  bubbleContent.innerHTML = '<h3>' + content.title + '</h3>'
                          + '<img src="' + content.image + '" alt="" />'
                          + '<p>' + content.description + '</p>';
  bubble.className = 'shown';
}

function closeBubble() {
  bubble.className = '';
}

// Make the image map responsive
imageMapResize();
#myWrapper{ position: relative; font-family: Arial, Helvetica, sans-serif; }
#myImage{ display: block; }
#myBubble{ position: absolute; background: #fff; border: 1px solid #aaa; padding: .5rem 1rem; display: none; top: 1.5rem; left: 1.5rem; width: 15rem; }
#myBubble.shown{ display: block; }
#myBubble img{ display: block; width: 100%; }
#myBubbleCloseButton{ position: absolute; top: 0; right: 0; padding: .5rem; background: #eee; line-height: 1; cursor: pointer; }
#myBubbleCloseButton:hover{ background: #000; color: #fff; }
<script src="https://cdn.rawgit.com/davidjbradshaw/image-map-resizer/master/js/imageMapResizer.js"></script>

<div id="myWrapper">
   <img id="myImage" src="http://tinypic.com/images/goodbye.jpg" usemap="#myMap" alt="" />


 <map name="myMap" id="myMap">
  <area id="left-eye"  coords="118,36,148,80" shape="rect">
  <area id="mouth"  coords="121,84,198,118" shape="rect">
</map>

   <div id="myBubble">
      <div id="myBubbleContent"></div>
      <div id="myBubbleCloseButton">✕</div>
    </div>
</div>
newuser
  • 245
  • 1
  • 1
  • 8

1 Answers1

1

I added a bit of code to read the position of the mouse cursor and use that to position the bubble. The result is:

var myData = {
    "left-eye": {
        "title": "This point A",
        
        "description": "Lorem ipsum A dolor sin amet."
    },
    "mouth": {
        "title": "This point B",
       
        "description": "Lorem ipsum B dolor sin amet."
    },
   
};
var areas         = document.getElementsByTagName('area'),
    bubble        = document.getElementById('myBubble'),
    bubbleContent = document.getElementById('myBubbleContent'),
    bubbleClose   = document.getElementById('myBubbleCloseButton');

// On click of an area, open popup
for(var i=0, l=areas.length; i<l; i++) {
  areas[i].addEventListener('click', openBubble, false);
}

// On click of close button, close popup
bubbleClose.addEventListener('click', closeBubble, false);

function openBubble(event) {
  var content = myData[this.id];
  bubbleContent.innerHTML = '<h3>' + content.title + '</h3>'
                          + '<img src="' + content.image + '" alt="" />'
                          + '<p>' + content.description + '</p>';
  bubble.style.top = (event.clientY + 30) + "px";                        
  bubble.style.left = (event.clientX + 30) + "px";                        
  bubble.className = 'shown';
}

function closeBubble() {
  bubble.className = '';
}

// Make the image map responsive
imageMapResize();
#myWrapper{ position: relative; font-family: Arial, Helvetica, sans-serif; }
#myImage{ display: block; }
#myBubble{ position: absolute; background: #fff; border: 1px solid #aaa; padding: .5rem 1rem; display: none; width: 15rem; }
#myBubble.shown{ display: block; }
#myBubble img{ display: block; width: 100%; }
#myBubbleCloseButton{ position: absolute; top: 0; right: 0; padding: .5rem; background: #eee; line-height: 1; cursor: pointer; }
#myBubbleCloseButton:hover{ background: #000; color: #fff; }
<script src="https://cdn.rawgit.com/davidjbradshaw/image-map-resizer/master/js/imageMapResizer.js"></script>

<div id="myWrapper">
   <img id="myImage" src="http://tinypic.com/images/goodbye.jpg" usemap="#myMap" alt="" />


 <map name="myMap" id="myMap">
  <area id="left-eye"  coords="118,36,148,80" shape="rect">
  <area id="mouth"  coords="121,84,198,118" shape="rect">
</map>

   <div id="myBubble">
      <div id="myBubbleContent"></div>
      <div id="myBubbleCloseButton">✕</div>
    </div>
</div>

Here is the JSFiddle: https://jsfiddle.net/xqo2ge3m/

I am not sure this is what you wanted?

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Thankyou. This works in fiddle but not in my environment. I looked at the browser console for errors. It gives me undefined error for this code - function openBubble(event) { var content = myData[this.id]; bubbleContent.innerHTML = '

    ' + content.title + '

    ' + '' + '

    ' + content.description + '

    '; bubble.style.top = (event.clientY + 30) + "px"; bubble.style.left = (event.clientX + 30) + "px"; bubble.className = 'shown'; }
    – newuser Jul 26 '23 at 15:30
  • My best guess is that the `event` is undefined? I am not sure. I cannot debug code I don't have. – KIKO Software Jul 26 '23 at 15:34
  • How do I do that? I am new to JS, still trying to learn. – newuser Jul 26 '23 at 15:44
  • Nm, it was working but the pop up box is getting displayed at the bottom of the page. I change the number 30 to 1 but still no good. Do I use negative numbers? – newuser Jul 26 '23 at 16:08
  • Sorry, it was dinner time here. There can be many causes for your problem, but the pop up box is places with `position: absolute;`, which means that it is positioned relative to its closest positioned ancestor. The `event.clientX` and `event.clientY` are however always positioned in relation to the browser's window. If the image is not placed in the upper left corner of the windows these two will not be the same. So you need to subtract [the position of the wrapper](https://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element), positioned with `position: relative;`. – KIKO Software Jul 26 '23 at 17:25
  • Thankyou, I just had to change the postion from absolute to Fixed and it worked. Thanks again. – newuser Jul 26 '23 at 17:37
  • @newuser Ah, yes, that makes sense: Also set the pop up box with respect to the browser window, just like the mouse cursor coordinates. – KIKO Software Jul 26 '23 at 17:43