React中路由切换动画

简介: React中路由切换动画

react框架下App.js

import React from 'react'
import { Switch, Route, withRouter, Link, Redirect } from 'react-router-dom'
import './App.css';
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import Login from './components/Login'
import Wish from './components/Wish'
class App extends React.Component {
    state = {
        animate: "animateLeft"
    }
    componentWillReceiveProps(nextProps) {
        let to = nextProps.location.pathname
        let from = this.props.location.pathname
        console.log(to, from)
        if (to == "/" && from == "/wish" || to == "/wish" && from == "/") {
            // 加右动画
            this.state.animate = "animateRight"
        } else {
            // 加左动画
            this.state.animate = "animateLeft"
        }
    }
    shouldComponentUpdate(nextProps) {
        let to = nextProps.location.pathname
        let from = this.props.location.pathname
        if (to == from) {
            return false
        } else {
            return true
        }
    }
    render() {
        return (
            <div className="App">
                <TransitionGroup>
                    <CSSTransition timeout={2000} classNames={this.state.animate} unmountOnExit key={this.props.location.pathname}>
                        <Switch location={this.props.location}>
                            <Route exact path="/" component={Login} />
                            <Route path="/wish" component={Wish} />
                            <Redirect to="/"></Redirect>
                        </Switch>
                    </CSSTransition>
                </TransitionGroup>
            </div>
        );
    }
}
export default withRouter(App);

App.css

/* 入场动画过程 */
.animateLeft-enter{
  transform: translateX(100%) rotate(0) scale(0);
}
.animateLeft-enter-active{
  transition: all 2s;
  transform: translateX(0)  rotate(360deg) scale(1);
}
.animateLeft-enter-done{
  transform: translateX(0);
}
/* 出场动画过程 */
.animateLeft-exit{
  opacity: 1;
  /* transform: translateX(0px) */
}
.animateLeft-exit-active{
  opacity: 0;
  transition: 2s;
  /* transform: translateX(200px) */
}
.animateLeft-exit-done{
  opacity: 0;
  /* transform: translateX(200px) */
}
/* 入场动画过程 */
.animateRight-enter{
  transform: translateX(-100%) rotate(0) scale(0);
}
.animateRight-enter-active{
  transition: all 2s;
  transform: translateX(0)  rotate(360deg) scale(1);
}
.animateRight-enter-done{
  transform: translateX(0);
}
/* 出场动画过程 */
.animateRight-exit{
  opacity: 1;
  /* transform: translateX(0px) */
}
.animateRight-exit-active{
  opacity: 0;
  transition: 2s;
  /* transform: translateX(200px) */
}
.animateRight-exit-done{
  opacity: 0;
  /* transform: translateX(200px) */
}
相关文章
|
前端开发 Java API
【第49期】一文了解React动画
【第49期】一文了解React动画
483 0
|
前端开发 JavaScript Java
react中实现组件之间的转场动画
react中实现组件之间的转场动画
508 0
|
前端开发
滚动页面触发相应位置动画 ---react
滚动页面触发相应位置动画 ---react
|
前端开发
react函数组件购物车小球动画实现
react函数组件购物车小球动画实现
193 0
react函数组件购物车小球动画实现
|
前端开发
使用react实现一个简易的小球动画
使用react实现一个简易的小球动画
157 0
|
前端开发
React 购物车小球动画
React 购物车小球动画
161 0
|
前端开发
第三十一章 React中路由组件和一般组件
第三十一章 React中路由组件和一般组件
173 0
|
前端开发 JavaScript
React中组件间过渡动画如何实现?
React中组件间过渡动画如何实现?
249 0
|
前端开发
React-组件-原生动画 和 React-组件-性能优化
React-组件-原生动画 和 React-组件-性能优化
137 0
|
前端开发 JavaScript UED
React | React的过渡动画
React | React的过渡动画
382 0