1. React过渡动画
2. 动画组件:
原生CSS一般是通过state转换为props再结合style-conponents以及逻辑完成动画
const StyleDiv = styled.div`
width: ${props => props.width};
height: ${props => props.height};
background: skyblue;
opacity: ${props => props.opacity};
transition: all 3s;
`
class App extends React.Component{
constructor(props){
super(props);
this.state = {
width: 0,
height: 0,
opacity:0
}
}
render(){
return (
<div>
<StyleDiv {...this.state}></StyleDiv>
<button onClick={()=>{this.btnClick()}}>按钮</button>
</div>
);
}
btnClick(){
this.setState({
width: '100px',
height: '100px',
opacity:1
})
}
}
export default App;
在前端开发中,通常使用CSSTransition来完成过渡动画效果
npm install react-transition-group --save
import {CSSTransition} from 'react-transition-group';
.-enter
/ .-enter-active
/ .-enter-done
/.-exit
/ .-exit-active
/ .-exit-done
in
属性 :classNames
属性:timeout
属性 :3.CSSTransition状态
unmountOnExit
:import React from 'react';
import './App.css'
import {CSSTransition} from 'react-transition-group';
class App extends React.Component{
constructor(props){
super(props);
this.state = {
isShow: false
}
}
render(){
return (
<div>
<CSSTransition in={this.state.isShow}
classNames={'box'}
timeout={3000}
appear
unmountOnExit={true}>
<div></div>
</CSSTransition>
<button onClick={()=>{this.btnClick()}}>显示</button>
</div>
);
}
btnClick(){
this.setState({
isShow: true
})
}
}
export default App;
.box-enter{
/*进入动画执行之前绑定的类名*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}
.box-enter-active{
/*进入动画执行过程中绑定的类名*/
width: 100px;
height: 100px;
opacity: 1;
transition: all 3s;
}
.box-enter-done{
/*进入动画执行完毕之后绑定的类名*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}
.box-exit{
/*退出动画执行之前绑定的类名*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}
.box-exit-active{
/*退出动画执行过程中绑定的类名*/
width: 0;
height: 0;
opacity: 0;
transition: all 3s;
}
.box-exit-done{
/*退出动画执行完毕之后绑定的类名*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}
.box{
width: 100px;
height: 100px;
background: skyblue;
}
.box-appear{
/*初始化动画执行之前绑定的类名*/
width: 0;
height: 0;
opacity: 0;
background: skyblue;
}
.box-appear-active{
/*初始化动画执行过程中绑定的类名*/
width: 100px;
height: 100px;
opacity: 1;
transition: all 3s;
}
.box-appear-done{
/*初始化动画执行完毕之后绑定的类名*/
width: 100px;
height: 100px;
opacity: 1;
background: red;
}
onEnter/onEntering/onEntered
onExit/onExiting/onExited
import React from 'react';
import './App.css'
import {CSSTransition} from 'react-transition-group';
class App extends React.Component{
constructor(props){
super(props);
this.state = {
isShow: true
}
}
render(){
return (
<div>
<CSSTransition in={this.state.isShow}
classNames={'box'}
timeout={3000}
unmountOnExit={true}
appear
onEnter = {this.myFn}
onEntering={()=>{console.log('进入动画执行过程中');}}
onEntered={()=>{console.log('进入动画执行完毕');}}
>
<div className={'box'}></div>
</CSSTransition>
<button onClick={()=>{this.btnClick1()}}>显示</button>
<button onClick={()=>{this.btnClick2()}}>隐藏</button>
</div>
);
}
myFn(currentEl, isAppear){
// console.log(currentEl, isAppear);
console.log('进入动画开始之前');
}
btnClick1(){
this.setState({
isShow: true
})
}
btnClick2(){
this.setState({
isShow: false
})
}
}
export default App;
SwitchTransition
SwitchTransition
可以完成组件切换的动画SwitchTransition
组件里面要有CSSTransition
或者Transition
组件,不能直接包裹你想要切换的组件SwitchTransition
里面的CSSTransition或Transition
组件不再像以前那样接受in
属性来判断元素是何种状态, 取而代之的是key
属性import React from 'react';
import './App.css'
import {CSSTransition, SwitchTransition} from 'react-transition-group';
class App extends React.Component{
constructor(props){
super(props);
this.state = {
isOn: true
}
}
render(){
return (
<div>
<SwitchTransition mode={'in-out'}>
<CSSTransition key={this.state.isOn}
timeout={3000}
classNames={'btn'}>
<button onClick={()=>{this.setState({isOn: !this.state.isOn})}}>{this.state.isOn ? 'on' : 'off'}</button>
</CSSTransition>
</SwitchTransition>
</div>
);
}
}
export default App;
import React from 'react';
import './App.css'
import {CSSTransition, TransitionGroup} from 'react-transition-group';
class App extends React.Component{
constructor(props){
super(props);
this.state = {
// heroList : ['鲁班', '鲁班', '鲁班']
heroList:[
{id:1, name:'鲁班'},
{id:2, name:'虞姬'},
{id:3, name:'黄忠'},
]
}
}
render(){
return (
<div>
<ul>
<TransitionGroup>
{
/*
注意点:
在企业开发中一定要保证CSSTransition key的唯一性
* */
this.state.heroList.map((obj, index) =>{
return (
<CSSTransition key={obj.id}
timeout={3000}
classNames={'item'}>
<li onClick={()=>{this.removeHero(index)}}>{obj.name}</li>
</CSSTransition>
)
})
}
</TransitionGroup>
</ul>
<button onClick={()=>{this.btnClick()}}>新增</button>
</div>
);
}
btnClick(){
this.setState({
// heroList: [...this.state.heroList, '阿珂']
heroList: [...this.state.heroList, {id: this.state.heroList.length + 1, name:'阿珂'}]
})
}
removeHero(index){
// console.log(index);
const list = this.state.heroList.filter((name, idx) =>{
return idx !== index;
})
// console.log(list);
this.setState({
heroList: list
})
}
}
export default App;