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) */ }