gpt4 book ai didi

react-router - 使用 React Router v4 和 react-transition-group v2 实现页面滑动动画

转载 作者:行者123 更新时间:2023-12-04 17:31:11 26 4
gpt4 key购买 nike

我正在使用 React Router v4 和 react-transition-group v2 来测试页面滑动动画。

const RouterMap = () => (
<Router>
<Route render={({ location }) =>
<TransitionGroup>
<CSSTransition key={location.pathname.split('/')[1]} timeout={500} classNames="pageSlider" mountOnEnter={true} unmountOnExit={true}>
<Switch location={location}>
<Route path="/" exact component={ Index } />
<Route path="/comments" component={ Comments } />
<Route path="/opinions" component={ Opinions } />
<Route path="/games" component={ Games } />
</Switch>
</CSSTransition>
</TransitionGroup>
} />
</Router>
)

和 CSS:
.pageSlider-enter {
transform: translate3d(100%, 0, 0);
}

.pageSlider-enter.pageSlider-enter-active {
transform: translate3d(0, 0, 0);
transition: all 600ms;
}
.pageSlider-exit {
transform: translate3d(0, 0, 0);
}

.pageSlider-exit.pageSlider-exit-active {
transform: translate3d(-100%, 0, 0);
transition: all 600ms;
}

动画如下:

enter image description here

如您所见, index page 滑动到 detail page 的动画是正确的(从右到左)。但是当我点击 Back 图标时,我希望 index page 从左到右出来。

我知道如果我按如下方式更改 CSS,页面将从左到右出现:
.pageSlider-enter {
transform: translate3d(-100%, 0, 0);
}
.pageSlider-exit.pageSlider-exit-active {
transform: translate3d(100%, 0, 0);
transition: all 600ms;
}

但是如何将两个动画结合在一起呢?一般来说,每当用户点击后退图标时,动画应该是 from left to right

更新:2017.08.31

感谢@MatijaG,使用路径深度确实是一个很棒的主意。我跟着它,遇到了一个新问题。
function getPathDepth(location) {
let pathArr = (location || {}).pathname.split('/');
pathArr = pathArr.filter(n => n !== '');
return pathArr.length;
}

<Route render={({ location }) =>
<TransitionGroup>
<CSSTransition
key={location.pathname.split('/')[1]}
timeout={500}
classNames={ getPathDepth(location) - this.state.prevDepth > 0 ? 'pageSliderLeft' : 'pageSliderRight' }
mountOnEnter={true}
unmountOnExit={true}
>
<Switch location={location}>
<Route path="/" exact component={ Index } />
<Route path="/comments" component={ Comments } />
<Route path="/opinions" component={ Opinions } />
<Route path="/games/lol" component={ LOL } /> // add a new route
<Route path="/games" component={ Games } />
</Switch>
</CSSTransition>
</TransitionGroup>
} />

并更新了 CSS:
.pageSliderLeft-enter {
transform: translate3d(100%, 0, 0);
}
.pageSliderLeft-enter.pageSliderLeft-enter-active {
transform: translate3d(0, 0, 0);
transition: all 600ms;
}
.pageSliderLeft-exit {
transform: translate3d(0, 0, 0);
}
.pageSliderLeft-exit.pageSliderLeft-exit-active {
transform: translate3d(100%, 0, 0);
transition: all 600ms;
}

.pageSliderRight-enter {
transform: translate3d(-100%, 0, 0);
}
.pageSliderRight-enter.pageSliderRight-enter-active {
transform: translate3d(0, 0, 0);
transition: all 600ms;
}
.pageSliderRight-exit {
transform: translate3d(0, 0, 0);
}
.pageSliderRight-exit.pageSliderRight-exit-active {
transform: translate3d(-100%, 0, 0);
transition: all 600ms;
}

动画:

enter image description here

从 '/' 到 '/games' 没问题,从 '/games' 回到 '/' 仍然没问题(类型 1: 路线 A -> 路线 B ​​,只有 2 条路线)。但是如果先从'/'到'/games',然后从'/games'到'/games/lol',第二阶段就会失去动画(类型2: route A -> route B -> route C , 3 条或更多路线)。我们还看到,从'/games/lol'回到'/games'再回到'/',幻灯片动画与类型1不同。

有人对这个问题有任何想法吗?

最佳答案

好吧,主要问题是,即使在您的问题中,您也没有指定应用程序应该如何知道要使用的方向。

一种方法是使用路径深度/长度:如果您要导航的路径比当前路径“更深”,则从右向左过渡,但如果从左向右过渡较浅?

另一个问题是路由器只给你你要去的位置,所以你应该保存以前的位置(或深度),以便你可以比较它。

之后就是在css类名之间切换的问题,
沿着这条线的东西:

import React from 'common/react'
import { withRouter, Route, Switch } from 'react-router'

function getPathDepth (location) {
return (location || {} ).pathname.split('/').length
}
class RouterMap extends React.PureComponent {
constructor (props, context) {
super(props, context)
this.state = {
prevDepth: getPathDepth(props.location),
}
}

componentWillReceiveProps () {
this.setState({ prevDepth: getPathDepth(this.props.location) })
}
render () {

return (
<Router>
<Route render={ ({ location }) =>
(<TransitionGroup>
<CSSTransition
key={ location.pathname.split('/')[1] }
timeout={ 500 }
classNames={ getPathDepth(location) - this.state.prevDepth ? 'pageSliderLeft' : 'pageSliderRight' } mountOnEnter={ true } unmountOnExit={ true }
>
<Switch location={ location }>
<Route path='/' exact component={ Index } />
<Route path='/comments' component={ Comments } />
<Route path='/opinions' component={ Opinions } />
<Route path='/games' component={ Games } />
</Switch>
</CSSTransition>
</TransitionGroup>)
}
/>
</Router>
)
}
}

export default withRouter(RouterMap)

代码笔示例: https://codepen.io/matijag/pen/ayXpGr?editors=0110

关于react-router - 使用 React Router v4 和 react-transition-group v2 实现页面滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45932263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com