사용자 정의 속성 및 Cubic-Bezier ()를 사용하여 복잡한 CSS 전환을 구축하십시오.
I recently illustrated how we can achieve complex CSS animations using 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.
This time, let’s use the @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.
I highly recommend reading my previous article because I will be referring to a few concepts I explained in detail there. Also, please note that the demos in this article are best viewed in Chromium-based browsers while @property support is still limited.
Let’s start with a demo:
Click on the button (more than once) and see the “magic” curve we get. It may look trivial at first glance because we can achieve such effect using some complex keyframes. But the trick is that there is no keyframe in there! That animation is done using only a transition.
Awesome right? And this is only the beginning, so let’s dig in!
The main idea
The trick in the previous example relies on this code:
@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; }
We’re defining two custom properties, --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.
The two properties are defined as Notice that we apply a different transition to each variable—more precisely, a different timing-function with the same duration. It’s actually a different sinusoidal curve for both variables which is something I get deep into in my previous article. From there, the property values change when the .box is hovered, triggering the animation. But why do we get the result we see in the demo? It’s all about math. We are adding two functions to create a third one. For --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. An example to better illustrate: The first two transitions illustrate each variable individually. The third one is the sum of them. Imagine that at in each step of the animation we take the value of both variables and we add them together to get each point along the final curve. Let’s try another example: This time, we combine two parabolic curve to get a… well, I don’t know its name it but it’s another complex curve! This trick is not only limited to the parabolic and sinusoidal curve. It can work with any kind of timing function even if the result won’t always be a complex curve. This time: 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). We are not getting a complex transition in this case—it’s more to illustrate the fact that it’s a generic idea not only limited to cubic-bezier(). I think it’s time for an interactive demo. All you have to do is to adjust a few variables to build your own complex transition. I know cubic-bezier() may be tricky, so consider using this online curve generator and also refer to my previous article. Here are some examples I made: As you can see, we can combine two different timing functions (created using cubic-bezier() ) to create a third one, complex enough to achieve a fancy transition. The combinations (and possibilities) are unlimited! In that last example, I wanted to demonstrate how adding two opposite functions lead to the logical result of a constant function (no transition). Hence, the flat line. You thought we’d stop at only two variables? Certainly not! We can extend the logic to N variables. There is no restriction—we define each one with a timing function and sum them up. An example with three variables: In most cases, two variables are plenty to create a fancy curve, but it’s neat to know that the trick can be extended to more variables. Of course! We can also extend the same idea to consider more operations. We can add, subtract, multiply, divide—and even perform a complex formula between variables. Here, we’re multiplying values: We can also use one variable and multiply it by itself to get a quadratic function! Let’s add more fun in there by introducing min()/max() to simulate an abs() function: Notice that in the second box we will never get higher than the center point on the y-axis because top is always a positive value. (I added a margin-top to make the center of box the reference for 0.) I won’t get into all the math, but you can imagine the possibilities we have to create any kind of timing function. All we have to do is to find the right formula either using one variable or combining multiple variables. Our initial code can be generalized: This is pseudo-code to illustrate the logic: Given this, the property transitions from f(i1,i2,…,in) to f(f1,f2,..,fn) with a custom timing function. We’ve reached the point where we were able to create a complex timing function by combining basic ones. Let’s try another idea that allow us to have more complex timing function: chaining timing functions together. The trick is to run the transitions sequentially using the transition-delay property. Let’s look back at the interactive demo and apply a delay to one of the variables: We are chaining timing functions instead of adding them together for yet another way to create more complex timing functions! Mathematically, it’s still a sum, but since the transitions do not run at the same time, we will be summing a function with a constant, and that simulates the chaining. Now imagine the case with N variables that we are incrementally delayed. Not only can we create complex transitions this way, but we have enough flexibility to build complex timelines. Here is a funny hover effect I built using that technique: You will find no keyframes there. A small action scene is made entirely using one element and a CSS transition. Here is a realistic pendulum animation using the same idea: Or, how about a ball that bounces naturally: Or maybe a ball rolling along a curve: See that? We just created complex animations without a single keyframe in the code! I hope you took three key points away from this article and the previous one: Thanks to these three features, we have no limits when it comes to creating complex animations. 위 내용은 사용자 정의 속성 및 Cubic-Bezier ()를 사용하여 복잡한 CSS 전환을 구축하십시오.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
Let’s add more variables!
Can we subract, multiply and divide variables?
@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;
}
Chaining timing functions
That’s a wrap!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

다른 날, 나는 Corey Ginnivan의 웹 사이트에서 스크롤 할 때 카드 모음이 서로 쌓이는 것을 발견했습니다.

Google Fonts가 새로운 디자인 (트윗)을 출시 한 것을 볼 수 있습니다. 마지막 큰 재 설계와 비교할 때 이것은 훨씬 더 반복적 인 느낌이 듭니다. 차이를 간신히 말할 수 있습니다

프로젝트에 카운트 다운 타이머가 필요한 적이 있습니까? 그런 것은 플러그인에 도달하는 것이 당연하지만 실제로는 훨씬 더 많습니다.

요소 수가 고정되지 않은 경우 CSS를 통해 지정된 클래스 이름의 첫 번째 자식 요소를 선택하는 방법. HTML 구조를 처리 할 때 종종 다른 요소를 만듭니다 ...

플렉스 레이아웃의 보라색 슬래시 영역에 대한 질문 플렉스 레이아웃을 사용할 때 개발자 도구 (d ...)와 같은 혼란스러운 현상이 발생할 수 있습니다.

새로운 프로젝트가 시작될 때, Sass 컴파일은 눈을 깜박이게합니다. 특히 BrowserSync와 짝을 이루는 경우 기분이 좋습니다.

프론트 엔드 개발에서 Windows와 같은 구현 방법 ...
