Как передать реквизиты для ключевых кадров в styled-компоненте с реагировать? - PullRequest
0 голосов
/ 11 июня 2018

У меня следующий код, и я хочу передать значение 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>
        );
    }

Ответы [ 2 ]

0 голосов
/ 11 июня 2018

Вы можете сделать 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
`;

Здесь у вас есть y в реквизите BallAnimation .Таким образом, вы можете извлечь его и передать его функции moveVertically , которая принимает значение y в качестве параметра.

0 голосов
/ 11 июня 2018

Как насчет того, чтобы сделать 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;
`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...