如何使用 React 将 props 传递到样式组件中的关键帧?

问题描述 投票:0回答:6

我有以下代码,我想将

y
的值从反应组件传递到
moveVertically
关键帧。可以这样做吗?

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


    const moveVertically = keyframes`
        0% {
            transform : translateY(0px) 
        }
        100% {
            transform : translateY(-1000px) //I need y here
        }
    `;

    //I can access y in here via props but can't send it above
    const BallAnimation = styled.g`
        animation : ${moveVertically} ${props => props.time}s linear
    `;


    export default function CannonBall(props) {

        const cannonBallStyle = {
            fill: '#777',
            stroke: '#444',
            strokeWidth: '2px',
        };

        return (
            <BallAnimation time = {4} y = {-1000}>
                <circle cx = {0} cy = {0} r="25" style = {cannonBallStyle}/>
            </BallAnimation>
        );
    }
reactjs styled-components
6个回答
51
投票

您可以将 moveVertically 设为函数。请考虑下面的代码:

const moveVertically = (y) => keyframes`
    0% {
        transform : translateY(0px) 
    }
    100% {
        transform : translateY(${y}px)
    }
`;

const BallAnimation = styled.g`
    animation : ${props => moveVertically(props.y)} ${props => props.time}s linear
`;

在这里,BallAnimation 的道具中有 y。因此,您可以提取它并将其传递给 moveVertically 函数,该函数接受 y 值作为参数。


9
投票

如何制作 moveVertically 一个返回 keyframes 样式组件的函数?

这样你就可以传入你想要的 prop 了:

const moveVertically = (y) =>
  keyframes`
    0% {
      transform: translateY(0px);
    }
    100% {
      transform: translateY(${y}px);
    }
  `

const BallAnimation = styled.g`
  animation: ${props => moveVertically(props.y)} ${props => props.time}s linear;
`

3
投票

此示例是使用打字稿的进度条动画

    export const animatedProgress = (y:number) => keyframes`
0%{
      width: 0%;
    }
    25%{
        width:  ${y >= 25 ? 25 : y}%;
    }
    50%{
      width:  ${y >= 50 ? 50 : y}%;
    }
    75%{
      width:  ${y >= 75 ? 75 : y}%;
    }
    100%{
      width:  ${y >= 100 ? 100 : y}%;
    }

`;

export const ProgressAnimated = styled("div")<progressAnimated>`

animation : ${props => animatedProgress(props.value)} 3s linear;
`;

0
投票
const Stick = styled("div")(() => ({
  "@keyframes gradient": {
    "0%": {
      backgroundPosition: "0% 50%",
    },
    "50%": {
      backgroundPosition: "100% 50%",
    },
    "100%": {
      backgroundPosition: "0% 50%",
    },
  },
  width: "40vw",
  height: "20vw",
  background:
    "linear-gradient(-45deg, #ee7752, #e73c7e, #9581F4, #23a6d5, #23d5ab)",
  animation: "gradient 10s ease infinite",
}));

0
投票

这似乎对我有用

sx 道具内部

{Array.from(new Array(5)).map((item, index) => (
    <Box
      sx={{
        key={index}
          sx={{
            position: "absolute",
            top: "100%",
            left: "-50%",
            animationName: "move",
            animationTimingFunction: "linear",
            animationIterationCount: "infinite",
            animationDuration: `${index * 6}s`,
            animationDelay: `${index * 6}s`,
            transform: `translateX(${1000 * index}%) `,
            transformOrigin: "left bottom", 
            "@keyframes move": {
             "0%": {
               opacity: 0,
            },
            "50%": {
              opacity: 1,
            },
            "100%": {
              opacity: 0,
            },
        },          
      }}
    >
     Content
    </Box>
))}

0
投票

它使用 css styled-components 库对我有用:

const animation = (props) => keyframes`
 0% {
  top: ${props.$position1.top}vh;
  left: ${props.$position1.left}vw;
}
100% {
  top: ${props.$position2.top}vh;
  left: ${props.$position2.left}vw;
}`

const MobileImage = styled.img`
  position: absolute;
  width: ${(props) => props.$cartdWidth}vw;
  height: ${(props) => props.$cartdHeight}vh;
  z-index: ${(props) => props.$zIndex};
  top: ${(props) => props.$position1.top}vh;
  left: ${(props) => props.$position1.left}vw;
  animation: ${(props) =>
    css`
      ${animation(props)} ${flightTime}ms linear forwards;
    `};
`

© www.soinside.com 2019 - 2024. All rights reserved.