The problem is that the transform
property's value has multiple part like translate
, scale
etc.
This is a theoretical question about element, let's .loader
that has transform:translate(10px, 10px)
and in the animation I want to animate the scale
property. In this case, the browser will not take the transform:translate(10px, 10px)
and will take only the scale
.
I am looking for a way around this problem.
Here is an example to this question. Please, keep attention that I'm not looking for an answer to this particular example (like: wrap the element or add the translate
value to the animation definition) but a generic solution (if exist, of course).
.loading {
position: relative;
width: 100px;
height: 100px;
background: #eee;
}
.loading:before,
.loading:after {
content: "";
width: 50%;
height: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
background-color: #fff;
opacity: 0.6;
position: absolute;
top: 0;
left: 0;
/* the broswer doesn't take this */
transform: translate(100px, 300px);
-webkit-animation: bounce 2s infinite ease-in-out;
animation: bounce 2s infinite ease-in-out;
}
.loading:after {
-webkit-animation-delay: -1s;
animation-delay: -1s;
}
@keyframes bounce {
0%, 100% {
transform: scale(0);
-webkit-transform: scale(0);
}
50% {
transform: scale(1);
-webkit-transform: scale(1);
}
}
<div class="loading"></div>
Generally when you add an
animation
with changes to thetransform
property then the transforms that are specified in the base element should also be carried over to be present within the animation's keyframes also. That is, the new transforms (that are part of the animation) should be added over on top of the existingtransform
and not overwrite it. Below is how it should be done.I wrote a similar answer here to a question about adding multiple animations on an element with each of those animations modifying the
transform
property's values independent of the other. I am linking it here only for reference and don't think they are duplicates.Having said the above, adding the the original transform to each animation's kefyrames is not possible when you are trying to create animation libraries or trying to split each animation into a separate class. Say for example, you want to add the same
bounce
animation to multiple elements and each of them have a different initialtransform
setting then it becomes impossible to add it to animation's keyframe.In such cases, you can still achieve the desired output using CSS but it would be very difficult (almost impossible in my opinion) to get it done with a single element.
What options do you have? Well, one option is for you to add the animation on a wrapper element.
The solution is quite generic and you can apply it to almost all such cases. The drawback is that if you want to stack multiple such transformations then you'd likely end up with multiple such wrappers. There is no pure CSS way other than adding original transformations within the animation's keyframes also.
The below snippet is another sample.
You can delete the
translate(100px, 300px);
in.loading:after
, then set thetranslate(100px, 300px)
in@keyframes
, like follows: