我正在尝试使用 CSSTransitionGroup (或 ReactTransitionGroup)为内容在 dom 中出现和消失时创建垂直平滑滚动效果。
我知道在纯 css 中使用过渡来动画化这种效果的方法,但是当一个项目出现在 DOM 中然后离开 DOM 时,我需要能够在 React 中执行此操作,因此 CSSTransitionGroup 似乎是正确的解决方案。
虽然我能够对颜色和不透明度等属性进行动画处理,但我未能成功对高度和/或最大高度等属性进行动画处理以实现内容从上到下或从下到上逐渐消失。
当项目进入和离开 DOM 时,如何制作像这样的平滑滚动效果的动画?我当前使用 CSSTransitionGroup 的代码如下:
class App extends React.Component {
state = {
random: true
};
toggleItem = () => {
this.setState({
random: !this.state.random
});
};
render() {
console.log("random", this.state.random);
return (
<div>
<button onClick={this.toggleItem}>toggle item</button>
<br />
<CSSTransition
in={this.state.random}
timeout={400}
classNames="alert"
unmountOnExit
appear
enter={false}
>
<div class="back">
Hello adsffd asdfadfs asdfasd asdfasdf asdfasfa fdasfas asdfasdf
afdsafas asdfasd asdfasdf asdfadsf asdfads asdfads asdfasdf
asdfadsadsf world Hello adsffd asdfadfs asdfasd asdfasdf asdfasfa
fdasfas asdfasdf afdsafas asdfasd asdfasdf asdfadsf asdfads asdfads
asdfasdf asdfadsadsf world Hello adsffd asdfadfs asdfasd asdfasdf
asdfasfa fdasfas asdfasdf afdsafas asdfasd asdfasdf asdfadsf asdfads
asdfads asdfasdf asdfadsadsf world Hello adsffd asdfadfs asdfasd
asdfasdf asdfasfa fdasfas asdfasdf afdsafas asdfasd asdfasdf
</div>
</CSSTransition>
</div>
);
}
}
export default App;
我当前的输入状态类别是:
.alert-enter {
height: 0px;
visibility: hidden;
overflow: hidden;
}
.alert-enter-active {
height: auto;
overflow: auto;
visibility: visible;
transition: all 300ms;
}
拉莫斯之舞
相关分类