gpt4 book ai didi

javascript - 使用 React-Icon 库将鼠标悬停在图标上时显示文本

转载 作者:行者123 更新时间:2023-12-05 09:07:39 27 4
gpt4 key购买 nike

所以我试图在您将鼠标悬停在鼠标上时显示文本。我正在使用 React-Icons 库并使用 Styled-Components 进行样式设置

我的导航栏上有 4 个图标 - 主页 - 关于 - 技能 - 工作

每个按钮都是它自己的组件,以便悬停正常工作,所以当我将鼠标悬停在 1 个图标上时,它不会显示所有按钮的文本

import React, { useState } from 'react';
import { SkillsButton } from './SkillsBtnElements'

const SkillsBtn = () => {
const [hover, setHover] = useState(false);
const onHover = () => {
setHover(!hover)
}

return (
<div onMouseEnter={onHover} onMouseLeave={onHover} role="button" tabIndex='-3' >
{ hover ? "SKILLS" : <SkillsButton /> }
</div>
)
}

export default SkillsBtn;

对于样式我有

import styled from 'styled-components';
import { GiGearHammer } from 'react-icons/gi';

export const SkillsButton = styled(GiGearHammer)`
font-size: 1.75rem;
color: white;
flex-grow: 1;
cursor: pointer;
@media screen and (max-width: 960px) {
transition: all 0.2s ease-in-out;
font-size: 1rem;
color: white;
}
&:hover {
transition: all 0.2s ease-in-out;
}
`;

我确实实现了悬停效果,但是当我不断悬停图标时,逻辑似乎变得一团糟,然后它只出现了文本,当我将鼠标悬停在文本上时,图标出现了......这不是想要的效果

示例:https://gph.is/g/4ARQoRV

最佳答案

异常效果是由于陈旧的关闭问题。 {hover ? "SKILLS" : <SkillsButton />}正在使用悬停的陈旧值呈现。如果您希望文本仅在鼠标悬停在 div 上时出现,请尝试为 onMouseEnter 和 onMouseLeave 事件创建两个单独的函数。像这样:

import React, { useState } from "react";
import { SkillsButton } from './SkillsBtnElements'

const SkillsBtn = () => {
const [hover, setHover] = useState(false);
const onHover = () => {
setHover(true);
};

const onLeave = () => {
setHover(false);
};
return (
<div
onMouseEnter={onHover}
onMouseLeave={onLeave}
role="button"
tabIndex="-3"
>
{hover ? "SKILLS" : <SkillsButton />}
</div>
);
};

export default SkillsBtn;

关于javascript - 使用 React-Icon 库将鼠标悬停在图标上时显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64709508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com