17

我正在用 MUI 构建一个 GatsbyJS 站点。使用withStylesHOC,是否可以制作闪烁动画?我尝试在以下位置提供动画styles

const styles = theme => ({
        '@keyframes blinker': {
            from: {opacity: 1},
            to: {opacity: 0}
        },
        headerGT: {
            color: 'white',
            animation: ['blinker', '1s', 'linear', 'infinite'],
            '-webkit-animation': ['blinker', '1s', 'linear', 'infinite'],
            '-moz-animation': ['blinker', '1s', 'linear', 'infinite'],
        }
    })

我可以看到类和关键帧在headerGT构建 DOM 时被识别并具有动画方法,但动画不会触发。有任何想法吗?

4

4 回答 4

19

我遇到了同样的问题,但正如JSS 文档中所指定的那样,使用$前缀引用我的动画解决了它。

尝试这个:

const style = theme => (
{
    '@keyframes blinker': {
        from: { opacity: 1 },
        to: { opacity: 0 },
    },
    headerGT: {
        [...]
        animationName: '$blinker',
        animationDuration: '1s',
        animationTimingFunction: 'linear',
        animationIterationCount: 'infinite',
    },
});
于 2019-10-28T10:44:59.667 回答
6

是的,这很有可能。例如:

const style = theme => (
{
    '@keyframes blinker': {
        from: {opacity: 1},
        to: {opacity: 0}
    },
    headerGT: {
            position: 'absolute',
            width: '60px',
            height: '60px',
            right: 17,
            backgroundImage: 'url(https://cdn3.iconfinder.com/data/icons/common-4/24/ui-21-512.png)',
            backgroundRepeat: 'no-repeat',
            backgroundSize: 'contain',
            border: 'none',
            bottom: '108px',
            opacity: '0.4',
            backgroundColor: 'red',
            animationName: 'blinker',
            animationDuration: '1s',
            animationTimingFunction: 'linear',
            animationIterationCount:'infinite',
    },
});
于 2019-05-09T23:31:20.650 回答
1

下面是一个闪烁动画的例子,可以根据组件的 disabled 属性打开或关闭:

import { makeStyles } from '@material-ui/core'

const useStyles = makeStyles({
  '@keyframes flicker': {
    from: {
      opacity: 1,
    },
    to: {
      opacity: 0.7,
    },
  },
  flicker: {
    animationName: '$flicker',
    animationDuration: '1000ms',
    animationIterationCount: 'infinite',
    animationDirection: 'alternate',
    animationTimingFunction: 'ease-in-out',
  },
  withAnimation: ({ disabled }: { disabled: boolean }) => ({
    animationPlayState: disabled ? 'paused' : 'running',
  }),
});

const Component: React.FC<{ disabled }> = () => {
  const { flicker, withAnimation  } = useStyles({ disabled })

  return (
    <div className={`${flicker} ${withAnimation}`}>Hello</div>
  )
}

请注意,由于只有 animationPlayState 依赖于 prop 的值,因此有 2 个单独的类。但是,animationName必须在对象内声明,因为@material-ui将映射到对象并替换$flicker为附加了随机生成的哈希的动画名称(即makeStyles-keyframes-flicker-4043)。使用 props 调用的函数返回的对象不会发生映射。

于 2020-04-14T16:42:27.270 回答
1

MUI v5中,您可以使用 fromemotion 定义动画keyframes

import { styled } from '@mui/material/styles';
import { keyframes } from '@mui/system';

const blink = keyframes`
  from { opacity: 0; }
  to { opacity: 1; }
`;

const BlinkedBox = styled('div')({
  backgroundColor: 'pink',
  width: 30,
  height: 30,
  animation: `${blink} 1s linear infinite`,
});

现场演示

Codesandbox 演示

于 2021-11-08T16:11:17.077 回答