无法使用 CSSTransition Group 创建平滑滚动

我正在尝试使用 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;

}


UYOU
浏览 137回答 1
1回答

拉莫斯之舞

您可以添加一个白色 div 和过渡来包裹您的文本 divclass App extends Component {state = {&nbsp; &nbsp; random: false&nbsp; };&nbsp; toggleItem = () => {&nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; random: !this.state.random&nbsp; &nbsp; });&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <button onClick={this.toggleItem}>toggle item</button>&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <div className="back">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hello adsffd asdfadfs asdfasd asdfasdf asdfasfa fdasfas asdfasdf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; afdsafas asdfasd asdfasdf asdfadsf asdfads asdfads asdfasdf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asdfadsadsf world Hello adsffd asdfadfs asdfasd asdfasdf asdfasfa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fdasfas asdfasdf afdsafas asdfasd asdfasdf asdfadsf asdfads asdfads&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asdfasdf asdfadsadsf world Hello adsffd asdfadfs asdfasd asdfasdf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asdfasfa fdasfas asdfasdf afdsafas asdfasd asdfasdf asdfadsf asdfads&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asdfads asdfasdf asdfadsadsf world Hello adsffd asdfadfs asdfasd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asdfasdf asdfasfa fdasfas asdfasdf afdsafas asdfasd asdfasdf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CSSTransition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in={this.state.random}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeout={1000}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; classNames="alert"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unmountOnExit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div className="white" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </CSSTransition>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}样式.css.alert-enter {&nbsp; transform: translateY(400px);}.alert-enter-active {&nbsp; transform: translateY(0px);&nbsp; transition: all 1000ms;}.alert-exit {&nbsp; transform: translateY(0px);}.alert-exit-active {&nbsp; transform: translateY(800px);&nbsp; transition: all 3000ms;}.back {&nbsp; position: relative;&nbsp; overflow: hidden;}.white {&nbsp; height: 100%;&nbsp; position: absolute;&nbsp; top: 0;&nbsp; left: 0;&nbsp; background: white;&nbsp; width: 100%;}您可以在这里检查CodeSandBox。希望能帮助到你
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5