I'm learning about mouse events. At the moment, my goal is to have 2 images on top of each other. For example, you move the mouse over an image and see a pop up. When you move the mouse away, the pop up goes. I think this is quite a common thing.
Because I have an image in front of the other, my research suggests I need to use mouseleave
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove_leave_out
The problem I have is that I get a flickering effect as I try to hide or remove the hide class (which makes the image visible or not)
This snippet shows everything
var inst = document.getElementById("instrument");
inst.addEventListener("mouseover", function() {
toggleEdit(true);
})
inst.addEventListener("mouseleave", function() {
toggleEdit(false);
})
var edit = document.getElementById("edit");
var i = 0;
function toggleEdit(isShow) {
console.log(i++);
edit.className = edit.className.replace(" hide", "");
if (!isShow) {
edit.className += " hide";
}
}
.parent {
position: relative;
margin-left: 40%;
background: green;
}
.image1 {
position: absolute;
top: 0;
left: 0;
}
.image2 {
position: absolute;
top: 0px;
}
.image2 img {
max-width: 20px;
}
.hide {
visibility: hidden;
display: block;
}
<div class="parent">
<img class="image1" src="https://placehold.it/100" id="instrument" />
<a id="edit" href="#" class="image2 hide"><img src="https://cdn.pixabay.com/photo/2017/06/06/00/33/edit-icon-2375785_960_720.png" /></a>
</div>
How do I prevent this flickering?