` element to a trigger named `openClose` with a status expression of `isOpen`, and with possible values of `true` and `false`. This is an alternative to the practice of creating two named states of `open` and `close`.
在上述代码片段中,HTML 模板将 `
` 元素绑定到名为 `openClose` 的触发器,其状态表达式是 `isOpen`,可能的值为 `true` 和 `false`。这种方式可以代替创建两个命名状态 `open` 和 `close` 的方式。
In the component code, in the `@Component` metadata under the `animations:` property, when the state evaluates to `true` (meaning "open" here), the associated HTML element's height is a wildcard style or default. In this case, use whatever height the element already had before the animation started. When the element is "closed," the element animates to a height of 0, which makes it invisible.
在组件代码中,`@Component` 元数据下的 `animations:` 属性中,当该状态求值为 `true` 时(这里表示 "open"),相关 HTML 元素的高度值为通配符样式 `*` 或某个默认值。在这种情况下,它会使用此元素开始动画前的现有高度。当该元素是 "closed" 时,它的高度会从指定的高度运动到 0,这会让它不可见。
## Multiple animation triggers
## 多重动画触发器
You can define more than one animation trigger for a component. You can attach animation triggers to different elements, and the parent-child relationships among the elements affect how and when the animations run.
你可以为组件定义多个动画触发器并将这些动画触发器附着到不同的元素上,这些元素之间的父子关系会影响动画的运行方式和时机。
### Parent-child animations
### 父-子动画
Each time an animation is triggered in Angular, the parent animation always get priority and child animations are blocked. In order for a child animation to run, the parent animation must query each of the elements containing child animations and then allow the animations to run using the [`animateChild()`](https://angular.io/api/animations/animateChild) function.
每次在 Angular 中触发动画时,父动画始终会优先,而子动画会被阻塞。为了运行子动画,父动画必须查询出包含子动画的每个元素,然后使用 [`animateChild()`](https://angular.cn/api/animations/animateChild) 函数来运行它们。
#### Disabling an animation on an HTML element
#### 在某个 HTML 元素上禁用动画
A special animation control binding called `@.disabled` can be placed on an HTML element to disable animations on that element, as well as any nested elements. When true, the `@.disabled` binding prevents all animations from rendering.
可以把一个名叫 `@.disabled` 的动画控制绑定放在 HTML 元素上,以禁用该元素及其子元素上的动画。当 `@.disabled` 绑定为 `true` 时,就会禁止渲染所有动画。
The code sample below shows how to use this feature.
下面的代码范例展示了如何使用此特性。
When the `@.disabled` binding is true, the `@childAnimation` trigger doesn't kick off.
当 `@.disabled` 绑定为 `true` 时,`@childAnimation` 触发器就不会启动。
When an element within an HTML template has animations disabled using the `@.disabled` host binding, animations are disabled on all inner elements as well.
You can't selectively disable multiple animations on a single element.
当 HTML 模板中的某个元素使用 `@.disabled` 禁止了动画时,也会同时禁止其所有内部元素的动画。你无法有选择的单独禁用单个元素上的多个动画。
However, selective child animations can still be run on a disabled parent in one of the following ways:
不过,选择性的子动画仍然可以用如下方式之一在已禁用的父元素上运行:
* A parent animation can use the [`query()`](https://angular.io/api/animations/query) function to collect inner elements located in disabled areas of the HTML template.
Those elements can still animate.
父动画可以使用 [`query()`](https://angular.cn/api/animations/query) 函数来收集 HTML 模板中位于禁止动画区域内部的元素。这些元素仍然可以播放动画。
* A subanimation can be queried by a parent and then later animated with the `animateChild()` function.
子动画可以被父动画查询,并且稍后使用 `animateChild()` 来播放它。
#### Disabling all animations
#### 禁用所有动画
To disable all animations for an Angular app, place the `@.disabled` host binding on the topmost Angular component.
要禁用 Angular 应用中的所有动画,只要把 `@.disabled` 绑定放在顶级的 Angular 组件上即可。
**Note:** Disabling animations application-wide is useful during end-to-end (E2E) testing.
**注意:**禁用应用级的动画在端到端(E2E)测试中是很有用的。
## Animation callbacks
## 动画回调
The animation `trigger()` function emits *callbacks* when it starts and when it finishes. The example below features a component that contains an `openClose` trigger.
当动画启动和终止时,`trigger()` 函数会发出一些*回调*。在下面的例子中,我们有一个包含 `openClose` 触发器的组件。
In the HTML template, the animation event is passed back via `$event`, as `@trigger.start` and `@trigger.done`, where `trigger` is the name of the trigger being used.
In this example, the trigger `openClose` appears as follows.
在 HTML 模板中,动画事件可以通过 `$event` 传递回来,比如 `@trigger.start` 和 `@trigger.done` ,这里的 `trigger` 表示所使用的触发器名字。在我们的例子中,`openClose` 触发器将会是这样的:
A potential use for animation callbacks could be to cover for a slow API call, such as a database lookup.
For example, you could set up the **InProgress** button to have its own looping animation where it pulsates or does some other visual motion while the backend system operation finishes.
动画回调的潜在用途之一,是用来覆盖比较慢的 API 调用,比如查阅数据库。例如,你可以建立一个 **InProgress** 按钮,让它拥有自己的循环动画。当后端系统操作完成时,它会播放脉动效果或其它一些视觉动作。
Then, another animation can be called when the current animation finishes.
For example, the button goes from the `inProgress` state to the `closed` state when the API call is completed.
然后,在当前动画结束时,可以调用另一个动画。比如,当 API 调用完成时,按钮会从 `inProgress` 状态编程 `closed` 状态。
An animation can influence an end user to *perceive* the operation as faster, even when it isn't.
Thus, a simple animation can be a cost-effective way to keep users happy, rather than seeking to improve the speed of a server call and having to compensate for circumstances beyond your control, such as an unreliable network connection.
动画可以影响最终用户,让他*觉得*操作更快 —— 虽然并没有。因此,简单的动画是保持用户满意的一种经济有效的手段,而不必寻求提高服务器调用的速度或被迫补救那些你无法控制的情况,比如不可靠的网络连接。
Callbacks can serve as a debugging tool, for example in conjunction with `console.warn()` to view the application's progress in a browser's Developer JavaScript Console.
The following code snippet creates console log output for the original example, a button with the two states of `open` and `closed`.
回调可以作为调试工具,例如与 `console.warn()` 结合使用,以便在浏览器的开发者控制台中查看应用的进度。下列代码片段为我们原始的双态按钮(`open` 与 `closed`)范例创建了控制台输出。
{@a keyframes}
## Keyframes
## 关键帧动画
The previous section features a simple two-state transition. Now create an animation with multiple steps run in sequence using *keyframes*.
前一节是简单的双态转场。现在,我们要使用*关键帧动画*创建一个具有多个顺序执行步骤的动画。
Angular's `keyframe()` function is similar to keyframes in CSS. Keyframes allow several style changes within a single timing segment.
For example, the button, instead of fading, could change color several times over a single 2-second timespan.
Angular 的 `keyframe()` 函数类似于 CSS 中的关键帧。关键帧允许在单个时间段内进行多种样式更改。例如,我们的按钮可以在单个的 2 秒时间段内多次改变颜色,而不是渐隐掉。
The code for this color change might look like this.
这些更改颜色的代码如下所示:
### Offset
### 偏移
Keyframes include an *offset* that defines the point in the animation where each style change occurs.
Offsets are relative measures from zero to one, marking the beginning and end of the animation, respectively and should be applied to each of the keyframe's steps if used at least once.
关键帧包括一个用来定义动画中每个样式何时开始更改的*偏移(offset)*属性。偏移是个 0 到 1 之间的相对值,分别标记动画的开始和结束时间,并且只要使用了它,就要同样应用于这个关键帧的每个步骤。
Defining offsets for keyframes is optional.
If you omit them, evenly spaced offsets are automatically assigned.
For example, three keyframes without predefined offsets receive offsets of 0, 0.5, and 1.
Specifying an offset of 0.8 for the middle transition in the above example might look like this.
定义关键帧的偏移量是可选的。如果省略它们,就会自动分配均匀间隔的偏移。例如,三个没有预定义偏移的关键帧会分别使用 0、0.5、1 作为偏移。在上面的例子中,还可以为中间的转场指定偏移量 0.8。代码如下:
The code with offsets specified would be as follows.
带有指定偏移量的代码如下:
You can combine keyframes with `duration`, `delay`, and `easing` within a single animation.
你可以在单个动画中组合使用 `duration`、`delay` 和 `easing` 来定义关键帧。
### Keyframes with a pulsation
### 带脉动效果的关键帧
Use keyframes to create a pulse effect in your animations by defining styles at specific offset throughout the animation.
通过在整个动画中定义特定偏移处的样式,可以使用关键帧在动画中创建脉动效果。
Here's an example of using keyframes to create a pulse effect:
下面是使用关键帧创建脉动效果的例子:
* The original `open` and `closed` states, with the original changes in height, color, and opacity, occurring over a timeframe of 1 second.
原始的 `open` 和 `closed` 状态(包括其原始的高度、颜色和透明度)会在一秒钟内逐渐发生变化。
* A keyframes sequence inserted in the middle that causes the button to appear to pulsate irregularly over the course of that same 1-second timeframe.
插在中间的关键帧序列会导致该按钮在一秒钟内出现不规则的脉动。
The code snippet for this animation might look like this.
此动画的代码片段是这样的:
### Animatable properties and units
### 可动的属性与单位
Angular's animation support builds on top of web animations, so you can animate any property that the browser considers animatable.
This includes positions, sizes, transforms, colors, borders, and more. The W3C maintains a list of animatable properties on its [CSS Transitions](https://www.w3.org/TR/css-transitions-1/) page.
Angular 的动画支持是基于 Web 动画的,所以你可以动浏览器认为可动(animatable)的任意属性。包括位置、大小、变形、颜色、边框等。W3C 在 [CSS 转场](https://www.w3.org/TR/css-transitions-1/)页也维护了一个可动属性的列表。
For positional properties with a numeric value, define a unit by providing the value as a string, in quotes, with the appropriate suffix:
对于带有数值的位置属性,可以把值作为字符串(别忘了带引号)并使用适当的后缀来定义其单位:
* 50 pixels: `'50px'`
50 像素:`'50px'`
* Relative font size: `'3em'`
相对字体大小:`'3em'`
* Percentage: `'100%'`
百分比:`'100%'`
If you don't provide a unit when specifying dimension, Angular assumes a default unit of pixels, or px.
Expressing 50 pixels as `50` is the same as saying `'50px'`.
如果在指定尺寸时未提供单位,则 Angular 将使用默认单位像素(px)。把 50 像素表示为 `50` 和 `'50px'` 是一样的。
### Automatic property calculation with wildcards
### 使用通配符自动计算属性
Sometimes you don't know the value of a dimensional style property until runtime.
For example, elements often have widths and heights that depend on their content and the screen size.
These properties are often challenging to animate using CSS.
有时你在运行之前并不知道某个样式的属性值。比如,元素的宽度和高度通常取决于其内容和屏幕大小。在使用 CSS 动画时,这些属性通常会具有挑战性(译注:因为 CSS 动画不支持自动确定宽高)。
In these cases, you can use a special wildcard `*` property value under `style()`, so that the value of that particular style property is computed at runtime and then plugged into the animation.
这些情况下,你可以在 `style()` 中指定通配符 `*` 属性,以便在运行期间计算该属性的值,然后把它插入到动画中。
The following example has a trigger called `shrinkOut`, used when an HTML element leaves the page.
The animation takes whatever height the element has before it leaves, and animates from that height to zero.
下面的例子中有一个名叫 `shrinkOut` 的触发器,它会在 HTML 元素离开页面时使用。该动画会使用它离开之前的任意高度,并从该高度动画到 0。
### Keyframes summary
### 关键帧动画总结
The `keyframes()` function in Angular allows you to specify multiple interim styles within a single transition, with an optional offset to define the point in the animation where each style change occurs.
Angular 中的 `keyframes()` 函数允许你在单个转场中指定多个临时样式,并使用可选的偏移量来定义动画中每次样式变化的发生时机。
## More on Angular animations
## Angular 动画的更多知识
You may also be interested in the following:
你可能还对下列内容感兴趣:
* [Introduction to Angular animations](guide/animations)
[Angular 动画简介](guide/animations)
* [Complex animation sequences](guide/complex-animation-sequences)
[复杂动画序列](guide/complex-animation-sequences)
* [Reusable animations](guide/reusable-animations)
[可复用动画](guide/reusable-animations)
* [Route transition animations](guide/route-animations)
[路由转场动画](guide/route-animations)