css - 如何使用带 react 动画的样式组件制作滑入和滑出

标签 css reactjs styled-components react-animations

我正在构建一个显示 noneblock 的组件,具体取决于它的 isActive 属性。

我已经设法使用 react-animationsstyled-components 像这样slideInDown

import styled, { keyframes } from 'styled-components';
import { ifProp } from 'styled-tools';
import { slideInDown } from 'react-animations';

const slideInAnimation = keyframes`${slideInDown}`;

const MyComponent = styled.div`
  animation: 0.4s ${slideInAnimation};
  display: ${ifProp('isActive', 'block', 'none')};
`;

我现在需要让它 slideOutUp 。但是,我不确定如何同时实现 slideInDownslideOutUp 动画。

我将如何实现?

注意:有效。但我不知道如何让它同时滑入和滑出。我在下面尝试过类似的方法,但没有用。

import styled, { keyframes } from 'styled-components';
import { ifProp } from 'styled-tools';
import { slideInDown, slideOutUp } from 'react-animations';

const slideInAnimation = keyframes`${slideInDown}`;
const slideOutAnimation = keyframes`${slideOutUp}`;

const MyComponent = styled.div`
  animation: 0.4s ${slideInAnimation}, 0.4s ${slideOutAnimation};
  display: ${ifProp('isActive', 'block', 'none')};
`;

最佳答案

我浏览了 react-animations 库,我发现的是 slideOutUp设置 visibility: 'hidden.'

const slideOutUp: Animation = {
  from: {
    transform: translate3d(0, 0, 0)
  },
  to: {
    visibility: 'hidden',
    transform: translate3d(0, '-100%', 0)
  }
};

您可以使用 animation-fill-mode: forwards,这有助于在动画结束后保留​​样式。

你可以这样做(它有效):

const MyComponent = styled.div`
  animation: ${ifProp(
    "isActive",
    `0.4s ${slideInAnimation}`,
    `0.4s ${slideOutAnimation} forwards`
  )};
`;

这是工作示例,出于测试目的,我正在设置 onClick 事件 {isActive: false}

https://codesandbox.io/s/949ql6p6no

关于css - 如何使用带 react 动画的样式组件制作滑入和滑出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49875586/

相关文章:

javascript - 渲染 react 组件预览

javascript - WIth Bootstrap 有表格打印问题

javascript - typescript 构造函数参数。解析错误: Unexpected reserved word 'public'

reactjs - 如何将 React 组件的 props 传递给样式组件

javascript - Styled-components React HTML 元素工厂

javascript - HTML 表单共享不需要的输入值

html - 使用 Selenium 在 Ruby 中选中复选框时访问出现的类属性

javascript - 跨浏览器编程的最佳工具是什么?

css - 如何设置表格显示

javascript - 您可能需要一个适当的加载器来处理 ReactJS 文件中发生的此文件类型错误