简体   繁体   English

使用 react-transition-group 左右滑动

[英]Right and left sliding with react-transition-group

I want to make left and right slide with react-transition-group .我想用react-transition-group制作左右滑动。 When I press next button it comes right (next step) and when I click go back button, I want to come it from left.当我按下下一步按钮时,它是正确的(下一步),当我点击返回按钮时,我想从左边来。 My code always gets it from right side.我的代码总是从右侧获取它。 How can I achieve something like this我怎样才能实现这样的目标

.onboarding-screen
    display flex
    flex-direction column
    flex-grow 1
    position absolute
    width 100%
    height 100%
    align-items center
    &.step-enter
        transform translateX(100%)
        transition(all 1s)
    &.step-enter-active
        transform translateX(0%)
        transition(all 1s)
    &.step-enter-done
        position relative
    &.step-exit
        transform translateX(-100%)
        transition(all 1s)
    &.step-exit-active
        transform translateX(-100%)
        transition(all 1s)
    &.step-exit-done
        position relative
  • First of all you don't need -done variations,首先你不需要-done变化,
  • Your css only handles the the animation when you click the next button (enters from right, exits from left), therefore you also need the opposite of that:您的 css 仅在您单击下一个按钮时处理动画(从右侧进入,从左侧退出),因此您还需要相反的:

CSS: CSS:

.right-to-left-enter {
    transform: translateX(100%);
}
.right-to-left-enter-active {
    transform: translateX(0);
    transition:all 1s ease;
}      

.right-to-left-exit {
    transform: translateX(0);
}
.right-to-left-exit-active {
    transform: translateX(-100%);
    transition:all 1s ease;
}      

.left-to-right-enter {
    transform: translateX(-100%);
}
.left-to-right-enter-active {
    transform: translateX(0);
    transition:all 1s ease;
}      

.left-to-right-exit {
    transform: translateX(0);
}
.left-to-right-exit-active {
    transform: translateX(100%);
    transition:all 1s ease;
}      

You need TransitionGroup and CSSTransition for the sliding animation you want.你需要TransitionGroupCSSTransition来制作你想要的滑动动画。 However there is one thing to note:不过有一点需要注意:

Once a CSSTransition is mounted, you can not alter the exit animation even if you change the classNames property on the go.安装 CSSTransition 后,即使您随时更改classNames属性,也无法更改退出动画。 But there is a property called childFactory of TransitionGroup component which gives you the ability yo change the mounted CSSTransition component's properties.但是TransitionGroup组件有一个名为childFactory的属性,它使您能够更改已安装的CSSTransition组件的属性。

Here is the code you need:这是您需要的代码:

const [isNext, setIsNext] = useState(true);

const onNext = () => setIsNext(true);
const onPrevious = () => setIsNext(false);

return (
<TransitionGroup childFactory={child => React.cloneElement(child, { classNames: isNext ? "right-to-left" : "left-to-right", timeout: 1000 })}>
    <CSSTransition key={key} classNames="right-to-left" timeout={1000}>
          // Put your sliding content here. Change the key as the content changes.
          // The value of the key is unimportant
    </CSSTransition>
</TransitionGroup>
);

Do not forget to use absolute positioning on sliding content不要忘记在滑动内容上使用绝对定位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM