If you've worked with animations in CSS so far, you've likely become familiar with easing-function or similar functions that change the speed over time. These functions are quite commonly used in website design and applications and can control the feeling and state of movements in animations. In simple terms, these functions allow us to define how an animation will speed up or slow down over time.
The most common functions for changing the speed in CSS include linear, ease, ease-in, ease-out, and ease-in-out. Each of these functions provides a specific pattern for controlling speed during the animation duration. For example, linear provides a constant movement and speed, while ease-in allows an animation to start slowly and then gradually become faster.
When we want to use an easing-function, it creates a more natural and attractive feeling for the animations. It's also important to be precise in choosing which of these functions has the desired effect. For instance, when we want an element to enter the scene smoothly and reach the center quickly, we might use ease-out. Conversely, if we want this action to happen in reverse, we might employ ease-in.
More advanced functions like cubic-bezier also exist, allowing designers to define more precise and creative speed curves. For instance, using cubic-bezier, I will illustrate further below.
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: all 2s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}
.box:hover {
transform: translateX(200px);
}
</style>
Explanation of line-by-line code
<style>
- This tag is used to define the styles in CSS and gets placed in the HTML document.
.box
- The CSS class used for the element that we want to animate.
transition: all 2s cubic-bezier(0.68, -0.55, 0.27, 1.55);
- Describes the speed change of the animation using cubic-bezier for a duration of 2 seconds.
.box:hover
- An effect that takes place when the mouse hovers over the element.
transform: translateX(200px);
- An action that is executed when the mouse hovers over the element, making it move to the right.