Lon*_*olf 8 reactjs styled-components
我有以下代码,我想将yreact组件的值传递给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>
);
}
Run Code Online (Sandbox Code Playgroud)
Hri*_*odi 14
您可以使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
`;
Run Code Online (Sandbox Code Playgroud)
在这里,您有Ÿ在道具BallAnimation。因此,您可以提取它并将其传递给moveVertically函数,该函数接受y值作为参数。
如何使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;
`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3168 次 |
| 最近记录: |