简体   繁体   English

React - 使用 animation 注入内联样式

[英]React - Injecting inline style with animation

I have a component that needs to have different animations for each one of its children.我有一个组件需要为其每个子级设置不同的动画。

I am using React Hooks and Material UI to style my components.我正在使用 React Hooks 和 Material UI 来设置我的组件的样式。

My component(s) looks as follows:我的组件如下所示:

const useStyles = makeStyles({ // from material ui
  root: {
    position: 'relative',
  },
  child: {
    position: 'absolute',
  },
})

const Children = (props) => {
  const { count } = props;
  const classes = useStyles();
  const elements = [];

  // Generating random values for each child.
  for (let i = 0; i < count; i += 1){
    const randomX = Math.random() * 100;
    const randomY = Math.random() * 100;
    ... other random variables
    const delay = Math.random(); // I want to use this in my animation
    const duration = Math.random() * (3 - 0.5) + 0.5; // I want to use this in my animation
    elements.push({ ... all random variables mapped });
  }

  return (
    <>
      {elements.map(item => {
        <div
          key={item.x}
          style={{
            top: `${item.x}`,
            left: `${item.y}`,
            color: `${item.color}`,
            ... and so forth
          }}
          className={classes.child}
        />
      }
    </>
  );
};

const Parent = () => {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Children count={5} />
    </div>
  );
};

My issue is that I want different animations to trigger for the children elements.我的问题是我希望为子元素触发不同的动画。 I have tried adding a keyframe animation section for the makeStyles styling, and if I just define it up there I can easily add the styling and keyframe animation, and it works, However I have issues with adding different parameters for each child element if I do it there.我已经尝试为 makeStyles 样式添加一个关键帧 animation 部分,如果我只是在那里定义它,我可以轻松添加样式和关键帧 animation,它可以工作,但是如果我这样做,我会遇到为每个子元素添加不同参数的问题它在那里。 as far as I know.我所知道的。

const useStyles = makeStyles({
  root: { ... }, 
  child: {
    ...
    animation: '$fade 2s 1s infinite', // This works but I can't add different values for each child 
                                     // I want to change duration and delay for each child
  },
  '@keyframes fade': {
    '0%': { opacity: '0' }, 
    '100%': { opacity: '1' },
  },
})

I have also tried adding my keyframe animation to the inline styling of the child, but that seems to not work at all.我还尝试将我的关键帧 animation 添加到孩子的内联样式中,但这似乎根本不起作用。

<div
  key={item.x}
  style={{
    top: `${item.x}`,
    left: `${item.y}`,
    color: `${item.color}`,
    ... and so forth
    animation: `fade ${item.duration} ${item.delay} infinite`, // this does not work - not even with static values
    }}
  className={classes.child}
/>

I'm posting here hoping that someone knows how to overcome my issue.我在这里发帖希望有人知道如何克服我的问题。 Let me know your thoughts.让我知道你的想法。 I'm pretty sure it is possible with StyledComponents , but I don't feel like installing another styling library just to overcome this very specific issue.我很确定StyledComponents是可能的,但我不想安装另一个样式库来解决这个非常具体的问题。

I'm pretty sure that I at some point worked with CSS custom variables var(--duration) & var(--delay) , and it could do some pretty nice things (might even fix this issue), but I have been unable to find anything usable on the topic as of today.我很确定我在某些时候使用过 CSS 自定义变量var(--duration) & var(--delay) ,它可以做一些非常好的事情(甚至可以解决这个问题),但我一直无法到今天为止找到关于该主题的任何可用信息。 The issue is mostly how I inject the custom variables into my styling.问题主要是我如何将自定义变量注入到我的样式中。 If you know how I need to set this up, let me know.如果您知道我需要如何设置,请告诉我。

Thanks in advance.提前致谢。

I found the solution that I was wanting.我找到了我想要的解决方案。

The reason it didn't work was due to "random naming" from Material UI makestyles package.它不起作用的原因是由于 Material UI makestyles package 中的“随机命名”。 So what I ended up doing was to use the animation source in the makestyles package:所以我最终做的是在makestyles package中使用animation源:

const useStyles = makeStyles({
  root: { ... }, 
  child: {
    ...
    animationName: '$fade',
  },
  '@keyframes fade': {
    '0%': { opacity: '0' }, 
    '100%': { opacity: '1' },
  },
})

And then changing the duration and delay in the inline styling as follows:然后更改内联样式的持续时间和延迟,如下所示:

<div
  key={item.x}
  style={{
    animationDuration: `${item.duration}s´,
    animationDelay: `${item.delay}s`,
    animationIterationCount: 'infinite', 
  }}
  className={classes.child}
/>

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

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