浏览器悬停事件过慢

huangapple go评论76阅读模式
英文:

Brower hover events too slow

问题

I have a photo, which displays an overlay when hovered.

Everything works fine, but if I'm fast with the mouse (going from top to bottom of the page, for example), the overlay will be shown when I pass over the photo but won't hide when the hover is done.

Here is what I tried:

  • Different event handlers like onMouseEnter and onMouseLeave, onMouseOver and onMouseOut, all with onFocus and onBlur.
  • Conditional CSS classes: ${!isHover && 'hidden'}

But always got the same result.

Any idea how to improve this so the browser can follow the state change?

Here is the simplified code:

const Photo = (): JSX.Element => {
  const [isHover, setHover] = useState<boolean>(false);

  const LayoutFactory = (): JSX.Element => {
    switch (true) {
      case archived:
        return <ArchiveLayout />;
      case Boolean(photo):
        return isHover ? (
          <HoverLayout />
        ) : (
          <IconPlayOutline />
        );
      default:
        return <></>;
    }
  };

  const MissingImageFactory = (): JSX.Element => {
    if (archived) {
      return (
        <div>
          {/* CONTENT */}
        </div>
      );
    }

    return <MissingImage />;
  };

  const LayoutWrapper = ({ children }: PropsWithChildren): JSX.Element => {
    return (
      <div>
        {children}
      </div>
    );
  };

  const HoverLayout = (): JSX.Element => {
    return (
      <LayoutWrapper>
        {/* CONTENT */}
      </LayoutWrapper>
    );
  };

  const ArchiveLayout = (): JSX.Element => {
    return (
      <LayoutWrapper>
        <div>
          {/* CONTENT */}
        </div>
      </LayoutWrapper>
    );
  };

  const Image = (): JSX.Element => {
    return photo ? (
      <img
        alt="annonce"
        src={photo}
      />
    ) : (
      <MissingImageFactory />
    );
  };

  return (
    <button
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      {...props}
    >
      {/* CONTENT */}
    </button>
  );
};

export default Photo;

Please note that some parts of the code may require additional context to provide a full translation.

英文:

I have a photo, which displays an overlay when hovered.

Everything works fine, but if i'm beeing fast with the mouse (going from top to bottom of page for example), the overlay will be shown when i pass hover the photo, but won't hide when hover is done.

Here is what I tried :

  • different events handler like onMouseEnter and onMouseLeave, onMouseOver and onMouseOut, all with onFocus and onBlur
  • conditional CSS classes : ${!isHover &amp;&amp; &#39;hidden&#39;}

But always got the same result.

Any idea how to improve this so the browser can follow the state change ?

Here is the simplified code:

const Photo = (): JSX.Element =&gt; {
const [isHover, setHover] = useState&lt;boolean&gt;(false);
const LayoutFactory = (): JSX.Element =&gt; {
switch (true) {
case archived:
return &lt;ArchiveLayout /&gt;;
case Boolean(photo):
return isHover ? (
&lt;HoverLayout /&gt;
) : (
&lt;IconPlayOutline /&gt;
);
default:
return &lt;&gt;&lt;/&gt;;
}
};
const MissingImageFactory = (): JSX.Element =&gt; {
if (archived) {
return (
&lt;div&gt;
// CONTENT
&lt;/div&gt;
);
}
return &lt;MissingImage /&gt;;
};
const LayoutWrapper = ({ children }: PropsWithChildren): JSX.Element =&gt; {
return (
&lt;div&gt;
{children}
&lt;/div&gt;
);
};
const HoverLayout = (): JSX.Element =&gt; {
return (
&lt;LayoutWrapper&gt;
// CONTENT
&lt;/LayoutWrapper&gt;
);
};
const ArchiveLayout = (): JSX.Element =&gt; {
return (
&lt;LayoutWrapper&gt;
&lt;div&gt;
// CONTENT
&lt;/div&gt;
&lt;/LayoutWrapper&gt;
);
};
const Image = (): JSX.Element =&gt; {
return photo ? (
&lt;img
alt=&quot;annonce&quot;
src={photo}
/&gt;
) : (
&lt;MissingImageFactory /&gt;
);
};
return (
&lt;button
onMouseEnter={() =&gt; setHover(true)}
onMouseLeave={() =&gt; setHover(false)}
{...props}
&gt;
// CONTENT
&lt;/button&gt;
);
};
export default Photo;

答案1

得分: 1

我认为州无法跟上事件触发的速度。您可以尝试在几毫秒后检查一次鼠标是否仍然在图片上,以“修复”它。不过,我不太喜欢这个解决方案,因为它很糊弄,难以维护,而且可能很容易出问题。

最好的方法可能是找到一个CSS解决方案,因为我认为对于大多数与样式相关的问题来说,这是最好的方法。这些更容易维护并且性能更好。经典的CSS仍然有很多优点 浏览器悬停事件过慢

英文:

I think the state isn't able to keep up with how fast the events are fired. You can try to check once after a few ms if the mouse is still on the picture to "fix" it. I don't really like that solution tho, as it is very hacky, hard to maintain and will probably break easily.

The best way is probably to find a css solution, as I think it is for most style related questions. Those are easier to maintain and more performant. Good old css still has a lot going for it 浏览器悬停事件过慢

huangapple
  • 本文由 发表于 2023年5月17日 21:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定