• React过渡动画


    1.react-transition-group介绍

    对于实现一个组件的显示与消失的过渡动画,可以通过原生的CSS来实现这些过渡动画,但是React社区为我们提供了react-transition-group库用来完成过渡动画。

    # npm
    npm install react-transition-group --save
    # yarn
    yarn 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:退出后卸载组件
    1. import React, { createRef, PureComponent } from 'react'
    2. import { CSSTransition } from "react-transition-group"
    3. import "./style.css"
    4. export class App extends PureComponent {
    5. constructor(props) {
    6. super(props)
    7. this.state = {
    8. isShow: true
    9. }
    10. this.sectionRef = createRef()
    11. }
    12. render() {
    13. const { isShow } = this.state
    14. return (
    15. <div>
    16. <button onClick={e => this.setState({isShow: !isShow})}>切换button>
    17. {/* { isShow && <h2>哈哈哈h2> } */}
    18. <CSSTransition
    19. nodeRef={this.sectionRef}
    20. in={isShow}
    21. unmountOnExit={true}
    22. classNames="why"
    23. timeout={2000}
    24. appear
    25. onEnter={e => console.log("开始进入动画")}
    26. onEntering={e => console.log("执行进入动画")}
    27. onEntered={e => console.log("执行进入结束")}
    28. onExit={e => console.log("开始离开动画")}
    29. onExiting={e => console.log("执行离开动画")}
    30. onExited={e => console.log("执行离开结束")}
    31. >
    32. <div className='section' ref={this.sectionRef}>
    33. <h2>哈哈哈h2>
    34. <p>我是内容, 哈哈哈p>
    35. div>
    36. CSSTransition>
    37. div>
    38. )
    39. }
    40. }
    41. export default App
    1. .why-appear, .why-enter {
    2. opacity: 0;
    3. }
    4. .why-appear-active, .why-enter-active {
    5. opacity: 1;
    6. transition: opacity 2s ease;
    7. }
    8. /* 离开动画 */
    9. .why-exit {
    10. opacity: 1;
    11. }
    12. .why-exit-active {
    13. opacity: 0;
    14. transition: opacity 2s ease;
    15. }
    2.3 SwitchTransition

    SwitchTransition可以完成两个组件之间切换的炫酷动画:

    比如我们有一个按钮需要在on和off之间切换,我们希望看到on先从左侧退出,off再从右侧进入;
    SwitchTransition中主要有一个属性:mode,有两个值
    • in-out:表示新组件先进入,旧组件再移除;
    • out-in:表示就组件先移除,新组建再进入;
    1. import React, { PureComponent } from 'react'
    2. import { SwitchTransition, CSSTransition } from 'react-transition-group'
    3. import "./style.css"
    4. export class App extends PureComponent {
    5. constructor() {
    6. super()
    7. this.state = {
    8. isLogin: true
    9. }
    10. }
    11. render() {
    12. const { isLogin } = this.state
    13. return (
    14. <div>
    15. <SwitchTransition mode='out-in'>
    16. <CSSTransition
    17. key={isLogin ? "exit": "login"}
    18. classNames="login"
    19. timeout={1000}
    20. >
    21. <button onClick={e => this.setState({ isLogin: !isLogin })}>
    22. { isLogin ? "退出": "登录" }
    23. button>
    24. CSSTransition>
    25. SwitchTransition>
    26. div>
    27. )
    28. }
    29. }
    30. export default App
    1. .login-enter {
    2. transform: translateX(100px);
    3. opacity: 0;
    4. }
    5. .login-enter-active {
    6. transform: translateX(0);
    7. opacity: 1;
    8. transition: all 1s ease;
    9. }
    10. .login-exit {
    11. transform: translateX(0);
    12. opacity: 1;
    13. }
    14. .login-exit-active {
    15. transform: translateX(-100px);
    16. opacity: 0;
    17. transition: all 1s ease;
    18. }
    2.4 TransitionGroup

    将多个动画放到一组动画里执行,通过TransitionGroup组件实现;

    例如书籍列表的添加与删除动画,为每个书籍项添加新增与删除动画;

    1. import React, { PureComponent } from 'react'
    2. import { TransitionGroup, CSSTransition } from "react-transition-group"
    3. import "./style.css"
    4. export class App extends PureComponent {
    5. constructor() {
    6. super()
    7. this.state = {
    8. books: [
    9. { id: 111, name: "你不知道JS", price: 99 },
    10. { id: 222, name: "JS高级程序设计", price: 88 },
    11. { id: 333, name: "Vuejs高级设计", price: 77 },
    12. ]
    13. }
    14. }
    15. addNewBook() {
    16. const books = [...this.state.books]
    17. books.push({ id: new Date().getTime(), name: "React高级程序设计", price: 99 })
    18. this.setState({ books })
    19. }
    20. removeBook(index) {
    21. const books = [...this.state.books]
    22. books.splice(index, 1)
    23. this.setState({ books })
    24. }
    25. render() {
    26. const { books } = this.state
    27. return (
    28. <div>
    29. <h2>书籍列表:h2>
    30. <TransitionGroup component="ul">
    31. {
    32. books.map((item, index) => {
    33. return (
    34. <CSSTransition key={item.id} classNames="book" timeout={1000}>
    35. <li>
    36. <span>{item.name}-{item.price}span>
    37. <button onClick={e => this.removeBook(index)}>删除button>
    38. li>
    39. CSSTransition>
    40. )
    41. })
    42. }
    43. TransitionGroup>
    44. <button onClick={e => this.addNewBook()}>添加新书籍button>
    45. div>
    46. )
    47. }
    48. }
    49. export default App
    1. .book-enter {
    2. transform: translateX(150px);
    3. opacity: 0;
    4. }
    5. .book-enter-active {
    6. transform: translateX(0);
    7. opacity: 1;
    8. transition: all 1s ease;
    9. }
    10. .book-exit {
    11. transform: translateX(0);
    12. opacity: 1;
    13. }
    14. .book-exit-active {
    15. transform: translateX(150px);
    16. opacity: 0;
    17. transition: all 1s ease;
    18. }

  • 相关阅读:
    可执行文件的装载与进程
    水电站与数据可视化:洞察未来能源趋势的窗口
    SpringMVC中如何使用注解的方式实现文件上传呢?
    maven根据操作系统的不同打包时引入不同的依赖(jar)
    记录一次开机内存分析的全过程
    Docker 在 M1 Mac arm64架构上构建 amd64镜像。
    VLAN技术 — Super VLAN
    自动驾驶——估计预瞄轨迹YawRate
    react antd 主题
    图片去水印免费软件哪个好?这几款软件值得一看
  • 原文地址:https://blog.csdn.net/weixin_47342624/article/details/133748329