Hey Deanna, welcome to the forums.
So there's a couple of problems with the code sample you provided.
- The </div>
right after the map is stopping it from working
- You're calling a function (writetxt) that isn't defined in what you provided
Anyways I wrote up a full example for you (see below). I used the sample images you provided and added one area to test.
The only thing not included is making sure it fits within the view port. Let me know if you still need this and I'll take a look.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#centeredMain {
/*
Center the div
*/
margin:auto;
}
.tooltip {
animation: fadein .25s;
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
</style>
<script type="text/javascript">
//An object of all the tooltips in the format tooltipName:imageLocation
const tooltips = {
"titansRevenge": "https://cdn.itemforge.com/catalog/product/cache/image/1000x1320/e9c3970ab036de70892d86c6d221abfe/t/i/titan-revenge-eth-upg.jpg"
};
//Run once the page is fully loaded
document.addEventListener("DOMContentLoaded", () => {
const areas = document.querySelectorAll("area");
//Loop through all the areas
for(let i=0; i<areas.length; i++) {
areas[i].addEventListener("mouseover", (e) => {
const target = e.target.getAttribute("data-tooltip");
const coords = e.target.getAttribute("coords").split(",");
//Create an image to use as a tooltip
let tooltip = document.createElement("img");
tooltip.src = tooltips[target];
tooltip.classList.add("tooltip");
//ID so it can be removed later
tooltip.id = target;
//Positition it
tooltip.style.position = "absolute";
tooltip.style.top = coords[1] + "px"
tooltip.style.left = (+coords[2] + 100) + "px"
tooltip.style.zIndex = "2";
//Add it to the body of the page
document.body.appendChild(tooltip);
});
areas[i].addEventListener("mouseout", (e) => {
const target = e.target.getAttribute("data-tooltip");
document.getElementById(target).remove();
});
}
});
</script>
</head>
<body>
<div id="centeredMain">
<img src="http://theinspirationgallery.com/D2-2020/images/image_map_example.png" usemap="#mysticzon">
<map name="mysticzon">
<area shape="rect" coords="529,304,595,431" alt="Titan's Revenge" data-tooltip="titansRevenge">
</map>
</div>
</body>
</html>
Bookmarks