React过渡动画

news/2024/7/15 17:14:58 标签: react.js, 前端, javascript

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:退出后卸载组件
javascript">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.state

    return (
      <div>
        <button onClick={e => this.setState({isShow: !isShow})}>切换</button>
        {/* { isShow && <h2>哈哈哈</h2> } */}

        <CSSTransition 
          nodeRef={this.sectionRef}
          in={isShow} 
          unmountOnExit={true} 
          classNames="why" 
          timeout={2000}
          appear
          onEnter={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:表示就组件先移除,新组建再进入;
javascript">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.state

    return (
      <div>
        <SwitchTransition mode='out-in'>
          <CSSTransition
            key={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组件实现;

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

javascript">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.state

    return (
      <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;
}


http://www.niftyadmin.cn/n/5082136.html

相关文章

Leetcode.2867 统计树中的合法路径数目

题目链接 Leetcode.2867 统计树中的合法路径数目 rating : 2428 题目描述 给你一棵 n n n 个节点的无向树&#xff0c;节点编号为 1 1 1 到 n n n 。给你一个整数 n n n 和一个长度为 n − 1 n - 1 n−1 的二维整数数组 e d g e s edges edges &#xff0c;其中 e d g …

css修改滚动条

系统默认的滚动条可能不是很美观&#xff0c;有时候我们想要自己进行样式的修改&#xff0c;就可以用到下面的方法 设置滚动条的属性为overflow&#xff0c;取值是x轴和y轴 如果想单独设置某个轴&#xff0c;可以使用overflow-x,overflow-y <!DOCTYPE html> <html …

Linux网络和系统管理

网络管理命令 1、ifconfig 命令 作用 ifconfig 命令用于显示或设置网络设备的信息。格式 ifconfig [网卡名字] [参数]可选项 网卡名字:指定要操作的网络设备。参数: up:启动指定网卡。down:关闭指定网卡。-a:显示所有网卡接口的信息,包括未激活的网卡接口。使用示例 1…

OpenCV7-copyTo截取ROI

OpenCV7-copyTo截取ROI copyTo截取感兴趣区域 copyTo截取感兴趣区域 有时候&#xff0c;我们只对一幅图像中的部分区域感兴趣&#xff0c;而原图像又十分大&#xff0c;如果带着非感兴趣区域一次处理&#xff0c;就会对程序的内存造成负担&#xff0c;因此我们希望从原始图像中…

性能分析工具的使用(超详细)

数据库服务器的优化步骤 整个流程划分成了观察&#xff08;Show status&#xff09;和行动&#xff08;Action&#xff09;两个部分。字母 S 的部分代表观察&#xff08;会使用相应的分析工具&#xff09;&#xff0c;字母 A 代表的部分是行动&#xff08;对应分析可以采取的行…

小程序uView2.X框架upload组件上传方法总结+避坑

呈现效果: 1.1单图片上传 1.2多图片上传 前言:相信很多人写小程序会用到uView框架,总体感觉还算OK吧,只能这么说,肯定也会遇到图片视频上传,如果用到这个upload组件相信你,肯定遇到各种各样的问题,这是我个人总结的单图片和多图片上传方法. uView2.X框架:uView 2.0 - 全面兼容…

新晋国产证书品牌——JoySSL

JoySSL是一家国产SSL证书服务厂商&#xff0c;拥有着国内外绝大数主流证书品牌&#xff0c;还有自己自主品牌JoySSL&#xff0c;遍寻诚意伙伴&#xff0c;单域名证书都是不限年限、不限证书张数不限量提供的&#xff0c;目前我了解到的市面上基本上所有的品牌都最多提供免费的单…

项目管理之常见七大问题挑战

在当今复杂多变的市场环境下&#xff0c;企业为了生存和发展&#xff0c;必须不断应对和解决各种挑战。其中&#xff0c;项目管理作为企业运营及项目交付等的重要组成部分&#xff0c;也面临着七大问题挑战。这些挑战不仅影响着项目的成功实施&#xff0c;也对企业的发展产生着…