如何通过反应将道具传递给样式化组件中的关键帧?
How to pass props to keyframes in styled-component with react?
我有以下代码,我想将 y
的值从 React 组件传递到 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>
);
}
如何制作 moveVertically returns keyframes 样式组件的函数?
这样你就可以传入你想要的道具了:
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;
`
您可以使 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。因此,您可以提取它并将其传递给接受 y 值作为参数的 moveVertically 函数。
这个使用 typescript 的进度条动画示例
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;
`;
我有以下代码,我想将 y
的值从 React 组件传递到 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>
);
}
如何制作 moveVertically returns keyframes 样式组件的函数?
这样你就可以传入你想要的道具了:
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;
`
您可以使 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。因此,您可以提取它并将其传递给接受 y 值作为参数的 moveVertically 函数。
这个使用 typescript 的进度条动画示例
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;
`;