添加CSS过渡到项目的不断增多

问题描述 投票:0回答:1

我有一个与项目/箱空单开出,并作为列表的增长,以前的项目在阙推倒的组成部分。你可以看到下面的代码片段的例子。如何添加CSS过渡,这样你可以直观地看到项目从原来的位置移动到下一个位置了吗?谢谢!

class App extends React.Component {
    constructor() {
    super();
    this.state = {
      count: 0
    };
    this.handleClick = this.handleClick.bind(this);
    this.getItems = this.getItems.bind(this);
  }

  handleClick() {
    this.setState({
      count: this.state.count + 1,
    });
  }
  
  getItems() {
    let items = [];
    for (let j = 0; j < this.state.count; j++) {
      items.push(<div className="item">This is a box {j}</div>)
    }
    return items.reverse();
  };

  render() {
    return (
      <div className="container">
        <button onClick={this.handleClick}>Add box</button>
        {this.getItems()}
      </div>
    );
  }
}



ReactDOM.render(<App />, app);
.container {
  border: 1px solid green !important;
}


.item {
  height: 40px;
  width: 100px;
  background: yellow;
  border: 2px solid red;
  margin: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
javascript html css reactjs
1个回答
1
投票

您可以在最大高度使用动画;但是你要知道你的.item,最大高度的尺寸:100%不工作的动画。

class App extends React.Component {
    constructor() {
    super();
    this.state = {
      count: 0
    };
    this.handleClick = this.handleClick.bind(this);
    this.getItems = this.getItems.bind(this);
  }

  handleClick() {
    this.setState({
      count: this.state.count + 1,
    });
  }
  
  getItems() {
    let items = [];
    for (let j = 0; j < this.state.count; j++) {
      items.push(<div className="item">This is a box {j}</div>)
    }
    return items.reverse();
  };

  render() {
    return (
      <div className="container">
        <button onClick={this.handleClick}>Add box</button>
        {this.getItems()}
      </div>
    );
  }
}



ReactDOM.render(<App />, app);
.container {
  border: 1px solid green !important;
}


.item {
  height: 40px;
  width: 100px;
  background: yellow;
  border: 2px solid red;
  margin: 20px;

  animation-name: example;
  animation-duration: 2s;
  animation-iteration-count: 1;
  overflow: hidden;
}
@keyframes example {
  from { max-height: 0px;}
  to { max-height: 40px;}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
© www.soinside.com 2019 - 2024. All rights reserved.