React 路由动画新写法尝试

实现前进和后退的不同动画。

.left-enter {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  z-index: 10000;
  opacity: 1;
  transform: translate3d(100%, 0, 0);
}

.left-enter.left-enter-active {
  opacity: 1;
  transform: translate3d(0, 0, 0);
  transition: all 250ms ease-out;
}

.left-leave {
  opacity: 1;
  transform: translate3d(0, 0, 0);
}

.left-leave.left-leave-active {
  opacity: 1;
  transform: translate3d(-100%, 0, 0);
  transition: all 250ms ease-out;
}

.right-enter {
  transform: translate3d(-100%, 0, 0);
}

.right-enter.right-enter-active {
  transform: translate3d(0, 0, 0);
  transition: all 250ms ease-out;
}

.right-leave {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  background: #fff;
  z-index: 10000;
  opacity: 1;
  transform: translate3d(0, 0, 0);
}

.right-leave.right-leave-active {
  opacity: 1;
  transform: translate3d(100%, 0, 0);
  transition: all 250ms ease-out;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { Component } from "react";
import { HashRouter as Router, Route } from "react-router-dom";
import CSSTransitionGroup from "react-addons-css-transition-group";
// ... 省略
class App extends Component {
  render() {
    return (
      <div>
        <Router>
          <Route
            render={props => {
              let animateCls = "right";
              if (props.history.action === "PUSH") {
                animateCls = "left";
              }
              return (
                <CSSTransitionGroup
                  transitionName={animateCls}
                  transitionEnter={true}
                  transitionLeave={true}
                  transitionEnterTimeout={250}
                  transitionLeaveTimeout={250}
                >
                  <div key={props.location.pathname}>
                    <Route location={props.location} exact path="/" component={MainPage} />
                    <Route location={props.location} exact path="/payRecord" component={PayRecord} />
                    <Route location={props.location} exact path="/userCenter" component={UserCenter} />
                    <Route location={props.location} exact path="/couponList" component={CouponList} />
                    <Route location={props.location} exact path="/order" component={Order} />
                    <Route location={props.location} exact path="/myWallet" component={MyWallet} />
                    <Route location={props.location} exact path="/about" component={About} />
                    <Route location={props.location} exact path="/bookingRecord" component={BookingRecord} />
                  </div>
                </CSSTransitionGroup>
              );
            }}
          />
        </Router>
      </div>
    );
  }
}

export default App;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

如何在外部控制 push 和 pop?

this.props.history.push("/...");
1