aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/easing-function/index.html
blob: 0af33c48c2265ed6f679460dce3271e8a62fe966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
title: <easing-function>
slug: Web/CSS/easing-function
translation_of: Web/CSS/easing-function
---
<div>{{CSSRef}}</div>

<p>The <strong><code>&lt;easing-function&gt;</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/Web/CSS/CSS_Types">data type</a> denotes a mathematical function that describes how fast one-dimensional values change during animations. This lets you vary the animation's speed over the course of its duration.</p>

<p>The easing functions in the cubic-bezier subset of easing functions are often called "smooth" easing functions, because they can be used to smooth down the start and end of the animation. They correlate a time ratio to an output ratio, both expressed as {{cssxref("&lt;number&gt;")}}s. For these values, <code>0.0</code> represents the initial state, and <code>1.0</code> represents the final state.</p>

<p><img src="/files/3434/TF_with_output_gt_than_1.png" style="float: left;"><img src="/files/3435/TF_with_output_gt_than_1_clipped.png" style="float: left; margin-right: 5px;">Depending on the specific function used, the calculated output can sometimes grow to be greater than <code>1.0</code> or smaller than <code>0.0</code> during the course of an animation. This causes the animation to go farther than the final state, and then return. For some properties, such as {{cssxref("left")}} or {{cssxref("right")}}, this creates a kind of "bouncing" effect.</p>

<p>However, certain properties will restrict the output if it goes outside an allowable range. For example, a color component greater than <code>255</code> or smaller than <code>0</code> will be clipped to the closest allowed value (<code>255</code> and <code>0</code>, respectively). Some <code>cubic-bezier()</code> curves exhibit this property.</p>

<h2 class="cleared" id="Easing_functions">Easing functions</h2>

<p>CSS supports two kinds of easing functions: the subset of the cubic Bézier curves that are functions, and staircase functions. The most useful of these functions are given a keyword that allows them to be easily referenced.</p>

<h3 id="The_cubic-bezier_class_of_easing_functions"><a id="cubic-bezier()" name="cubic-bezier()">The <code>cubic-bezier()</code> class of easing functions</a></h3>

<p style="float: left;"><img src="/files/3433/cubic-bezier,%20example.png"></p>

<p>The <code>cubic-bezier()</code> functional notation defines a <a href="https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves">cubic Bézier curve</a>. As these curves are continuous, they are often used to smooth down the start and end of the animation and are therefore sometimes called <em>easing functions</em>.</p>

<p>A cubic Bézier curve is defined by four points P<sub>0</sub>, P<sub>1</sub>, P<sub>2</sub>, and P<sub>3</sub>. P<sub>0</sub> and P<sub>3</sub> are the start and the end of the curve and, in CSS these points are fixed as the coordinates are ratios (the abscissa the ratio of time, the ordinate the ratio of the output range). P<sub>0</sub> is <code>(0, 0)</code> and represents the initial time and the initial state, P<sub>3</sub> is <code>(1, 1)</code> and represents the final time and the final state.</p>

<p>Not all cubic Bézier curves are suitable as easing functions as not all are <a href="https://en.wikipedia.org/wiki/Function_%28mathematics%29">mathematical functions</a>; i.e., curves that for a given abscissa have zero or one value. With P<sub>0</sub> and P<sub>3</sub> fixed as defined by CSS, a cubic Bézier curve is a function, and is therefore valid, if and only if the abscissas of P<sub>1</sub> and P<sub>2</sub> are both in the <code>[0, 1]</code> range.</p>

<p>Cubic Bézier curves with the P<sub>1</sub> or P<sub>2</sub> ordinate outside the <code>[0, 1]</code> range may generate <em>bouncing</em> effects.</p>

<p>When you specify an invalid <code>cubic-bezier</code> curve, CSS ignores the whole property.</p>

<h4 id="Syntax">Syntax</h4>

<pre class="syntaxbox">cubic-bezier(<var>x<sub>1</sub></var>, <var>y<sub>1</sub></var>, <var>x<sub>2</sub></var>, <var>y<sub>2</sub></var>)
</pre>

<p>where:</p>

<dl>
 <dt><strong><em>x<sub>1</sub></em>, <em>y<sub>1</sub></em>, <em>x<sub>2</sub></em>, <em>y<sub>2</sub></em></strong></dt>
 <dd>Are {{cssxref("&lt;number&gt;")}} values representing the abscissas, and ordinates of the P<sub>1</sub> and P<sub>2</sub> points defining the cubic Bézier curve. x<sub>1</sub> and x<sub>2</sub> must be in the range [0, 1] or the value is invalid.</dd>
</dl>

