react顯示隱藏動畫


通過className的true或者false控制元素顯示或者隱藏。

App.js

import React, { Component } from 'react';
import './App.css'
class App extends Component {
  constructor(props) {
    super(props);
    this.state = { 
        show:true,
        text:'隱藏'
     }
  }
  render() { 
    return ( 
      <div>
        <p className={this.state.show?'show':'hide'}>文字</p>
        <button onClick={this.toggle}>{this.state.text}</button>
      </div>
     );
  }
  toggle =()=>{
    var show = this.state.show;
    var text = this.state.text;
    if(show){
      show =false
      text = '顯示'
    }else{
      show = true 
      text = '隱藏'
    }
    this.setState({
      show:show,
      text:text
    })
  }
}
 
export default App;

 

App.css

.show{
  opacity: 1;
  transition: .5s all ease-in;
}

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM