react-transition-group-CSSTransition 的使用
阿新 • • 發佈:2018-12-03
eg:app.js
import React, { Component,Fragment } from 'react'; import { CSSTransition } from 'react-transition-group'; import logo from './logo.svg'; import './App.css'; class App extends Component { constructor(props){ super(props); this.state={ show:true } this.handleToggole=this.handleToggole.bind(this) } render() { // render函式裡面都是jsx語法-需要用reat return ( <Fragment> <CSSTransition in={this.state.show} timeout={1000} classNames='fade' unmountOnExit onEntered={(el)=>{ el.style.color='blue' }} appear={true}> <div>hello</div> </CSSTransition> {/*<div className={this.state.show ? 'show':'hide'}>hello</div>*/} <button onClick={this.handleToggole}> toggole</button> </Fragment> ); } handleToggole(){ this.setState(()=>( { show:this.state.show ? false : true } )) } } export default App;
app.css:
.fade-enter,.fade-appear{ opacity: 0; } .fade-enter-active,.fade-appear-active{ opacity: 1; transition: opacity 1s ease-in; } .fade-enter-done{ opacity: 1; } .fade-exit{ opacity: 1; } .fade-exit-active{ opacity: 0; transition: opacity 1s ease-in; } .fade-exit-done{ opacity: 0; }