How To Create Advanced Animations With CSS
About The Author
Yosra Emad is a software engineering student and a content creator/technical writer. One of her main goals is to give back to the community and help lower the …
Note: This article assumes that you have basic knowledge of CSS animations. If you don’t, please check out this guide by Tom Waterhouse before proceeding with the article.
In this article, Yosra Emad explains how to create a rollercoaster path that a ball follows using cubic beziers and CSS transitions. You’ll also learn how the cubic-bezier function in CSS works in detail and how to stack multiple simple animations to create one complex one.
We surf the web daily, and as developers, we tend to notice subtle details on a website. The one thing I take note of all the time is how smooth the animations on a website are. Animation is great for UX and design purposes. You can make an interactive website that pleases the visitor and makes them remember your website.
Creating advanced animations sounds like a hard topic, but the good news is, in CSS, you can stack multiple simple animations after each other to create a more complex one!
In this blog post, you will learn the following:
Note: This article assumes that you have basic knowledge of CSS animations. If you don’t, please check out this link before proceeding with this article.
Cubic Beziers: What Are They?
The cubic-bezier function in CSS is an easing function that gives you complete control of how your animation behaves with respect to time. Here is the official definition:
Note: If you want to learn more about easing functions, you can check out this article. It goes behind the scenes of how linear, cubic-bezier, and staircase functions work!
But What Is An Easing Function?
Let’s Start With A Linear Curve
Imagine two points P0 and P1, where P0 is the starting point of the animation and P1 is the ending point. Now imagine another point moving linearly between the two points as follows:
This is called a linear curve! It is the simplest animation out there, and you probably used it before when you started learning CSS.
Next Up: The Quadratic Bezier Curve
Imagine you have three points: P0, P1 and P2. You want the animation to move from P0 to P2. In this case, P1 is a control point that controls the curve of the animation.
The idea of the quadratic bezier is as follows:
Note that Q1, Q2 and B do not move with the same velocity. They must all start at the same time and finish their path at the same time as well. So each point moves with the appropriate velocity based on the line length it moves along.
Finally: The Cubic Bezier Curve
The cubic bezier curve consists of 4 points: P0, P1, P2 and P3. The animation starts at P0 and ends at P3. P1 and P2 are our control points.
The cubic bezier works as follows:
If you want to have a better feel for how cubic beziers work, I recommend checking out this desmos link. Play around with the control points and check how the animation changes through time. (Note that the animation in the link is represented by the black line.)
More after jump! Continue reading below ↓
Smashing Online Workshops on front-end & UX
, with practical takeaways, live sessions, video recordings
and a friendly Q&A. On design systems, UX, web performance and CSS/JS. With Brad Frost, Stephanie Troeth and so many others.
Jump to online workshops ↬
Meet Smashing Online Workshops on front-end & UX, with practical takeaways, live sessions, video recordings and a friendly Q&A. On design systems, UX, web performance and CSS/JS. With Brad Frost, Stephanie Troeth and so many others.
Big animations with lots of steps can be broken down into multiple small animations. You can achieve that by adding the
animation-delay property to your CSS. Calculating the delay is simple; you add up the time of all the animations before the one you are calculating the animation delay for.
Here, we have two animations,
movePointDown. The animation delay for
movePointLeft will be zero because it is the animation we want to run first. However, the animation delay for
movePointDown will be four seconds because
movePointLeft will be done after that time.
animation-delay property will be as follows:
Note that if you have two or more animations starting at the same time, their animation delay will be the same. In addition, when you calculate the animation delay for the upcoming animations, you will consider them as one animation.
Assume we want to start
y simultaneously. In this case, the animation delay for both
y will be zero, while the animation delay for the jump animation will be four seconds (not eight!).
Creating The Rollercoaster
Now that we have the basics covered, it’s time to apply what we learned!
Understanding The Animation
The rollercoaster path consists of three parts:
Setting Things Up
We will start by creating a simple ball that will be our “cart” for the rollercoaster.
1. Add this to the body of your new HTML file:
2. Add this to your CSS file:
I’ll use viewport width (
vw) and viewport height (
vh) properties to make the animation responsive. You are free to use any units you want.
The Sliding Part
Creating the part where the ball slides can be done using the cubic-bezier function! The animation is made up of 2 animations, one along the
x-axis and the other along the
x-axis animation is a normal linear animation along the
x-axis. We can define its keyframes as follows:
Add it to your animation property in the ball path as follows:
y-axis animation is the one where we will use the cubic-bezier function. Let’s first define the keyframes of the animation. We want the difference between the starting and ending points to be so small that the ball reaches almost the same height.
Now let’s think about the cubic-bezier function. We want our path to move slowly to the right first, and then when it slides, it should go faster.
Now you have your cubic-bezier function, it will be
cubic-bezier(0.55, 0, 0.2, -800).
Let’s add keyframes to our animation property:
This is the first part of our animation, so the animation delay is zero. We should add an
animation-delay property because starting from the following animation, the animations will start at a different time than the first animation.
See the Pen [Rollercoaster sliding part [forked]](https://codepen.io/smashingmag/pen/VwxXBQb) by Yosra Emad.
Adding Horizontal Space
Before making the loop, the ball should move along the
x-axis for a short while, so there is space between both animations. So, let’s do that!
This animation should start after the sliding animation, and the sliding animation takes four seconds; thus, the animation delay will be four seconds:
See the Pen [Rollercoaster horizontal space [forked]](https://codepen.io/smashingmag/pen/dyemExY) by Yosra Emad.
The Loop Part
To create a circle (loop) in CSS, we need to move the circle to the center of the loop and start the animation from there. We want the circle’s radius to be
100px, so we will change the circle position to
top: 20vh (30 is desired radius (
10vh here)). However, this needs to happen after the sliding animation is done, so we will create another animation with a zero-second duration and add a suitable animation delay.
The Loop Itself
To create a loop animation:
See the Pen [Rollercoaster loop [forked]](https://codepen.io/smashingmag/pen/mdLxZdR) by Yosra Emad.
Adding Horizontal Space (Again)
We’re almost done! We just need to move the ball after the animation along the
x-axis so that the ball doesn’t stop exactly after the loop the way it does in the picture above.
The Final Output
See the Pen [Rollercoaster Final [forked]](https://codepen.io/smashingmag/pen/wvjmLKp) by Yosra Emad.
In this article, we covered how to combine multiple keyframes to create a complex animation path. We also covered cubic beziers and how to use them to create your own easing function. I would recommend going on and creating your own animation path to get your hands dirty with animations. If you need any help or want to give feedback, you’re more than welcome to send a message to any of the links here. Have a wonderful day/night!
(vf, il, yk)
This content was originally published here.