So I’ve been able to figure out how to create a simple overlay when hovering over image map areas, but I don’t know how to make it only hover over the area element I’m hovering over. I’m new to JS and HTML so I wouldn’t know what to look for. It’s all for an assignment I’m doing and we aren’t even learning JS which I know I need so this is really the only place I know to look.
Here’s the main HTML for the image map I have at the moment.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Buses Map</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<img class="mapbus" src="Test_assetsMap_Rescaled.png" width="1920px" height="1080px" alt="BusesMap" usemap="#busmap">
<map name="busmap">
<area shape="circle" class="HounBStn" coords="932.5,463,16" href="Hounslow_Bus_Station.html" alt="HounslowBusStn">
<area shape="circle" coords="829,463,14.5" href="TreatyCentre.html" alt="TreatyCtr">
<area shape="rect" class="Overlay-81" coords="20,953,280,964" href="Bus_81.html" alt="">
<area shape="rect" coords="20,967.5,315,979" href="Bus_110.html" alt="">
<area shape="rect" coords="20,982,310,994" href="Bus_111.html" alt="">
<area shape="rect" coords="20,997,275,1008" href="Bus_116.html" alt="">
<area shape="rect" coords="20,1011.5,345,1023" href="Bus_117.html" alt="">
<area shape="rect" coords="20,1026,275,1037" href="Bus_120.html" alt="">
<area shape="rect" coords="20,1041,355,1052" href="Bus_203.html" alt="">
<area shape="rect" coords="20,1055.5,245,1067" href="Bus_222.html" alt="">
<area shape="rect" coords="385,953,645,964" href="Bus_235.html" alt="">
<area shape="rect" coords="385,967.5,640,979" href="Bus_237.html" alt="">
<area shape="rect" coords="385,982,690,994" href="Bus_281.html" alt="">
<area shape="rect" coords="385,997,725,1008" href="Bus_423.html" alt="">
<area shape="rect" coords="385,1011.5,680,1023" href="Bus_635.html" alt="">
<area shape="rect" coords="385,1026,650,1037" href="Bus_681.html" alt="">
<area shape="rect" coords="385,1041,635,1052" href="Bus_E8.html" alt="">
<area shape="rect" coords="385,1055.5,640,1067" href="Bus_H20.html" alt="">
<area shape="rect" coords="750,967.5,1110,979" href="Bus_H22.html" alt="">
<area shape="rect" coords="750,982,1040,994" href="Bus_H28.html" alt="">
<area shape="rect" coords="750,997,1095,1008" href="Bus_H32.html" alt="">
<area shape="rect" coords="750,1011.5,1065,1023" href="Bus_H37.html" alt="">
<area shape="rect" coords="750,1026,980,1037" href="Bus_H98.html" alt="">
<area shape="rect" coords="750,1041,1100,1052" href="Bus_N9.html" alt="">
</map>
<div id="overlay">test</div>
<script src="script.js"></script>
</body>
</html>
Then the CSS For the Image map itself
#overlay{
position: absolute;
top: 0;
bottom: 0;
background-color: aquamarine;
display: none;
z-index: 100;
}
.mapbus{
display: block;
}
And then the little Javascript that I’ve also got for the basic overlay I have now.
document.querySelectorAll('area').forEach(area => {
area.addEventListener('mouseover', () => {
document.getElementById('overlay').style.display = 'block';
});
area.addEventListener('mouseout', () => {
document.getElementById('overlay').style.display = 'none';
});
});
All I’ll really need is to know how to do it for one and then I should be able to figure out the rest from that.
I tried making the javascript change the size and position of the overlay when I hovered over each area element on the image map by checking for specific classes in the area tag through an if statement, which I initially used on colour to test if it worked. It didn’t and when hovering over the area element the class was attached to it still showed the aquamarine colour from prior.
This if statement was placed in the mouseover listener above.
if (document.area.class = 'Overlay-81') {
document.getElementById('overlay').style.background-color == 'black';
}
I also attempted it through CSS with another element, but I later abandoned that idea when I learnt from a prior post that area tags are invisible so I wouldn’t see that.
.x:hover{
opacity: 0.7;
background-color: brown;
}
Again I’m new to JavaScript cause I knowingly chose to stay away from it due to how much of a pain it is.