1.react-transition-group介绍
对于实现一个组件的显示与消失的过渡动画,可以通过原生的CSS来实现这些过渡动画,但是React社区为我们提供了react-transition-group库用来完成过渡动画。
# npmnpm install react-transition-group --save# yarnyarn add react-transition-group
2.react-transition-group主要组件
- Transition
- CSSTransition
- SwitchTransition
- TransitionGroup
2.1 CSSTransition
CSSTransition是基于Transition组件构建的;
CSSTransition执行过程中,有三个状态:appear、enter、exit;
对应三种状态:
- 第一类, 开始状态 :对于的类是-appear、-enter、exit;
- 第二类: 执行动画 :对应的类是-appear-active、-enter-active、-exit-active;
- 第三类:执行结束:对应的类是-appear-done、-enter-done、-exit-done;
2.2 CSSTransition常见对应的属性
in:触发进入或者退出状态
- 如果添加了unmountOnExit={true},那么该组件会在执行退出动画结束后被移除掉;
- 当in为true时,触发进入状态,会添加-enter、-enter-acitve的class开始执行动画,当动画执行结束后,会移除两个class,并且添加-enter-done的class;
- 当in为false时,触发退出状态,会添加-exit、-exit-active的class开始执行动画,当动画执行结束后,会移除两个class,并且添加-enter-done的class;
classNames:动画class的名称
- 决定了在编写css时,对应的class名称:比如card-enter、card-enter-active、card-enter-done;
timeout:过渡动画的时间;
appear: 是否在初次进入添加动画(需要和in同时为true) ;
unmountOnExit:退出后卸载组件 ;
import React, { createRef, PureComponent } from 'react'
import { CSSTransition } from "react-transition-group"
import "./style.css"export class App extends PureComponent {constructor(props) {super(props)this.state = {isShow: true}this.sectionRef = createRef()}render() {const { isShow } = this.statereturn (<div><button onClick={e => this.setState({isShow: !isShow})}>切换</button>{/* { isShow && <h2>哈哈哈</h2> } */}<CSSTransition nodeRef={this.sectionRef}in={isShow} unmountOnExit={true} classNames="why" timeout={2000}appearonEnter={e => console.log("开始进入动画")}onEntering={e => console.log("执行进入动画")}onEntered={e => console.log("执行进入结束")}onExit={e => console.log("开始离开动画")}onExiting={e => console.log("执行离开动画")}onExited={e => console.log("执行离开结束")}><div className='section' ref={this.sectionRef}><h2>哈哈哈</h2><p>我是内容, 哈哈哈</p></div></CSSTransition></div>)}
}export default App
.why-appear, .why-enter {opacity: 0;
}.why-appear-active, .why-enter-active {opacity: 1;transition: opacity 2s ease;
}/* 离开动画 */
.why-exit {opacity: 1;
}.why-exit-active {opacity: 0;transition: opacity 2s ease;
}
2.3 SwitchTransition
SwitchTransition可以完成两个组件之间切换的炫酷动画:
比如我们有一个按钮需要在on和off之间切换,我们希望看到on先从左侧退出,off再从右侧进入;
SwitchTransition中主要有一个属性:mode,有两个值
- in-out:表示新组件先进入,旧组件再移除;
- out-in:表示就组件先移除,新组建再进入;
import React, { PureComponent } from 'react'
import { SwitchTransition, CSSTransition } from 'react-transition-group'
import "./style.css"export class App extends PureComponent {constructor() {super() this.state = {isLogin: true}}render() {const { isLogin } = this.statereturn (<div><SwitchTransition mode='out-in'><CSSTransitionkey={isLogin ? "exit": "login"}classNames="login"timeout={1000}><button onClick={e => this.setState({ isLogin: !isLogin })}>{ isLogin ? "退出": "登录" }</button></CSSTransition></SwitchTransition></div>)}
}export default App
.login-enter {transform: translateX(100px);opacity: 0;
}.login-enter-active {transform: translateX(0);opacity: 1;transition: all 1s ease;
}.login-exit {transform: translateX(0);opacity: 1;
}.login-exit-active {transform: translateX(-100px);opacity: 0;transition: all 1s ease;
}
2.4 TransitionGroup
将多个动画放到一组动画里执行,通过TransitionGroup组件实现;
例如书籍列表的添加与删除动画,为每个书籍项添加新增与删除动画;
import React, { PureComponent } from 'react'
import { TransitionGroup, CSSTransition } from "react-transition-group"
import "./style.css"export class App extends PureComponent {constructor() {super()this.state = {books: [{ id: 111, name: "你不知道JS", price: 99 },{ id: 222, name: "JS高级程序设计", price: 88 },{ id: 333, name: "Vuejs高级设计", price: 77 },]}}addNewBook() {const books = [...this.state.books]books.push({ id: new Date().getTime(), name: "React高级程序设计", price: 99 })this.setState({ books })}removeBook(index) {const books = [...this.state.books]books.splice(index, 1)this.setState({ books })}render() {const { books } = this.statereturn (<div><h2>书籍列表:</h2><TransitionGroup component="ul">{books.map((item, index) => {return (<CSSTransition key={item.id} classNames="book" timeout={1000}><li><span>{item.name}-{item.price}</span><button onClick={e => this.removeBook(index)}>删除</button></li></CSSTransition>)})}</TransitionGroup><button onClick={e => this.addNewBook()}>添加新书籍</button></div>)}
}export default App
.book-enter {transform: translateX(150px);opacity: 0;
}.book-enter-active {transform: translateX(0);opacity: 1;transition: all 1s ease;
}.book-exit {transform: translateX(0);opacity: 1;
}.book-exit-active {transform: translateX(150px);opacity: 0;transition: all 1s ease;
}