27
loading...
This website collects cookies to deliver better user experience
cubic-bezier()
and how to do the same when it comes to CSS transitions. I was able to create complex hover effect without resorting to keyframes. In this article, I will show you how to create even more complex CSS transitions.@property
feature. It's only supported on Chrome-based browsers for now but we can still play with it and demonstrate how it, too, and can be used to build complex animations.@property
@property
support is still limited.@property --d1 {
syntax: '<number>';
inherits: false;
initial-value: 0;
}
@property --d2 {
syntax: '<number>';
inherits: false;
initial-value: 0;
}
.box {
top: calc((var(--d1) + var(--d2)) * 1%);
transition:
--d1 1s cubic-bezier(0.7, 1200, 0.3, -1200),
--d2 1s cubic-bezier(0.5, 1200, 0.5, -1200);
}
.box:hover {
--d1: 0.2;
--d1: -0.2;
}
--d1
and --d2
. Then, we declare the top
property on a .box
element using the sum of both those properties. Nothing overly complex yet—just calc()
applied to two variables.<number>
and I multiply those values by 1%
to convert them into a percentage. We could define these as <percentage>
right away to avoid the multiplication. But I've chosen numbers instead in favor of more flexibility for more complex operations later..box
is hovered, triggering the animation. But why do we get the result we see in the demo?--d1
, we have a function (let's call it F1
); for --d2
, we have another one (let's call it F2
). That means the value of top is F1 + F2
.--d1
goes from 0
to 30
with an ease-in
timing function--d2
goes from 0
to -20
with an ease-out
timing function
The result? The top value goes from 0
to 10
(30-20
) with a custom timing function (the sum of ease-in
and ease-out
).cubic-bezier()
.cubic-bezier()
may be tricky, so consider using this online curve generator and also refer to my previous article.cubic-bezier()
) to create a third one, complex enough to achieve a fancy transition. The combinations (and possibilities) are unlimited!min()
/max()
to simulate an abs()
function:top
is always a positive value. (I added a margin-top
to make the center of box the reference for 0
.)@property --d1 { /* we do the same for d2 .. dn */
syntax: '<number>';
inherits: false;
initial-value: i1; /* the initial value can be different for each variable */
}
.box {
--duration: 1s; /* the same duration for all */
property: calc(f(var(--d1),var(--d2), .. ,var(--dn))*[1UNIT]);
transition:
--d1 var(--duration) cubic-bezier( ... ),
--d2 var(--duration) cubic-bezier( ... ),
/* .. */
--dn var(--duration) cubic-bezier( ... );
}
.box:hover {
--d1:f1;
--d2:f2;
/* .. */
--dn:f3;
}
@property
to define numeric custom properties, each with an initial value.f
function that is the formula used between the variables. The function provides a number that we use to multiply the relevant unit. All this runs in calc()
applied to the property.f(i1,i2,…,in)
to f(f1,f2,..,fn)
with a custom timing function.transition-delay
property. Let's look back at the interactive demo and apply a delay to one of the variables:cubic-bezier()
that allow us to create complex transitions without keyframes.calc()
.transition-delay
to build a complex timeline.