0

我想通过增加图标大小来制作悬停效果,图标来自反应图标,我正在使用样式组件。我应该怎么做?

 export const BottomBar = () => {
  const style = { fontSize: "180%" };
  return (
    <StyledBottomBar>
      <StyledIcon>
        <FaFacebookSquare style={style} />
      </StyledIcon>
      <StyledIcon>
        <GrInstagram style={style} />
      </StyledIcon>
      <StyledIcon>
        <CgMail style={style} />
      </StyledIcon>
      <StyledIcon>
        <BiPhoneCall style={style} />
      </StyledIcon>
    </StyledBottomBar>
  );
};

谢谢 !!!

4

2 回答 2

1

所以 react-icons 将被渲染为<svg>元素。这些具有您可以使用样式覆盖的属性,您只能通过元素本身的操作来操作它们。

在您的示例中,如果您查看开发工具并检查 html,则 svg 元素周围可能有一个 div 包装器,这意味着您尝试应用于它们的样式已应用于 div 。

const style = { fontSize: "180%",transition: 'font-size 0.5s'  }

//Try writing it like this:
const style = {
    '& svg': {
        fontSize: '180%',
        transition: 'fontSize 0.5s'
    }
}

在这里,我们将这些规则应用于 svg 元素而不是它的包装器。

编辑 如果您想收听点击或悬停,您可以在包装 svg 的 div 上收听!确保它也具有相同的大小。

于 2021-06-09T02:19:13.613 回答
1

无法执行内联样式的操作,例如:hover. 您可以使用 JS 方法onMouseEnteronMouseLeave

const style = { fontSize: "180%",transition: 'font-size 0.5s'  };
...
<FaFacebookSquare style={style} onMouseEnter={({target})=>target.style.fontSize= "180%"} 
  onMouseLeave={({target})=>target.style.fontSize= "100%"}/>

或者您可以将它们分成 Component<StyledIcon/>然后useRefuseEffect然后useState进行悬停。

const style = { fontSize: "180%",transition: 'font-size 0.5s' };
export function StyledIcon(props){
const [hover,setHover] = useState(false)
const iconRef = useRef(null)

  useEffect(()=>{
    if(hover){
      iconRef.current.style.fontSize="200%"
    }
    else{
      iconRef.current.style.fontSize="180%"
    }
  },[hover]) // changing fontSize whenever the hover state is updated
const handleIconType = ()=>{
    switch(props.type){
      case 'facebook':
        {
          return <FaFacebookSquare style={style} ref={iconRef} onMouseEnter={()=>{setHover(true)}} onMouseLeave={()=>{setHover(false)}}/>
        }
      ...// cases for different type of icon
      default:
        return null
    }
  }
  return(
  <>
     <FaFacebookSquare style={style} ref={iconRef} onMouseEnter={()=>{setHover(true)}} onMouseLeave={()=>{setHover(false)}}/>
  </>
  )
}
export const BottomBar = () => {
  
  return (
    <StyledBottomBar>
      <StyledIcon type="facebook">
      </StyledIcon>
      <StyledIcon type="instagram">
      </StyledIcon>
    </StyledBottomBar>
  );
};
于 2021-06-08T22:42:07.337 回答