私はリアクションルータv4とリアクショントランジショングループv2を使ってページスライドアニメーションをテストしています。ページリアクションルータv4とリアクショントランジショングループ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;
}
アニメーションのように怒鳴るです:ご覧のとおり
、detail page
からindex page
スライドはすべての権利(権利であるアニメーション左へ)。しかし、Back
アイコンをクリックすると、index page
が左から右に出てくることを願っています。
.pageSlider-enter {
transform: translate3d(-100%, 0, 0);
}
.pageSlider-exit.pageSlider-exit-active {
transform: translate3d(100%, 0, 0);
transition: all 600ms;
}
しかし、どのように一緒に2つのアニメーションを組み合わせる:
私は怒鳴るようCSSを変更した場合、ページは左から右に出てくるだろう知っていますか?一般に、ユーザーが戻るアイコンをクリックするたびに、アニメーションはfrom left to right
になります。
更新:@MatijaGのため2017年8月31日
おかげで、パスの深さを使用すると、本当にすごいアイデアです。私はそれに続き、新しい問題を抱えていました。
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;
}
アニメーション: '/ゲーム' に '/' から
はokです、と '/ゲーム' からバックへ'/'はまだOKです(タイプ1:ルートA - >ルートB、2ルートのみ)。しかし、最初に「/」から「/ゲーム」、そして「/ゲーム」から「/ゲーム/笑」になると、第2段階でアニメーションが失われます(タイプ2:ルートA→ルートB→ルートC 、3つ以上のルート)。また、 '/ games/lol'から '/ games'に戻って '/'に戻ると、スライドアニメーションはタイプ1と同じではありません。
誰でもこの問題について考えていますか?
私はキー= {場所を使用する理由が何であるかを尋ねるかもしれません。 pathname.split( '/')[1]}これはa-> b-> cの問題だと思います。 これは/ gamesと/ games/lolが同じキーを持っているため、アニメーションがトリガーされないことを意味します(/ =>未定義、/ games =>ゲーム、/ games/lol => games)。あなたはkey = {location.key}を使うべきです – MatijaG
@MatijaG、はい、あなたは正しいです。それは私の間違いです。私はHashRouterのために 'key = {location.pathname.split( '/')[1]}'を使用します。 – jason
@MatijaG、この問題が深刻になると、いくつかの問題があることがわかりました。私は別の質問をしました:https://stackoverflow.com/questions/45978263/dynamic-page-sliding-animation-with-react-router-v4-and-react-transition-group-v。時間があれば、本当に助けてくれることを願っています。とにかく、ありがとう。 – jason