admin管理员组文章数量:1122826
I have a parent div, which have button "show details". Here is how it works: when it pressed -> bool changes -> details showing up.
Code is something like this: (instead of blabla is huge div)
import React, { useState, useCallback } from 'react';
function Page() {
const [showDetails, setShowDetails] = useState(true);
const PurchaseComponent = () => {
const toggleDetails = useCallback(() => {
setShowDetails(prevShowDetails => !prevShowDetails);
}, []);
return (
<div className="mydiv">
<button onClick={toggleDetails}>
Show Details
</button>
{showDetails && (
<div>BlaBla</div>
)}
</div>
);
};
return (
<div>
<PurchaseComponent />
</div>
);
}
export default Page;
CSS:
.mydiv {
min-height: 250px;
width: 800px;
border-radius: 10px;
padding: 50px;
margin-bottom: 20px;
background: linear-gradient(45deg, #4d474f, #282f32, #371211, #7d1416, #850b0f, #40080a, #850b0f, #7d1416, #371211, #282f32, #4d474f);
background-size: 2200% 2200%;
-webkit-animation: AnimationName 51s ease infinite;
animation: AnimationName 51s ease infinite;
transition: transform 2s ease;
}
@-webkit-keyframes AnimationName {
0%{background-position:0% 96%}
50%{background-position:100% 5%}
100%{background-position:0% 96%}
}
@keyframes AnimationName {
0%{background-position:0% 96%}
50%{background-position:100% 5%}
100%{background-position:0% 96%}
}
Parent have an animated background made with gradient animator. All I want it to not restart when details button is pressed (childs updating). Size of parent div changes when button pressed.
I have a parent div, which have button "show details". Here is how it works: when it pressed -> bool changes -> details showing up.
Code is something like this: (instead of blabla is huge div)
import React, { useState, useCallback } from 'react';
function Page() {
const [showDetails, setShowDetails] = useState(true);
const PurchaseComponent = () => {
const toggleDetails = useCallback(() => {
setShowDetails(prevShowDetails => !prevShowDetails);
}, []);
return (
<div className="mydiv">
<button onClick={toggleDetails}>
Show Details
</button>
{showDetails && (
<div>BlaBla</div>
)}
</div>
);
};
return (
<div>
<PurchaseComponent />
</div>
);
}
export default Page;
CSS:
.mydiv {
min-height: 250px;
width: 800px;
border-radius: 10px;
padding: 50px;
margin-bottom: 20px;
background: linear-gradient(45deg, #4d474f, #282f32, #371211, #7d1416, #850b0f, #40080a, #850b0f, #7d1416, #371211, #282f32, #4d474f);
background-size: 2200% 2200%;
-webkit-animation: AnimationName 51s ease infinite;
animation: AnimationName 51s ease infinite;
transition: transform 2s ease;
}
@-webkit-keyframes AnimationName {
0%{background-position:0% 96%}
50%{background-position:100% 5%}
100%{background-position:0% 96%}
}
@keyframes AnimationName {
0%{background-position:0% 96%}
50%{background-position:100% 5%}
100%{background-position:0% 96%}
}
Parent have an animated background made with gradient animator. All I want it to not restart when details button is pressed (childs updating). Size of parent div changes when button pressed.
Share Improve this question edited Nov 21, 2024 at 20:21 SyndRain 3,7503 gold badges13 silver badges36 bronze badges asked Nov 21, 2024 at 19:44 zombiezombie 231 silver badge3 bronze badges 2- Could you turn this into a snippet so it's easier to replicate please? meta.stackoverflow.com/questions/269753/… – admcfajn Commented Nov 21, 2024 at 19:57
- 1 Not sure where the downvotes came from. The code is minimized and the problem is fully reproducible when pasting directly into a sandbox – SyndRain Commented Nov 21, 2024 at 20:08
1 Answer
Reset to default 2The animation restarts if the animated div's parent rerenders. You can simply move the animated div to a wrapper component.
Codesandbox
function AnimatedCard({ children }) {
return <div className="mydiv">{children}</div>;
}
function Page() {
const [showDetails, setShowDetails] = useState(true);
const PurchaseComponent = () => {
const toggleDetails = useCallback(() => {
setShowDetails((prevShowDetails) => !prevShowDetails);
}, []);
return (
<>
<button onClick={toggleDetails}>Show Details</button>
{showDetails && <div>BlaBla</div>}
</>
);
};
return (
<AnimatedCard>
<PurchaseComponent />
</AnimatedCard>
);
}
export default Page;
本文标签: cssHow to prevent animation reset when div changesStack Overflow
版权声明:本文标题:css - How to prevent animation reset when div changes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307602a1933370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论