简体   繁体   English

如果悬停在另一个重叠元素上,请保持鼠标悬停

[英]keep mouse over active if hovering over another overlapping element

I've got a DOM structure like the one below.我有一个像下面这样的 DOM 结构。 These to elements are overlaping.这些 to 元素是重叠的。 On hover, video start to play.悬停时,视频开始播放。 Unfortunately, when I hover over .showsOnHover , mouseover and mouseout repeatedly fire one after another making .showsOnHover blink and video stutter.不幸的是,当我将鼠标悬停在.showsOnHover , mouseover 和 mouseout 会反复触发.showsOnHover闪烁和视频卡.showsOnHover

What should I do to instruct browser to treat hovering over .showsOnHover as one continuous hover?我该怎么做才能指示浏览器将悬停在.showsOnHover视为一次连续悬停?

.html .html

  <video (mouseover)="mouseover()" (mouseout)="mouseout()"></video>
  <div [hidden]="!hovering" class="showsOnHover"></div>

.ts .ts

  mouseover(e){
    this.hovering = true;
    this.video.play();

  }
  mouseout(e){
    this.hovering = false;
    this.video.stop();
  }

The issue is that when showsOnHover is shown, it covers up the video, which makes it mouseout, so it hides the div, then the hover event triggers again, and it shows the div, and it just loops over and over very rapidly. 问题是,当显示showsOnHover时,它会覆盖视频,从而使其showsOnHover鼠标,因此它会隐藏div,然后再次触发悬停事件,并显示div,并且循环非常快。

You essentially will want to do one of the following: 从本质上讲,您将需要执行以下操作之一:

  • Put the mouseover and mouseout events on both the video and the showsOnHover elements, perhaps do it on a containing parent div element. 将mouseover和mouseout事件放在视频 showsOnHover元素上,也许在包含父div元素上进行。 This way the mouseout event won't be triggered when it's shown; 这样,在显示时将不会触发mouseout事件。 instead it will only trigger when you leave the larger containing element. 相反,它只会在您离开较大的包含元素时触发。

  • Add pointer-events: none; 添加pointer-events: none; to your .showsOnHover class in the CSS. 到CSS中的.showsOnHover类。 This makes that div transparent to mouse events. 这使该div对鼠标事件透明。 However, this could cause issues if you wanted to run any click events off that button. 但是,如果您想通过该按钮运行任何单击事件,则可能会导致问题。

  • Change mouseover to mouseenter as mentioned in Ram's answer Ram的回答中所述,将mouseover更改为mouseenter

Use mouseenter instead of mouseover 使用mouseenter而不是mouseover

mouseenter(e){
    this.hovering = true;
    this.video.play();

  }

[.mouseover()] can cause many headaches due to event bubbling. [.mouseover()]可能由于事件冒泡而引起许多头痛。 For instance, when the mouse pointer moves over the Inner element in this example, a mouseover event will be sent to that, then trickle up to Outer. 例如,在此示例中,当鼠标指针移到Inner元素上时,将向该元素发送mouseover事件,然后再向上移动到Outer。 This can trigger our bound mouseover handler at inopportune times. 这可能会在不合时宜的时候触发绑定的鼠标悬停处理程序。 See the discussion for .mouseenter() for a useful alternative. 有关有用的替代方法,请参见.mouseenter()的讨论。

Please see What is the difference between the mouseover and mouseenter events? 请参阅mouseover和mouseenter事件之间有什么区别?

create a parent div and fire the same event on that, 创建一个父div并在其上触发相同的事件,

<div class="parentDiv" (mouseenter)="mouseover()" (mouseleave)="mouseout()">
  <video></video>
  <div [hidden]="!hovering" class="showsOnHover"></div>
</div>

使用(mouseenter)(mouseleave)代替(mouseover)(mouseout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM