I’m trying to make an animation in css and for the first frame, so at 0%, I need the size of the element before his data-space
attribute change before collapsing something like that.
Something like that
#settings {
display: grid;
grid-template-columns: repeat(8, 64px);
grid-template-rows: repeat(2, 64px);
}
#settings[data-space=collapsed] {
width: 50px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
}
#settings[data-space=collapsed][data-firstAnimation=false] {
animation: collapseSettings 1s ease 0s 1 normal none;
}
@keyframes collapseSettings {
0% {
width: /*something to get the width before collapsing*/
height: /*something to get the height before collapsing*/
}
/*...*/
}
2
Answers
I resolved my issue using JS, before change the attribute
data-space
tocollapsed
I save in a JS variable the size of my element then I change the attribute and I start the animation setting thewidth
and theheight
using CSS variables whitch are set at 0px and just before starting the animation I change value of my variables using the JS functiondocument.documentElement.style.setProperty()
. So it give me somthing like that.what you are looking for is most likely
animation-fill-mode
CSS property.using this you can specify how your element will look like after the animation
so, you can do something like:
then you can add the final result of after the animation like this :
or you can shorthand with the animation
check: mdn docs for animation-fill-mode