英文:
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
andonMouseLeave
,onMouseOver
andonMouseOut
, all withonFocus
andonBlur
. - 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
andonMouseLeave
,onMouseOver
andonMouseOut
, all withonFocus
andonBlur
- 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;
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论