Does anyone know how to propagate/trigger the mouseover event to multiple children when they appear overlaid on top of each other? Is it even possible?
I'm trying to get mouseover to be triggered for 2 overlaid elements. I've written a basic example to demonstrate the problem:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
background-color: black;
}
.relative {
position: relative;
}
.child-1,
.child-2 {
position: absolute;
width: 200px;
height: 200px;
}
.child-1 {
top: 0;
left: 0;
background-color: rgba(255, 255, 0, 0.5);
}
.child-2 {
top: 50px;
left: 50px;
background-color: rgba(0, 255, 255, 0.5);
}
</style>
<body class="relative">
<div class="child-1">1</div>
<div class="child-2">2</div>
</body>
<script>
const child1 = document.querySelector('.child-1');
const child2 = document.querySelector('.child-2');
child1.addEventListener('mousemove', () => console.log(1));
child2.addEventListener('mousemove', () => console.log(2));
</script>
</html>
Whenever I hover over just child 1, I get a log of 1. When I hover over just child 2, I get a log of 2. That all works. But when I hover over 1 and 2 where they overlap, I only get a log of which ever one is on top, in this case 2 (this can be flipped by adjusting the z-index or order in the DOM).
I found this answer, but changing the z-index or pointer-events: none doesn't help me because I need both the element's event to fire.