简体   繁体   English

Onmouseover 和 mouseout 不断闪烁和触发

[英]Onmouseover and mouseout keeps blinking & firing

I have columns and I want to display images when my mouse goes over the images, however, mouseover and mouseout keeps firing when i move on the same image.我有列,我想在鼠标移到图像上时显示图像,但是,当我在同一图像上移动时,mouseover 和 mouseout 会继续触发。 So the images keep blinking.所以图像一直在闪烁。 How can I resolve this?我该如何解决这个问题?

I filtered the image class (media-overlay) to change its display我过滤了图像 class (媒体覆盖)以更改其显示

this is my example https://jsfiddle.net/saltykiam/bw4ap70v/5/这是我的例子https://jsfiddle.net/saltykiam/bw4ap70v/5/

let row = document.getElementById("row");

    row.onmouseover =(e)=>{    
    [...e.path].filter(element => 
      element.classList && element.classList.contains("media-overlay"))[0].style.display = "none";
    }
    
    row.onmouseout =(e)=>{    
    
    [...e.path].filter(element => 
      element.classList && element.classList.contains("media-overlay"))[0].style.display = "block";
    
    }

The issue is that when you call your 'onmouseover' event, the image is being set to问题是当您调用“onmouseover”事件时,图像被设置为

display: none;

This actually removes the image from the page, so now when you move your mouse anywhere you're actually calling your "onmouseout" event.这实际上从页面中删除了图像,所以现在当您将鼠标移动到任何地方时,您实际上是在调用“onmouseout”事件。 This is because the image is no longer on the page, has no physical dimensions, and so any movement of the mouse is considered moving your mouse out of the image.这是因为图像不再在页面上,没有物理尺寸,因此鼠标的任何移动都被视为将鼠标移出图像。

To fix this, apply the style change against opacity rather than display .要解决此问题,请针对opacity而不是display应用样式更改。 Set opacity to 0 for when you want the image to disappear:当您希望图像消失时,将不透明度设置为 0:

row.onmouseover = (e) => {
  [...e.path].filter(element =>
    element.classList && element.classList.contains("media-overlay"))[0].style.opacity = 0;}

and set the opacity to 1 when you want the image to reappear:当您希望图像重新出现时,将不透明度设置为 1:

row.onmouseout = (e) => {
  [...e.path].filter(element =>
    element.classList && element.classList.contains("media-overlay"))[0].style.opacity = 1;}

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

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