I have this simple code where I have A Parent div and a Child Div, both have their own Animations and a button that start the animation and both have an onAnimationEnd Event that display on the console a message.
Whenever I click on the parent button the animation start and when it's over I have the right message on the console.
Parent Ended Animation
till now No Problems.
when I click on the Child button the animation start and when it's over I get the Child's message and the parent's message.
Child Ended Animation App.
Parent Ended Animation App
.
and that is my problem.
So How Can I stop the parent event from running when the child animation is over?
const ParentAnimation = keyframes`
from {
background-color: red;
}to {
background-color: blue;
}
`
const Parent = styled.div`
background-color: red;
height: 300px;
width: 300px;
animation: ${(props) =>
props.animate &&
css`
${ParentAnimation} .5s
`};
`
const ChildAnimation = keyframes`
from {
background-color: green;
}to {
background-color: yellow;
}
`
const Child = styled.div`
background-color: green;
height: 100px;
width: 100px;
position: relative;
top: 100%;
animation: ${(props) =>
props.animate &&
css`
${ChildAnimation} .5s
`};
`
function App() {
const [animeteParent, setAnimeteParent] = useState(false)
const [animeteChild, setAnimeteChild] = useState(false)
return (
<Parent
animate={animeteParent}
onAnimationEnd={() => console.log('Parent Ended Animation')}
>
<Child
animate={animeteChild}
onAnimationEnd={() => console.log('Child Ended Animation')}
>
</Child>
<button
onClick={() => {
setAnimeteParent(true)
}}
>
Animate Parent
</button>
<button
onClick={() => {
setAnimeteChild(true)
}}
>
Animate Child
</button>
</Parent>
)
}