<h4 id="Examples">Examples</h4>

<p>These cubic Bézier curves are valid for use in CSS:</p>

<pre class="brush: css">/* The canonical Bézier curve with four &lt;number&gt; in the [0,1] range. */
cubic-bezier(0.1, 0.7, 1.0, 0.1)

/* Using &lt;integer&gt; is valid as any &lt;integer&gt; is also a &lt;number&gt;. */
cubic-bezier(0, 0, 1, 1)

/* Negative values for ordinates are valid, leading to bouncing effects.*/
cubic-bezier(0.1, -0.6, 0.2, 0)

/* Values &gt; 1.0 for ordinates are also valid. */
cubic-bezier(0, 1.1, 0.8, 4)
</pre>

<p>These cubic Bézier curves definitions are invalid:</p>

<pre class="brush: css example-bad">/* Though the animated output type may be a color,
   Bézier curves work w/ numerical ratios.*/
cubic-bezier(0.1, red, 1.0, green)

/* Abscissas must be in the [0, 1] range or
   the curve is not a function of time. */
cubic-bezier(2.45, 0.6, 4, 0.1)

/* The two points must be defined, there is no default value. */
cubic-bezier(0.3, 2.1)

/* Abscissas must be in the [0, 1] range or
   the curve is not a function of time. */
cubic-bezier(-1.9, 0.3, -0.2, 2.1)
</pre>

<h3 id="Keywords_for_common_cubic-bezier_easing_functions">Keywords for common cubic-bezier easing functions</h3>

<h4 id="linear"><code>linear</code></h4>

<p style="float: left;"><img src="/files/3425/cubic-bezier,linear.png" style="height: 325px; width: 244px;"></p>

<p>动画以恒定速度运行。此关键词表示缓冲函数  <code>cubic-bezier(0.0, 0.0, 1.0, 1.0)</code></p>

<h4 class="cleared" id="ease"><code>ease</code></h4>

<p style="float: left;"><img src="/files/3429/cubic-bezier,ease.png" style="height: 332px; width: 244px;"></p>

<p>动画缓慢开始,然后突然加速,最后缓慢移向目标。此关键词表示缓冲函数 <code>cubic-bezier(0.25, 0.1, 0.25, 1.0)</code>.。它与 <code><a href="#ease-in-out">ease-in-out</a></code> 类似,但它在开始时加速更快。 </p>

<h4 class="cleared" id="ease-in"><code>ease-in</code></h4>

<p style="float: left;"><img src="/files/3426/cubic-bezier,ease-in.png" style="height: 325px; width: 244px;"></p>

<p>动画缓慢开始,然后逐渐加速直到结束,在结束点时突然停止。此关键词表示缓冲函数 <code>cubic-bezier(0.42, 0.0, 1.0, 1.0)</code></p>

<h4 class="cleared" id="ease-in-out"><code>ease-in-out</code></h4>

<p style="float: left;"><img src="/files/3428/cubic-bezier,ease-in-out.png" style="height: 332px; width: 244px;"></p>

<p>动画缓慢开始,然后加速,最后减速直至结束。此关键词表示缓冲函数 <code>cubic-bezier(0.42, 0.0, 0.58, 1.0)</code>。开始时,其表现与 <a href="#ease-in"><code>ease-in</code></a> 函数类似;结束时,与 <a href="#ease-out"><code>ease-out</code></a> 函数类似。</p>

<h4 class="cleared" id="ease-out"><code>ease-out</code></h4>

<p style="float: left;"><img src="/files/3427/cubic-bezer,ease-out.png" style="height: 325px;"></p>

<p>此动画突然开始,然后逐渐减速直至结束。此关键词表示缓冲函数 <code>cubic-bezier(</code><code>0.0, 0.0, 0.58, 1.0</code><code>)</code></p>

<p class="cleared" id="sect1"></p>

<h3 class="cleared" id="The_steps_class_of_easing_functions"><a id="steps()" name="steps()">The <code>steps()</code> class of easing functions</a></h3>

<p>The <code>steps()</code> functional notation defines a <a href="https://en.wikipedia.org/wiki/Step_function">step function</a> dividing the domain of output values in equidistant steps.This subclass of step functions are sometimes also called staircase functions.</p>

<h4 id="Syntax_2">Syntax</h4>

<pre class="syntaxbox">steps(<var>number_of_steps</var>, <var>direction</var>)
</pre>

<p>where:</p>

