【问题标题】:Styled Components + React: CSS transition keeps resettingStyled Components + React:CSS 过渡不断重置
【发布时间】:2017-11-20 03:19:35
【问题描述】:

我有一个样式组件:

import styled, { keyframes } from 'styled-components';

const slideToCorner = keyframes`
  from {
    right: 0;
    top: 0;
  }

  to {
    right: 300px;
    top: 50px;
  }
`;

const SlideToCorner = styled.div`
  position: relative;
  right: 0;
  top: 0;
  ${props => props.slide && `animation: ${slideToCorner} 0.5s linear;`}
  transition: all 1s linear;
`;

export default SlideToCorner;

这就是它的使用方式:

<SlideToCorner slide={matchingStatus === MATCHING_STATES.CONFIRMING}>
  <TargetBox>
    <LocalVideo />
  </TargetBox>
</SlideToCorner>

但是,当它动画时,它会在动画进行到一半时不断重置回原来的位置:

我可以确认不是 matchingStatus === MATCHING_STATES.CONFIRMING 造成的。

【问题讨论】:

    标签: css reactjs styled-components


    【解决方案1】:

    添加forwards

    animation: ${slideToCorner} 0.5s linear forwards

    或使用animation-fill-mode: forwards

    检查animation-fill-mode 属性以了解不同的行为

    【讨论】:

    • woahhhhh 成功了!!我从未见过动画填充模式
    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 2014-10-29
    • 2023-03-24
    • 2021-05-12
    相关资源
    最近更新 更多