Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i trigger onMouseEnter for elements behind other elements

I'm trying to trigger mouseEnter event when mouse is on top of multiple elements.

I want both mouseEnter events to trigger when the mouse is at the center, and preferably for both to turn yellow.

Run the code snippet below for an example:

<!DOCTYPE html>
<html>
<head>
<style>
div:hover {
  background-color: yellow;
}
div {
  width: 100px;
  height:100px;
  background:green;
  border: 2px solid black;
}
.second {
  transform:translateX(50%) translateY(-50%);
}
</style>
<script>
  function onhover(){console.log('hovered')}
</script>
</head>
<body>

<div onmouseenter=onhover()></div>
<div onmouseenter=onhover() class='second'></div>

</body>
</html>
like image 973
Eric Freeman Avatar asked Oct 31 '25 13:10

Eric Freeman


2 Answers

According to MDN, the mouseenter event does not bubble, whereas the mouseover event does. However, even if it DID bubble, your elements currently have no relation to one another, thus the mouse events are captured by the upper element.

One possible way around this is with the amazing elementsFromPoint function in JavaScript, which makes quick work of solving your issue:

// Only the IDs of the elments you are interested in
const elems = ["1", "2"];

// Modified from https://stackoverflow.com/a/71268477/6456163
window.onload = function() {
  this.addEventListener("mousemove", checkMousePosition);
};

function checkMousePosition(e) {
  // All the elements the mouse is currently overlapping with
  const _overlapped = document.elementsFromPoint(e.pageX, e.pageY);

  // Check to see if any element id matches an id in elems
  const _included = _overlapped.filter((el) => elems.includes(el.id));
  const ids = _included.map((el) => el.id);

  for (const index in elems) {
    const id = elems[index];
    const elem = document.getElementById(id);

    if (ids.includes(id)) {
      elem.style.background = "yellow";
    } else {
      elem.style.background = "green";
    }
  }
}
div {
  width: 100px;
  height: 100px;
  background: green;
  border: 2px solid black;
}
.second {
  transform: translateX(50%) translateY(-50%);
}
<div id="1"></div>
<div id="2" class="second"></div>
like image 80
Aaron Meese Avatar answered Nov 03 '25 02:11

Aaron Meese


I think that you can not without javascript, and with it it's a bit tricky, you have to check on every mousemove if the coordinates of the mouse are in de bounding box of the element, this fill fail with elements with border radius but for the others it's ok

<script>


var hovered=[]
function addHover(element){hovered.push(element)}

function onhover(element){console.log("hovered",element)}

function onCustomHover(e){
    hovered.forEach((el,i)=>{
        let bounds=el.getBoundingClientRect()
        if (e.pageX > bounds.left && e.pageX < bounds.bottom  &&
            e.pageY > bounds.top && e.pageY < bounds.right ) {            
            onhover(i);
        }
    })
}


</script>
</head>
<body>

<div id="div1"></div>
<div id="div2" class='second'></div>
<script>
    document.body.addEventListener('mousemove', onCustomHover, true);//{capture :false});
    addHover(document.getElementById("div1"))
    addHover(document.getElementById("div2"));
</script>

I would appreciate if you could rate the answer if that was usefull to you because I can not make comments yet <3

like image 22
Edoldin Avatar answered Nov 03 '25 03:11

Edoldin