aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/effecttiming/easing/index.html
blob: 0ee0bfea9fe4db9163cd8fb7a3f510bb7517f629 (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
---
title: EffectTiming.easing
slug: Web/API/EffectTiming/easing
translation_of: Web/API/EffectTiming/easing
---
<div>{{ SeeCompatTable() }}{{ APIRef("Web Animations") }}</div>

<p>这个{{domxref("EffectTiming")}} 词的 <strong><code>easing</code></strong> 属性在 <a href="/en-US/docs/Web/API/Web_Animations_API">Web Animations API</a> 指定用于缩放时间以产生缓和效果的计时函数,<em>easing</em> 是动画随时间变化的速率。</p>

<div class="note">
<p>{{domxref("Element.animate()")}}, {{domxref("KeyframeEffectReadOnly.KeyframeEffectReadOnly", "KeyframeEffectReadOnly()")}}, and {{domxref("KeyframeEffect.KeyframeEffect", "KeyframeEffect()")}} all accept an object of timing properties including <code>easing</code>. The value of easing corresponds directly to {{domxref("AnimationEffectTimingReadOnly.easing")}} in {{domxref("AnimationEffectReadOnly.timing", "timing")}} objects returned by {{domxref("AnimationEffectReadOnly")}}, {{domxref("KeyframeEffectReadOnly")}}, and {{domxref("KeyframeEffect")}}.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox notranslate">var <em>timingProperties</em> = {
  easing: {{cssxref("single-transition-timing-function")}}
}

<em>timingProperties</em>.easing = {{cssxref("single-transition-timing-function")}}
</pre>

<h3 id="Value">Value</h3>

<p>A string defining the timing function to use for easing transitions during the animation process. Accepts several pre-defined {{domxref("DOMString")}} values, a <code>steps()</code> timing function like <code>steps(5, end)</code>, or a custom <code>cubic-bezier</code> value like <code>cubic-bezier(0.42, 0, 0.58, 1)</code>. Defaults to <code>linear</code>. Available values include:</p>

<dl>
 <dt><code>linear</code></dt>
 <dd>A constant rate of change, neither accelerating nor deccelerating. </dd>
 <dt><code>cubic-bezier(&lt;number&gt;, &lt;number&gt;, &lt;number&gt;, &lt;number&gt;)</code></dt>
 <dd><img alt="A diagram showing the points of a cubic bezier timing function." src="https://www.w3.org/TR/css-timing-1/cubic-bezier-timing-curve.svg" style="height: 150px; width: 400px;"><br>
 Specifies <a href="https://w3c.github.io/web-animations/#cubic-bzier-timing-function">a cubic Bézier timing function</a>. The four numbers specify points P1 and P2 of the curve as (x1, y1, x2, y2). Both x values must be in the range [0, 1] or the definition is invalid.</dd>
 <dt><code>ease</code></dt>
 <dd>A decelerated rate of change, going from fast to slow. Equivalent to <code>cubic-bezier(0.25, 0.1, 0.25, 1)</code>.</dd>
 <dt><code>ease-in</code></dt>
 <dd>An accelerated rate of change, going from slow to fast. Equivalent to <code>cubic-bezier(0.42, 0, 1, 1)</code>.</dd>
 <dt><code>ease-out</code></dt>
 <dd>变化速率变慢了,从快到慢, 等价于<code>cubic-bezier(0, 0, 0.58, 1)</code>.</dd>
 <dt><code>ease-in-out</code></dt>
 <dd>This rate of change speeds up in the middle. Equivalent to <code>cubic-bezier(0.42, 0, 0.58, 1)</code>.</dd>
 <dt class="hidden"><code>frames(&lt;integer&gt;)</code></dt>
 <dd class="hidden">Specifies a <a href="https://www.w3.org/TR/css-timing-1/#frames-timing-functions">frames timing function</a>, which breaks the animation down into a number of equal time intervals but also displays the start (0%) and end (100%) states for an equal amount of time to the other intervals. The browser flips to a different static frame when each interval is reached, rather than animating smoothly. See GitHub for a <a href="https://mdn.github.io/css-examples/animation-frames-timing-function/index-waa.html">simple example</a> that illustrates the difference between <code>steps()</code> and <code>frames()</code>.</dd>
 <dt><code>steps(&lt;integer&gt;[, [ start | end ] ]?)</code></dt>
 <dd><img alt="A diagram of the various steps timing functions." src="https://www.w3.org/TR/css-timing-1/step-timing-func-examples.svg" style="height: 238px; width: 500px;"><br>
 Specifies <a href="https://w3c.github.io/web-animations/#step-timing-function">a step timing function</a>, which breaks the animation down into a number of equal time intervals. The browser flips to a different static frame when each interval is reached, rather than animating smoothly. The first parameter specifies the number of intervals in the function. It must be a positive integer (greater than 0). The second parameter, which is optional, specifies the point at which the change of values occur within the interval. If the second parameter is omitted, it is given the value end.</dd>
 <dt><code>step-start</code></dt>
 <dd>Equivalent to <code>steps(1, start)</code></dd>
 <dt><code>step-end</code></dt>
 <dd>Equivalent to <code>steps(1, end)</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>In the <a href="http://codepen.io/rachelnabors/pen/PNGGaV?editors=0010">Red Queen's Race</a> example, we animate Alice and the Red Queen by passing an easing <code>of steps(7, end)</code> to <code>animate()</code>:</p>

<pre class="brush: js notranslate">// Define the key frames
var spriteFrames = [
  { transform: 'translateY(0)' },
  { transform: 'translateY(-100%)' }
];

// Get the element that represents Alice and the Red Queen
var redQueen_alice_sprite = document.getElementById('red-queen_and_alice_sprite');

// Animate Alice and the Red Queen using steps()
var redQueen_alice = redQueen_alice_sprite.animate(
spriteFrames, {
  easing: 'steps(7, end)',
  direction: "reverse",
  duration: 600,
  playbackRate: 1,
  iterations: Infinity
});</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Web Animations', '#time-transformations', 'easing' )}}</td>
   <td>{{Spec2('Web Animations')}}</td>
   <td>Editor's draft.</td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("api.EffectTiming.easing", 2)}}</p>

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

<ul>
 <li><a href="/en-US/docs/Web/API/Web_Animations_API">Web Animations API</a></li>
 <li>{{domxref("Element.animate()")}}, {{domxref("KeyframeEffect.KeyframeEffect", "KeyframeEffect()")}}, and {{domxref("AnimationEffect.updateTiming()")}} all accept an object of timing properties including this one.</li>
 <li>The value of this property corresponds to the one in {{domxref("EffectTiming")}} (which is returned from {{domxref("AnimationEffect.getTiming()")}} and {{domxref("AnimationEffect.getComputedTiming()")}}).</li>
 <li>CSS's <code><a href="/en-US/docs/Web/CSS/animation-timing-function">animation-timing-function</a></code> and <code><a href="/en-US/docs/Web/CSS/transition-timing-function">transition-timing-function</a></code>.</li>
</ul>