[三元学React]使用react-transition-group开发React动画

栏目: IOS · Android · 发布时间: 6年前

内容简介:首先利用CSSTransition进行单个元素的动画开发。今天解锁了react使用动画的新姿势,记录一下,希望对大家有帮助。

首先利用CSSTransition进行单个元素的动画开发。

//App.js
import { CSSTransition } from 'react-transition-group'
import React, { Component } from 'react';
import './style.css'
class App extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      isShow: true
    }
    this.toToggole = this.toToggole.bind(this)
  }
  render() { 
    return ( 
      <div>
        <CSSTransition
            <!-- in表示是否出现 timeout表示动画延时 -->
            in={this.state.isShow}
            timeout={2000}
            <!-- classNames是钩子名,为后面的class名前缀 -->
            classNames="test"
            <!-- unmountOnExit表示元素隐藏则相应的DOM被移除 -->
            unmountOnExit
            <!-- appear设为true表示进场动画,CSS中有对应类名 -->
            appear={true}
            <!--以下为动画钩子函数, 与CSS中相对应-->
            onEnter={(el) => {}}
            onEntering={(el) => {}}
            onEntered={(el) => {}}
            onExit={(el) => {}}
            onExiting={(el) => {}}
            onExited={(el) => {}}
        >
          <div>hello</div>
        </CSSTransition>
        <div><button onClick={this.toToggole}>点我</button></div>
      </div>  
    );
  }
  toToggole() {
    this.setState({
      isShow: !this.state.isShow
    })
  }
}
 
export default App;
复制代码
//进场前的瞬间
.test-enter, .test-appear{
    opacity: 0;
}
//进场过程中
.test-enter-active, .test-appear-active{
    opacity: 1;
    transition: opacity 2000ms;
}
//进场之后
.test-enter-done{
    opacity: 1;
}
//离开前的瞬间
.test-exit{
    opacity: 1;
}
//离开过程中
.test-exit-active{
    opacity: 0;
    transition: opacity 2000ms;
}
//离开后
.test-exit-done{
    opacity: 0;
}
复制代码

三、对一组元素进行动画操作

//App.js
//CSS文件和style.css相同
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import React, { Component } from 'react';
import './style.css'
class App extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      list: []
    }
    this.handleAddItem = this.handleAddItem.bind(this)
  }
  render() { 
    return ( 
      <div>
        <TransitionGroup>
        {
          this.state.list.map((item, index) => {
            return (
              <CSSTransition
              timeout={2000}
              classNames="test"
              unmountOnExit
              onEntered={(el) => {el.style.color='blue'}}
              appear={true}
              >
                <div key={index}>{item}</div>
              </CSSTransition>
            )
          })
        }
        </TransitionGroup>
        <div><button onClick={this.handleAddItem}>点我</button></div>
      </div>  
    );
  }
  handleAddItem() {
    this.setState((prevState) => {
      return {
        list: [...prevState.list, 'item']
      }
    })
  }
}
 
export default App;
复制代码

今天解锁了react使用动画的新姿势,记录一下,希望对大家有帮助。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Probabilistic Method Second Edition

The Probabilistic Method Second Edition

Noga Alon、Joel H. Spencer / Wiley-Blackwell / 2000 / $121.95

The leading reference on probabilistic methods in combinatorics-now expanded and updated When it was first published in 1991, The Probabilistic Method became instantly the standard reference on one......一起来看看 《The Probabilistic Method Second Edition》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具