Skip to main content

React中实现CSS过渡动画

index.css

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

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

app.js

import React, { Component, Fragment } from 'react';
import './index.css'

class App extends Component{
constructor(props){
super(props)
this.state={
show:true
}
this.handleToggle = this.handleToggle.bind(this)
}
render(){
return (
<Fragment>
<div class={this.state.show?'show':'hide'}>hello world</div>
<button onClick={this.handleToggle}>toggle</button>
</Fragment>
)
}

handleToggle(){
this.setState(()=>({
show:!this.state.show
}))
}
}

export default App;