Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouseover and mouseout trigger multiple times

Tags:

I have these two on mouseover and mouseout events in JS. They are for checking wether I'm hovering over a panel on my page. But the problem is when I hover over a panel it triggers (good), but then when I hover over another element inside that panel it triggers a the mouseout event and then the mouseover events again when I move to another part inside the panel.

I only want the mouseover and mouseout events firing once! Once for entering the panel another for leaving it. Is there a way to propagate the on mouseover call to all child elements in the div.panel. It seems like that would correct it.

Here are my events

$(document).off("mouseover").on("mouseover", "div.panel", function() {
   var panelSpaceId = $(this).attr("data-spaceId");
   var marker = _.find(scope.markers, function(item) {
     return item.spaceId == panelSpaceId
   })
   google.maps.event.trigger(marker, "mouseover");
   console.log("over" + marker.spaceId);
 });

 $(document).off("mouseout").on("mouseout", "div.panel", function() {
   var panelSpaceId = $(this).attr("data-spaceId");
   var marker = _.find(scope.markers, function(item) {
     return item.spaceId == panelSpaceId
   })
   google.maps.event.trigger(marker, "mouseout");
   console.log("out" + marker.spaceId);
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
like image 882
user1186050 Avatar asked Jan 14 '17 00:01

user1186050


People also ask

What is difference between Mouseenter () and mouseover () event?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

What is mouseover and Mouseout?

The mouseover event occurs when a mouse pointer comes over an element, and mouseout – when it leaves. These events are special, because they have property relatedTarget . This property complements target . When a mouse leaves one element for another, one of them becomes target , and the other one – relatedTarget .

Is mouseover and hover same?

Alternatively referred to as mouseover or mouse hover, hover describes the act of moving a mouse pointer over a clickable object, but not actually clicking the left or right mouse button. For example, when you hover your mouse over any of the links on this page, they should change color, indicating they can be clicked.

How do you trigger a Mouseenter event?

The mouseenter() method triggers the mouseenter event, or attaches a function to run when a mouseenter event occurs.. Note: Unlike the mouseover event, the mouseenter event only triggers when the mouse pointer enters the selected element.


1 Answers

You can avoid this issue by using mouseenter instead of mouseover, and mouseleave instead of mouseout. The reason is simply: mouseenter is only fired when the cursor "enters" the element, which includes its children—so hovering over the child of the element does not re-fire the mouseenter event.

This similar logic applies to mouseleave vs of mouseout. You can see this effect happening based on a fiddle created by @gilly3 in his answer to a similar question. I am not marking your question as duplicate because your answer is primarily troubleshooting mouseover/leave events instead of asking about the distinct between mouseover/enter and mouseout/leave.

It is helpful to imagine this:

  • entering means when your cursor enters the bounds of the element. Even when you are in a child node, you are still within the bounds, hence it is never triggered multiple times.
  • (h)overing can be visualised as seeing your element on a canvas. If the element you your cursor over is visible on the screen, the mouseover event is triggered. The cursor is no longer over the parent element when your cursor moves over the child element, hence when you go back and forth the event is triggered over and over again.

Here is your modified code, where I simply replaced the offending events with the right ones:

$(document).off("mouseenter").on("mouseenter", "div.panel", function() {
  var panelSpaceId = $(this).attr("data-spaceId");
  var marker = _.find(scope.markers, function(item) {
    return item.spaceId == panelSpaceId
  })
  google.maps.event.trigger(marker, "mouseenter");
  console.log("over" + marker.spaceId);
});

$(document).off("mouseleave").on("mouseleave", "div.panel", function() {
  var panelSpaceId = $(this).attr("data-spaceId");
  var marker = _.find(scope.markers, function(item) {
    return item.spaceId == panelSpaceId
  })
  google.maps.event.trigger(marker, "mouseleave");
  console.log("out" + marker.spaceId);
});
like image 108
Terry Avatar answered Sep 28 '22 06:09

Terry