(31)React 拓展——① React 中使用 CSS3 过渡和动画 | React 基础理论实操

复制代码
转载请注明出处,未经同意,不可修改文章内容。

🔥🔥🔥"前端一万小时"两大明星专栏------"从零基础到轻松就业"、"前端面试刷题",已于本月大改版,合二为一,干货满满,欢迎点击公众号菜单栏各模块了解。

1 React 中使用 CSS3 过渡效果

❗️为了便于讲解,请将代码切换至《React 初识------③ React 中的组件和 JSX 初识》的版本!

进入 App.js 文件,再写一个 toggle 按钮:

jsx 复制代码
import React, { Component, Fragment } from "react";  

class App extends Component  {
  render() {
    return (
      <Fragment>
        <div>
          hello, qdywxs.
        </div>
        <button>toggle</button>
      </Fragment>
    );
  }
}

export default App;

❓需求:我希望点击 toggle 按钮时, hello, qdywxs. 能有一个"展示"和"隐藏"的效果。 答:

打开 App.js 文件:

jsx 复制代码
import React, { Component, Fragment } from "react";  

class App extends Component  {
  constructor(props) { 
    super(props);
    
    this.state = {
      show: true // 1️⃣首先,定义一个状态 show,默认是"显示"的;
    }
    
    this.handleBtnToggle = this.handleBtnToggle.bind(this); // ❗️在这里改变 this 指向!
  }
  
  render() {
    return (
      <Fragment>
      
        {/*
         2️⃣给 div 标签添加一个 className 属性:
         如果 show 是 true,则属性值为 show;
         如果 show 为 false,则属性值为 hide。
          */}
        <div className={this.state.show ? "show" : "hide"}>hello, qdywxs.</div>

				{/* 3️⃣给按钮绑定一个"点击"事件(❗️记得在上边去改变 this 指向); */}
        <button onClick={this.handleBtnToggle}>toggle</button>
      </Fragment>
    );
  }
  
  // 4️⃣将事件方法写在这里(❗️注意看逻辑是怎样的~);
  handleBtnToggle() {
    this.setState({
      show: this.state.show ? false : true
    })
  }
}

export default App;

返回页面查看控制台( div 元素的 class 名字真的随"点击"在不停的改变):

5️⃣既然实现了 class 名字的切换,我们就可以借助 class 名字来实现动画效果:

5️⃣-①:在 src 目录下创建一个 style.css 文件;

5️⃣-②:然后在 App.js 文件中去引入这个样式;

jsx 复制代码
import React, { Component, Fragment } from "react";  

import "./style.css"; // ❗️引入样式!

class App extends Component  {
  constructor(props) { 
    super(props);
    
    this.state = {
      show: true  
    }
    
    this.handleBtnToggle = this.handleBtnToggle.bind(this); 
  }
  
  render() {
    return (
      <Fragment>

        <div className={this.state.show ? "show" : "hide"}>hello, qdywxs.</div>

        <button onClick={this.handleBtnToggle}>toggle</button>
      </Fragment>
    );
  }
  
  handleBtnToggle() {
    this.setState({
      show: this.state.show ? false : true
    })
  }
}

export default App;

5️⃣-③:在 style.css 文件中编写样式代码;

🔗前置知识:《CSS------CSS 拓展:⑤ CSS3 Transition》

css 复制代码
.show {
  opacity: 1;
  transition: all 1s ease-in; /* ❗️其他"过渡"样式,大家可以自己尝试! */
} 

.hide {
  opacity: 0;
  transition: all 1s ease-in;
}

返回页面查看效果:

2 React 中使用 CSS3 动画效果

❓需求:我想让"隐藏"的效果更丰富些应该怎么做呢? 答:

紧接上边的代码,打开 style.css 文件编写动画效果。

🔗前置知识:《CSS------CSS 拓展:⑦ CSS3 Animation》

css 复制代码
.show {
  animation: show-item 2s ease-in forwards;
}

.hide {
  animation: hide-item 2s ease-in forwards;
}

@keyframes show-item {
  0% {
    opacity: o;
    color: red;
  }
  50% {
    opacity: 0.5;
    color: green;
  }
  100% {
    opacity: 1;
    color: blue;
  }
}

@keyframes hide-item {
  0% {
    opacity: 1;
    color: red;
  }
  50% {
    opacity: 0.5;
    color: green;
  }
  100% {
    opacity: 0;
    color: blue;
  }
}

返回页面查看效果:

祝好,qdywxs ♥ you!

相关推荐
海兰25 分钟前
【web应用】Excel 项目数据自动化分析系统(AI 驱动分析)详细设计与部署指南(附源代码)
前端·人工智能·自动化·excel
小二·27 分钟前
Next.js 15 全栈开发实战
开发语言·javascript·ecmascript
2501_9400417428 分钟前
技术分享:高质量全栈开发提示词设计实践 —— 覆盖简单到复杂
前端·prompt
IT_陈寒1 小时前
Python的os.path.join居然能这么坑?
前端·人工智能·后端
艳阳天_.1 小时前
星瀚弹框页面实现
java·前端·python
EdgeOne边缘安全加速平台1 小时前
EdgeOne Web 防护×AI 升级:让 AI 既参与攻击识别,也参与误报纠错
前端·人工智能·腾讯云·edgeone
nuIl1 小时前
实现一个 Coding Agent(6):并行工具调用
前端·ai编程·cursor
Rain5091 小时前
2.1 Nest.js 项目初始化与模块化架构
开发语言·前端·javascript·后端·架构·数据分析·node.js
cjp5601 小时前
009. ASP.NET WEB API 用户关联esp32设备
前端·后端·asp.net
Insseals2 小时前
因斯特浮动模块快速接头✨五大核心优势
前端