oem*_*era 3 animation reactjs react-spring react-hooks
以下来自官方示例的简单组件:
import {useSpring, animated} from 'react-spring'
function App() {
const props = useSpring({opacity: 1, from: {opacity: 0}})
return <animated.div style={props}>I will fade in</animated.div>
}
Run Code Online (Sandbox Code Playgroud)
问题
如何fadeIn再次为效果(或任何其他动画)设置动画,例如当我单击按钮或解决承诺时?
您基本上可以使用 useSpring 和事件来实现两种效果。
您可以根据事件的状态更改样式,例如不透明度。
您可以在状态更改时重新启动动画。重新启动最简单的方法是重新渲染它。
我创建了一个例子。我想你想要第二种情况。在我的示例中,我通过更改其关键属性来重新渲染第二个组件。
const Text1 = ({ on }) => {
const props = useSpring({ opacity: on ? 1 : 0, from: { opacity: 0 } });
return <animated.div style={props}>I will fade on and off</animated.div>;
};
const Text2 = () => {
const props = useSpring({ opacity: 1, from: { opacity: 0 } });
return <animated.div style={props}>I will restart animation</animated.div>;
};
function App() {
const [on, set] = React.useState(true);
return (
<div className="App">
<Text1 on={on} />
<Text2 key={on} />
<button onClick={() => set(!on)}>{on ? "On" : "Off"}</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
这是工作示例:https://codesandbox.io/s/upbeat-kilby-ez7jy
我希望这就是你的意思。
| 归档时间: |
|
| 查看次数: |
7042 次 |
| 最近记录: |