<dl>
 <dt><var>number_of_steps</var></dt>
 <dd>Is a strictly positive {{cssxref("&lt;integer&gt;")}}, representing the amount of equidistant treads composing the stepping function.</dd>
 <dt><var>direction</var></dt>
 <dd>Is a keyword indicating if it the function is <a href="https://en.wikipedia.org/wiki/Left-continuous#Directional_and_semi-continuity">left- or right-continuous</a>:</dd>
 <dd>
 <ul>
  <li><code>jump-start</code> denotes a left-continuous function, so that the first step or jump happens when the animation begins;</li>
  <li><code>jump-end</code> denotes a right-continuous function, so that the last step or jump happens when the animation ends;</li>
  <li><code>jump-both</code> denotes a right and left continuous function, includes pauses at both the 0% and 100% marks, effectively adding a step during the animation iteration;</li>
  <li><code>jump-none</code> There is no jump on either end. Instead, holding at both the 0% mark and the 100% mark, each for 1/n of the duration</li>
  <li><code>start</code> is the equivalent of <code>jump-start</code></li>
  <li><code>end</code> is the equivalent of <code>jump-end</code></li>
 </ul>
 </dd>
 <dd><code>end</code> is the default.</dd>
</dl>

<h4 id="steps_n_&lt;direction>"><code>steps( n, &lt;direction&gt; )</code></h4>

<ul style="list-style-type: none; display: flex;">
 <li>
  <p><code>steps(2, jump-start)</code><br>
   <code>steps(2, start)</code></p>
  <img src="/files/3436/steps(2,start).png" style="float: left; height: 327px; vertical-align: top;"></li>
 <li>
  <p><code>steps(4, jump-end)<br>
   steps(4, end)</code></p>
  <img alt="four steps, with a jump from the fourth step to the final value at the 100% mark" src="/files/3437/steps(4,end).png"></li>
 <li>
  <p><code>steps(5, jump-none)</code></p>
  <img alt="five steps, with no jumps, so 20% of the time is at the beginning state or 0% mark and the last 20% is at the final state, or 100% mark" src="/files/16419/step5none.png"></li>
 <li>
  <p><code>steps(3, jump-both)</code></p>
  <img src="https://mdn.mozillademos.org/files/16420/step3both.png"></li>
</ul>

<dl>
 <dt><code>steps-start</code></dt>
 <dd>The equivalent of <code>steps(1, jump-start)</code></dd>
 <dt><code>steps-end</code></dt>
 <dd>The equivalent of <code>steps(1, jump-end)</code></dd>
</dl>

<h4 class="cleared" id="step-start"><code>step-start</code></h4>

<p><img src="/files/3423/steps(1,start).png" style="float: left; height: 327px; vertical-align: top;"> The animation jumps immediately to its final state, where it stays until the end. This keyword represents the easing function <code>steps(1, jump-start)</code> or <code>steps(1, start)</code>.</p>

<h4 class="cleared" id="step-end"><code>step-end</code></h4>

<p><img src="/files/3424/steps(1,end).png" style="float: left; height: 327px; vertical-align: top;"> The animation stays in its initial state until the end, at which point it jumps directly to its final state. This keyword represents the easing function <code>steps(1, jump-end)</code> or <code>steps(1, end)</code>.</p>

<h4 class="cleared" id="Examples_2">Examples</h4>

<p>These easing functions are valid:</p>

<pre class="brush: css">/* There is 5 treads, the last one happens
   right before the end of the animation. */
steps(5, end)

/* A two-step staircase, the first one happening
   at the start of the animation. */
steps(2, start)

/* The second parameter is optional. */
steps(2)
</pre>

<p>These easing function are invalid:</p>

<pre class="brush: css example-bad">/* The first parameter must be an <a href="/en-US/docs/Web/CSS/integer">&lt;integer&gt;</a> and
   cannot be a real value, even if it is equal to one. */
steps(2.0, jump-end)

/* The amount of steps must be non-negative. */
steps(-3, start)

/* There must be at least one step.*/
steps(0, jump-none)</pre>

<h2 class="cleared" id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th>Specification</th>
   <th>Status</th>
   <th>Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Animations', '#animation-timing-function', '&lt;timing-function&gt;')}}</td>
   <td>{{Spec2('CSS3 Animations')}}</td>
   <td>Defines <code>&lt;single-timing-function&gt;</code> as a synonym for <code>&lt;single-transition-timing-function&gt;</code> of the CSS Transitions Module.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS Timing 1', '#typedef-easing-function', '&lt;easing-function&gt;')}}</td>
   <td>{{Spec2('CSS Timing 1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("css.types.easing-function", 2)}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/CSS/CSS_Animations">CSS Animations</a></li>
 <li><a href="/en-US/docs/Web/CSS/CSS_Transitions">CSS Transitions</a></li>
 <li><a href="http://cubic-bezier.com/">cubic-bezier</a></li>
</ul>