在组件上不显示 React 微动画

Fre*_*dy. 3 javascript css css-transitions css-animations reactjs

我让这个组件出现在按钮单击上。我想知道扩展或弹出动画是否有简单的缓解方法。为了让它在过渡中看起来更平滑而不是出现?

const CardExample = () => {
  const [show, setShow] = useState(false);

  const handleClick = () => {
    setShow(!show);
  };

  return (
    <div>
      <Button onClick={handleClick} type="primary">
        Show Card
      </Button>
      <Card style={{ display: show && "none" }}>This is my card</Card>
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)

编辑 lucid-star-fs6zi

Sum*_*mit 5

您可以尝试使用opacity而不是像下面那样在此处显示并使用transition来添加动画

const CardExample = () => {
  const [show, setShow] = useState(false);

  const handleClick = () => {
    setShow(!show);
  };

  const cardStyle = {
    opacity: show ? 0 : 1,
    transition: "all 1s ease-in"
  };

  return (
    <div>
      <Button onClick={handleClick} type="primary">
        Show Card
      </Button>
      <Card style={cardStyle}>This is my card</Card>
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)

编辑 自信莫尔斯 phg31