19

I want to animate two (or more) CSS transform properties separately using keyframe animation like this:

@keyframes translatex {
    100% {
        transform: translateX(100px);
    }
}
@keyframes rotatez {
    100% {
        transform: rotateZ(80deg);
    }
}

HTML:

<div class="rect"></div>

The translatex animation should start with a 0s delay and last for 5 seconds. The rotatez animation should start with a 1s delay and last for 3 seconds. The .rect element starts moving, then after 1 second it starts rotating, then after 3 seconds it stops rotating and after 1 more second it finishes its movement.

Apply animation:

.rect {
    animation-name: translatex, rotatez;
    animation-duration: 5s, 3s;
    animation-timing-function: ease, ease-in-out;
    animation-delay: 0s, 1s;
    animation-direction: forward, forward;
}

The problem is that only the rotatez animation is applied.

Are there ways to implement the animation using only CSS, such as keyframe animation or transitions, or do I need JavaScript and requestAnimationFrame?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Dmitry
  • 245
  • 1
  • 2
  • 9

1 Answers1

21

Yes, it is possible. Instead of calling two animation-names, create only one animation with both actions inside:

@keyframes translateXandZ {
    100% {
        transform: translateX(100px) rotateZ(80deg);
    }
}

Look at Google's "Animate your HTML5" presentation.

Here is a workaround, even though it is a bit of a coarse version:

@-webkit-keyframes translateXandZ {
    0% {-webkit-transform: translateX(0px) rotateZ(0deg);}
    2% {-webkit-transform: translateX(1px) rotateZ(0deg);}
    5% {-webkit-transform: translateX(3px) rotateZ(0deg);}
    20% {-webkit-transform: translateX(20px) rotateZ(0deg);}
    80% {-webkit-transform: translateX(80px) rotateZ(80deg);}
    95% {-webkit-transform: translateX(97px) rotateZ(80deg);}
    98% {-webkit-transform: translateX(99px) rotateZ(80deg);}
    100% {-webkit-transform: translateX(100px) rotateZ(80deg);}
}

Your animation is linear, but to make it ease-in-out, I played with the beginning and ending of the animation. It's still not perfect, but this is the only way I see how you could get what you want.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
leMoisela
  • 1,046
  • 8
  • 17
  • 6
    Thanks for helpful link. The problem is that in your case both properties animate together and I can use only one timing function and delay for them. What I need is separate transform properties animation. – Dmitry Nov 27 '12 at 08:58
  • 1
    You don’t necessarily have to simulate `ease-in-out` manually with percentages. The [CSS Animations spec](http://dev.w3.org/csswg/css-animations/#timing-functions) says you can specify the interpolation function to use when moving to the next keyframe with the `animation-timing-function` property of each keyframe. This can replace manual interpolation as long as you’re not moving two things at the same time with different animation lengths. – Rory O'Kane Sep 09 '13 at 16:04
  • this fiddle is gone :< does anyone still have a link? – Shanon Jackson May 24 '18 at 22:03
  • :O Sorry it disappeared! I guess they throw away code after some time. I don't have the solution at hand anymore – leMoisela May 25 '18 at 07:10