Steps to create an image map
If you're looking to create an image map, consider using Gimp as suggested by Badacadabra. Alternatively, you can explore online tools like image-maps.com.
To identify each area within the map, assign unique IDs:
<img id="myImage" src="https://i.sstatic.net/ErRo2.png" usemap="#myMap" alt="" />
<map name="myMap" id="myMap">
<area id="node-a" shape="rect" coords="731,159,757,188"/>
<area id="node-b" shape="rect" coords="685,139,713,168"/>
<area id="node-c" shape="rect" coords="597,142,625,171"/>
<!-- ... -->
</map>
Making it responsive for all screens
The challenge with image maps lies in their non-responsive nature. To overcome this issue in modern times, consider using a plugin such as Image Map Resizer by David Bradshaw. Simply enable it with this snippet:
imageMapResize();
Structuring your data effectively
An ideal approach to associating content with each node is by utilizing an Object:
var myData = {
"node-a": {
"title": "This point A",
"image": "image-a.jpg",
"description": "Lorem ipsum A dolor sin amet."
},
/* Add more nodes and their details here */
};
Implementing a personalized popup feature
If crafting popups isn't your forte, various plugins offer ready-made solutions. For customization, try creating a simple div element that can be toggled:
<div id="myBubble">
<div id="myBubbleContent"></div>
<div id="myBubbleCloseButton">✕</div>
</div>
Add CSS styling and JavaScript functionality to enhance its visual appeal and interactivity:
// Script referencing DOM elements
// Use Array.from() instead of getElementsByTagName('area')
var areas = Array.from(document.getElementsByTagName('area')),
bubble = document.getElementById('myBubble'),
bubbleContent = document.getElementById('myBubbleContent'),
bubbleClose = document.getElementById('myBubbleCloseButton');
// Trigger popup on area click
areas.forEach(function(area) {
area.addEventListener('click', openBubble, false);
});
// Close popup on close button click
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.classList.add('shown'); // Using classList method
}
function closeBubble() {
bubble.classList.remove('shown'); // Utilize classList for better handling
}
Dive into the Demo
Test out the functionalities by expanding the code snippet provided below: