likes
comments
collection
share

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

作者站长头像
站长
· 阅读数 7
转载请注明出处,未经同意,不可修改文章内容。

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

1 React 中使用 CSS3 过渡效果

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

import React, { Component, Fragment } from "react";  

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

export default App;

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

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

打开 App.js 文件:

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 名字真的随“点击”在不停的改变): (31)React 拓展——① React 中使用 CSS3 过渡和动画 | React 基础理论实操

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

5️⃣-①:在 src 目录下创建一个 style.css 文件; (31)React 拓展——① React 中使用 CSS3 过渡和动画 | React 基础理论实操

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

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》

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

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

返回页面查看效果: (31)React 拓展——① React 中使用 CSS3 过渡和动画 | React 基础理论实操

2 React 中使用 CSS3 动画效果

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

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

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

.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;
  }
}

返回页面查看效果: (31)React 拓展——① React 中使用 CSS3 过渡和动画 | React 基础理论实操

祝好,qdywxs ♥ you!