[英]Display none to React micro animations on component
I have this component appear on button click.我有这个组件出现在按钮点击。 I was wondering whether there is a simple ease in then expand or popout animation.
我想知道扩展或弹出动画是否有简单的易用性。 To make it look smoother in transition rather than appearing?
让它在过渡中看起来更平滑而不是出现?
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>
);
};
You can try using opacity
instead of display here like below and use transition
to add the animation您可以尝试使用
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>
);
};
The previous answer seems to get the job done, but I wrote up another way to do this with CSS instead of inline styles.上一个答案似乎完成了工作,但我写了另一种使用 CSS 而不是内联样式来做到这一点的方法。 You can conditionally apply a
className
and then handle the transition in a css file.您可以有条件地应用
className
,然后在 css 文件中处理转换。 The sandbox URL is at the bottom of this post.沙箱 URL 位于本文底部。
import './CardExample.css';
const CardExample = () => {
const [show, setShow] = useState(false);
const handleClick = () => {
setShow(!show);
};
return (
<div className='card-example'>
<button onClick={handleClick} type="primary">
Show Card
</button>
<Card className={show ? 'showing' : 'not-showing'}>
This is my card
</Card>
</div>
);
};
and then in the css file, you can have the following, which conditionally applies the visibility attribute on a 1 second animation interval.然后在 css 文件中,您可以拥有以下内容,它有条件地以 1 秒动画间隔应用可见性属性。
.not-showing {
visibility: hidden;
opacity: 0;
transition: visibility 1s, opacity 0.5s linear;
}
.showing {
visibility: visible;
transition: visibility 1s, opacity 0.5s linear;
opacity: 1;
}
I created a sandbox to demonstrate this:我创建了一个沙箱来演示这一点:
https://codesandbox.io/s/react-hooks-playground-boih3?fontsize=14&hidenavigation=1&theme=dark https://codesandbox.io/s/react-hooks-playground-boih3?fontsize=14&hidenavigation=1&theme=dark
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.