Skip to content

Commit e2d232e

Browse files
committed
feat: 使用react-transition-group添加路由转场动画
1 parent df63e88 commit e2d232e

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"react-redux": "^7.2.0",
3636
"react-router-dom": "^5.1.2",
3737
"react-scripts": "3.4.1",
38+
"react-transition-group": "^4.4.1",
3839
"redux": "^4.0.5",
3940
"redux-thunk": "^2.3.0",
4041
"screenfull": "^5.0.2",

src/styles/index.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import './transition.less';
2+
13
body {
24
height: 100%;
35
-moz-osx-font-smoothing: grayscale;

src/styles/transition.less

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 路由转场动画
2+
.fade-enter {
3+
opacity: 0;
4+
transform: translateX(-30px);
5+
}
6+
7+
.fade-enter-active,
8+
.fade-exit-active {
9+
opacity: 1;
10+
transition: all 500ms ease-out;
11+
transform: translateX(0);
12+
}
13+
14+
.fade-exit {
15+
opacity: 0;
16+
transform: translateX(30px);
17+
}

src/views/layout/Content/index.jsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import { Redirect, withRouter, Route, Switch } from "react-router-dom";
33
import DocumentTitle from "react-document-title";
44
import { connect } from "react-redux";
5+
import { CSSTransition, TransitionGroup } from "react-transition-group";
56
import { Layout } from "antd";
67
import { getMenuItemInMenuListByProperty } from "@/utils";
78
import routeList from "@/config/routeMap";
@@ -27,21 +28,25 @@ const LayoutContent = (props) => {
2728
return (
2829
<DocumentTitle title={getPageTitle(menuList, pathname)}>
2930
<Content style={{ height: "calc(100% - 100px)" }}>
30-
<Switch>
31-
<Redirect exact from="/" to="/dashboard" />
32-
{routeList.map((route) => {
33-
return (
34-
handleFilter(route) && (
35-
<Route
36-
component={route.component}
37-
key={route.path}
38-
path={route.path}
39-
/>
40-
)
41-
);
42-
})}
43-
<Redirect to="/error/404" />
44-
</Switch>
31+
<TransitionGroup>
32+
<CSSTransition key={location.pathname} timeout={500} classNames="fade" exit={false}>
33+
<Switch location={location}>
34+
<Redirect exact from="/" to="/dashboard" />
35+
{routeList.map((route) => {
36+
return (
37+
handleFilter(route) && (
38+
<Route
39+
component={route.component}
40+
key={route.path}
41+
path={route.path}
42+
/>
43+
)
44+
);
45+
})}
46+
<Redirect to="/error/404" />
47+
</Switch>
48+
</CSSTransition>
49+
</TransitionGroup>
4550
</Content>
4651
</DocumentTitle>
4752
);

0 commit comments

Comments
 (0)