如何在第一次加载页面时使用USESTATE进行动画更改(密钥帧),然后单击USESTATE,而无需启动动画?
我有一个棘手的问题。
我有一个容器在点击它时被着色。 单击它将导致将数据发送到数组,但这不是问题。
单击它也将使颜色的更改动画。动画将在单击时向前触发,如果将再次单击容器,则向后触发。
问题在于,动画是通过使用Usestate(Boolean)有条件地触发的,因此,在加载页面时,第二个动画甚至在单击容器之前就会运行。 我想知道动画状态风格,但这似乎还不够。
这是可编辑的演示 https://qeevsk.csb.app/ 有什么想法,也许有更好的侵犯?
代码本身:
const [activeDiv, setDivState] = useState(false);
const BigDiv = styled("div")(
({ theme, activeDiv }) => css`
min-width: 220px;
height: 100%;
position: relative;
padding: ${theme.spacing(2, 0)};
border-bottom: red 4px solid;
background-color: transparent;
cursor: pointer;
text-align: center;
transition-delay: 0.5s;
will-change: transform;
::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
background-color: ${activeDiv ? "red" : "transparent"};
animation: ${activeDiv ? setColor("red") : unsetColor("red")}
0.5s ease ;
transform-origin: bottom center};
}
`
);
const setColor = (colorTo) => keyframes`
0% {transform: scaleY(0); }
100% {transform: scaleY(1); background-color: ${colorTo}}
`;
const unsetColor = (colorFrom) => keyframes`
0% {transform: scaleY(1); background-color: ${colorFrom}}
100% {transform: scaleY(0); background-color: transparent;}
`;
const SecondDiv = styled("div")(
({ theme, activeDiv, color }) => css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
position: relative;
z-index: 1;
`
);
return (
<>
<BigDiv
activeDiv={activeDiv}
onClick={(e) => {
setDivState(!activeDiv);
}}
>
<SecondDiv>
<p>Click to animate</p>
</SecondDiv>
</BigDiv>
</>
);
}
I got a little tricky problem.
I am having a container with is being coloured on clicking it.
Clicking it will result in sending data to array, but thats not the problem.
Clicking it will also animate the change of colour. Animation will be triggered forward on click, and backwards if container will be clicked again.
The issue is that animation is triggered conditionally with use of useState (boolean) and thus, the second animation will be running already when page is loaded, before even clicking the container.
I was wondering about animation-play-state style, but it not seems to be sufficient.
Heres the editable demo https://qeevsk.csb.app/
Any ideas, maybe there is better aproach ?
The code itself:
const [activeDiv, setDivState] = useState(false);
const BigDiv = styled("div")(
({ theme, activeDiv }) => css`
min-width: 220px;
height: 100%;
position: relative;
padding: ${theme.spacing(2, 0)};
border-bottom: red 4px solid;
background-color: transparent;
cursor: pointer;
text-align: center;
transition-delay: 0.5s;
will-change: transform;
::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
background-color: ${activeDiv ? "red" : "transparent"};
animation: ${activeDiv ? setColor("red") : unsetColor("red")}
0.5s ease ;
transform-origin: bottom center};
}
`
);
const setColor = (colorTo) => keyframes`
0% {transform: scaleY(0); }
100% {transform: scaleY(1); background-color: ${colorTo}}
`;
const unsetColor = (colorFrom) => keyframes`
0% {transform: scaleY(1); background-color: ${colorFrom}}
100% {transform: scaleY(0); background-color: transparent;}
`;
const SecondDiv = styled("div")(
({ theme, activeDiv, color }) => css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
position: relative;
z-index: 1;
`
);
return (
<>
<BigDiv
activeDiv={activeDiv}
onClick={(e) => {
setDivState(!activeDiv);
}}
>
<SecondDiv>
<p>Click to animate</p>
</SecondDiv>
</BigDiv>
</>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经得到了一些有效的答案,但是如果您正在寻找一种不同的方法,则可以管理使用第三个值之类的动画状态:
这种方法的优势是您可以重复使用此
andy> animstatus
在所有动画中都作为接口,并轻松地为“空闲”添加特定效果,这是我的演示: https://codesandbox.io/s/animate-divs-forked-u2oltp
You got some valid answers already, but if you're looking for a different approach you could manage the animation state with a third value like "IDLE":
The advantage of this approach is that you can reuse this
AnimStatus
across all your animations as an interface, and easily add specific effects for "IDLE"Here is my demo: https://codesandbox.io/s/animate-divs-forked-u2oltp
您可以定义一个状态变量,例如
startAnimation
,并默认设置为false。当用户单击
单击它继续
时,将其设置为true,然后将动画应用在
组件中
使用,将其传递到类似的组件中
You can define a state variable something like
startAnimation
and set to to false by default.When user click on
click it to continue
then set it to true and apply animation accordinglyDefined state
Pass it in component
Use it in component like this
我能想到的最简单的修复是将初始的Activediv状态设置为
null
,然后将动画规则更改为:现在在启动时不会有任何动画,但是如果Activediv是任何动画。
这是我的叉子: https://codesandbox.io/s.io/s/animate-divs-divs-forked-forked- -gth814
Easiest fix I could think of was to set the initial activeDiv state to
null
and change the animation rule to:Now there won't be any animation at startup, but it will animated if activeDiv is anything but null.
Here is my fork: https://codesandbox.io/s/animate-divs-forked-gth814