1

I have a question similar to this one but involving Blazor. I have a div that works like a popup menu which which contains child elements that the user can click on to do something. I would like the popup div to disappear when the user moves the mouse off of it. I can use the onmouseout event, but the problem is that moving the mouse over the child elements within the popup div triggers that event. I want onmouseout to fire only when the mouse moves out of the parent div. The only solution I found so far that kind of works is to use CSS to set "pointer-events: none;" on the child elements. But that also disables the click event on them, which I want to maintain. Blazor does not support onmouseleave which is recommended elsewhere.

So how can I use the onmouseout event in Blazor on a div which is not triggered by its child elements?

UPDATE:

Cory's answer to use @onmouseout:preventDefault on the child elements did work. But the mouseout event did not fire reliably. I successfully found a work around by setting classes on the div container when showing the popup and used CSS onhover to hide and show it like this

Rono
  • 3,171
  • 2
  • 33
  • 58

2 Answers2

1

This may not be right but couldn't you use @onmouseout:preventDefault or @onmouseout:stopPropagation on the child HTML elements?

I haven't tested of course, but these exist for all Blazor events: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-5.0#prevent-default-actions

Cory Podojil
  • 776
  • 2
  • 10
0

Slight deviation but this solved the issue for me:

Instead of:

onmouseover
onmouseout

I used:

onmouseenter
onmouseleave

In my case this applied to the use case where I wanted to show / hide an overlay for a card.

<div class="template" onmouseenter="@this.MouseOver" onmouseleave="@this.MouseOut">
Ben Pretorius
  • 4,079
  • 4
  • 34
  • 28