You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aio/content/guide/transition-and-triggers.md
+45-55Lines changed: 45 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,11 @@
1
-
# Animations transitions and triggers
1
+
# Animation transitions and triggers
2
2
3
-
You learned the basics of Angular animations in the [introduction](guide/animations) page.
4
-
5
-
This guide goes into greater depth on special transition states such as `*`\(wildcard\) and `void`, and shows how these special states are used for elements entering and leaving a view.
6
-
This chapter also explores multiple animation triggers, animation callbacks, and sequence-based animation using keyframes.
3
+
This guide goes into depth on special transition states such as the `*` wildcard and `void`. It shows how these special states are used for elements entering and leaving a view.
4
+
This section also explores multiple animation triggers, animation callbacks, and sequence-based animation using keyframes.
7
5
8
6
## Predefined states and wildcard matching
9
7
10
-
In Angular, transition states can be defined explicitly through the [`state()`](api/animations/state) function, or using the predefined `*`\(wildcard\) and `void` states.
8
+
In Angular, transition states can be defined explicitly through the [`state()`](api/animations/state) function, or using the predefined `*` wildcard and `void` states.
11
9
12
10
### Wildcard state
13
11
@@ -25,18 +23,18 @@ For example, a transition of `open => *` applies when the element's state change
25
23
The following is another code sample using the wildcard state together with the previous example using the `open` and `closed` states.
26
24
Instead of defining each state-to-state transition pair, any transition to `closed` takes 1 second, and any transition to `open` takes 0.5 seconds.
27
25
28
-
This lets us add new states without having to include separate transitions for each one.
26
+
This allows the addition of new states without having to include separate transitions for each one.
### Using wildcard state with multiple transition states
34
+
### Use wildcard state with multiple transition states
37
35
38
36
In the two-state button example, the wildcard isn't that useful because there are only two possible states, `open` and `closed`.
39
-
In general, use wildcard states when an element in one particular state has multiple potential states that it can change to.
37
+
In general, use wildcard states when an element has multiple potential states that it can change to.
40
38
If the button can change from `open` to either `closed` or something like `inProgress`, using a wildcard state could reduce the amount of coding needed.
41
39
42
40
<divclass="lightbox">
@@ -50,12 +48,12 @@ If the button can change from `open` to either `closed` or something like `inPro
50
48
The `* => *` transition applies when any change between two states takes place.
51
49
52
50
Transitions are matched in the order in which they are defined.
53
-
Thus, you can apply other transitions on top of the `* => *`\(any-to-any\)transition.
54
-
For example, define style changes or animations that would apply just to `open => closed`, or just to `closed => open`, and then use `* => *` as a fallback for state pairings that aren't otherwise called out.
51
+
Thus, you can apply other transitions on top of the `* => *` transition.
52
+
For example, define style changes or animations that would apply just to `open => closed`, then use `* => *` as a fallback for state pairings that aren't otherwise called out.
55
53
56
54
To do this, list the more specific transitions *before*`* => *`.
57
55
58
-
### Using wildcards with styles
56
+
### Use wildcards with styles
59
57
60
58
Use the wildcard `*` with a style to tell the animation to use whatever the current style value is, and animate with that.
61
59
Wildcard is a fallback value that's used if the state being animated isn't declared within the trigger.
@@ -67,15 +65,15 @@ Wildcard is a fallback value that's used if the state being animated isn't decla
67
65
Use the `void` state to configure transitions for an element that is entering or leaving a page.
68
66
See [Animating entering and leaving a view](#enter-leave-view).
69
67
70
-
### Combining wildcard and void states
68
+
### Combine wildcard and void states
71
69
72
70
Combine wildcard and void states in a transition to trigger animations that enter and leave the page:
73
71
74
72
* A transition of `* => void` applies when the element leaves a view, regardless of what state it was in before it left
75
73
* A transition of `void => *` applies when the element enters a view, regardless of what state it assumes when entering
76
74
* The wildcard state `*` matches to *any* state, including `void`
77
75
78
-
## Animating entering and leaving a view
76
+
## Animate entering and leaving a view
79
77
80
78
This section shows how to animate elements entering or leaving a page.
81
79
@@ -90,7 +88,7 @@ In the preceding code, you applied the `void` state when the HTML element isn't
90
88
91
89
<aid="enter-leave-view"></a>
92
90
93
-
## :enter and :leavealiases
91
+
## Aliases :enter and :leave
94
92
95
93
`:enter` and `:leave` are aliases for the `void => *` and `* => void` transitions.
96
94
These aliases are used by several animation functions.
@@ -103,17 +101,17 @@ transition ( ':leave', [ … ] ); // alias for * => void
103
101
</code-example>
104
102
105
103
It's harder to target an element that is entering a view because it isn't in the DOM yet.
106
-
So, use the aliases `:enter` and `:leave` to target HTML elements that are inserted or removed from a view.
104
+
Use the aliases `:enter` and `:leave` to target HTML elements that are inserted or removed from a view.
107
105
108
-
### Use of \*ngIf and \*ngFor with :enter and :leave
106
+
### Use `*ngIf` and `*ngFor` with :enter and :leave
109
107
110
108
The `:enter` transition runs when any `*ngIf` or `*ngFor` views are placed on the page, and `:leave` runs when those views are removed from the page.
111
109
112
110
<divclass="alert is-important">
113
111
114
112
**NOTE**: <br />
115
113
Entering/leaving behaviors can sometime be confusing.
116
-
As a rule of thumb consider that any element being added to the DOM by Angular passes via the `:enter` transition, but only elements being directly removed from the DOM by Angular pass via the `:leave` transition\(For example, an element's view is removed from the DOM because its parent is being removed from the DOM or the app's route has changed, then the element will not pass via the `:leave` transition\).
114
+
As a rule of thumb consider that any element being added to the DOM by Angular passes via the `:enter` transition. Only elements being directly removed from the DOM by Angular pass via the `:leave` transition. For example, an element's view is removed from the DOM because its parent is being removed from the DOM.
117
115
118
116
</div>
119
117
@@ -122,15 +120,15 @@ The HTML template contains the following code.
In the component file, the `:enter` transition sets an initial opacity of 0, and then animates it to change that opacity to 1 as the element is inserted into the view.
123
+
In the component file, the `:enter` transition sets an initial opacity of 0. It then animates it to change that opacity to 1 as the element is inserted into the view.
Note that this example doesn't need to use [`state()`](api/animations/state).
130
128
131
-
## :increment and :decrement in transitions
129
+
## Transition :increment and :decrement
132
130
133
-
The `transition()` function takes additional selector values, `:increment` and `:decrement`.
131
+
The `transition()` function takes other selector values, `:increment` and `:decrement`.
134
132
Use these to kick off a transition when a numeric value has increased or decreased in value.
135
133
136
134
<divclass="alert is-helpful">
@@ -145,16 +143,16 @@ For more information on these methods, see the [complex sequences](guide/complex
145
143
146
144
## Boolean values in transitions
147
145
148
-
If a trigger contains a boolean value as a binding value, then this value can be matched using a `transition()` expression that compares `true` and `false`, or `1` and `0`.
146
+
If a trigger contains a Boolean value as a binding value, then this value can be matched using a `transition()` expression that compares `true` and `false`, or `1` and `0`.
In the code snippet above, the HTML template binds a `<div>` element to a trigger named `openClose` with a status expression of `isOpen`, and with possible values of `true` and `false`.
153
151
This pattern is an alternative to the practice of creating two named states like `open` and `close`.
154
152
155
-
In the component code, inside 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.
153
+
Inside the `@Component` metadata under the `animations:` property, when the state evaluates to `true`, the associated HTML element's height is a wildcard style or default.
156
154
In this case, the animation uses whatever height the element already had before the animation started.
157
-
When the element is "closed", the element gets animated to a height of 0, which makes it invisible.
155
+
When the element is `closed`, the element gets animated to a height of 0, which makes it invisible.
@@ -166,11 +164,11 @@ Attach animation triggers to different elements, and the parent-child relationsh
166
164
### Parent-child animations
167
165
168
166
Each time an animation is triggered in Angular, the parent animation always gets priority and child animations are blocked.
169
-
For a child animation to run, the parent animation must query each of the elements containing child animations and then let the animations run using the [`animateChild()`](api/animations/animateChild) function.
167
+
For a child animation to run, the parent animation must query each of the elements containing child animations. It then lets the animations run using the [`animateChild()`](api/animations/animateChild) function.
170
168
171
-
#### Disabling an animation on an HTML element
169
+
#### Disable an animation on an HTML element
172
170
173
-
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.
171
+
A special animation control binding called `@.disabled` can be placed on an HTML element to turn off animations on that element, as well as any nested elements.
174
172
When true, the `@.disabled` binding prevents all animations from rendering.
175
173
176
174
The following code sample shows how to use this feature.
@@ -182,19 +180,20 @@ The following code sample shows how to use this feature.
182
180
183
181
When the `@.disabled` binding is true, the `@childAnimation` trigger doesn't kick off.
184
182
185
-
When an element within an HTML template has animations disabled using the `@.disabled` host binding, animations are disabled on all inner elements as well.
186
-
You can't selectively disable multiple animations on a single element.
183
+
When an element within an HTML template has animations turned off using the `@.disabled` host binding, animations are turned off on all inner elements as well.
184
+
You can't selectively turn off multiple animations on a single element.<!-- vale off -->
187
185
188
-
However, selective child animations can still be run on a disabled parent in one of the following ways:
186
+
A selective child animations can still be run on a disabled parent in one of the following ways:
189
187
190
188
* A parent animation can use the [`query()`](api/animations/query) function to collect inner elements located in disabled areas of the HTML template.
191
189
Those elements can still animate.
190
+
<!-- vale on -->
192
191
193
192
* A child animation can be queried by a parent and then later animated with the `animateChild()` function
194
193
195
-
#### Disabling all animations
194
+
#### Disable all animations
196
195
197
-
To disable all animations for an Angular app, place the `@.disabled` host binding on the topmost Angular component.
196
+
To turn off all animations for an Angular application, place the `@.disabled` host binding on the topmost Angular component.
A potential use for animation callbacks could be to cover for a slow API call, such as a database lookup.
221
-
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.
220
+
For example, an **InProgress** button can be set up to have its own looping animation while the backend system operation finishes.
222
221
223
-
Then, another animation can be called when the current animation finishes.
222
+
Another animation can be called when the current animation finishes.
224
223
For example, the button goes from the `inProgress` state to the `closed` state when the API call is completed.
225
224
226
-
An animation can influence an end user to *perceive* the operation as faster, even when it isn't.
227
-
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.
225
+
An animation can influence an end user to *perceive* the operation as faster, even when it is not.
228
226
229
227
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.
230
228
The following code snippet creates console log output for the original example, a button with the two states of `open` and `closed`.
@@ -235,12 +233,10 @@ The following code snippet creates console log output for the original example,
235
233
236
234
## Keyframes
237
235
238
-
The previous section features a simple two-state transition.
239
-
Let's now create an animation with multiple steps run in sequence using *keyframes*.
236
+
To create an animation with multiple steps run in sequence, use *keyframes*.
240
237
241
-
Angular's `keyframe()` function is similar to keyframes in CSS.
242
-
Keyframes allow several style changes within a single timing segment.
243
-
For example, the button, instead of fading, could change color several times over a single 2-second timespan.
238
+
Angular's `keyframe()` function allows several style changes within a single timing segment.
239
+
For example, the button, instead of fading, could change color several times over a single 2-second time span.
244
240
245
241
<divclass="lightbox">
246
242
@@ -255,7 +251,7 @@ The code for this color change might look like this.
255
251
### Offset
256
252
257
253
Keyframes include an `offset` that defines the point in the animation where each style change occurs.
258
-
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.
254
+
Offsets are relative measures from zero to one, marking the beginning and end of the animation. They should be applied to each of the keyframe steps if used at least once.
259
255
260
256
Defining offsets for keyframes is optional.
261
257
If you omit them, evenly spaced offsets are automatically assigned.
@@ -281,7 +277,7 @@ Use keyframes to create a pulse effect in your animations by defining styles at
281
277
Here's an example of using keyframes to create a pulse effect:
282
278
283
279
* The original `open` and `closed` states, with the original changes in height, color, and opacity, occurring over a timeframe of 1 second
284
-
* 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
280
+
* A keyframes sequence inserted in the middle that causes the button to appear to pulsate irregularly over the course of that same 1second timeframe
285
281
286
282
<divclass="lightbox">
287
283
@@ -310,23 +306,23 @@ For properties with a numeric value, define a unit by providing the value as a s
310
306
* Percentage:
311
307
`'100%'`
312
308
313
-
You can also provide the value as a number\(thus not providing a unit\), in such cases Angular assumes a default unit of pixels, or `px`.
309
+
You can also provide the value as a number. In such cases Angular assumes a default unit of pixels, or `px`.
314
310
Expressing 50 pixels as `50` is the same as saying `'50px'`.
315
311
316
312
<divclass="alert is-helpful">
317
313
318
314
**NOTE**: <br />
319
-
The string `"50"` would instead be considered invalid\).
315
+
The string `"50"` would instead not be considered valid\).
320
316
321
317
</div>
322
318
323
319
### Automatic property calculation with wildcards
324
320
325
-
Sometimes you don't know the value of a dimensional style property until runtime.
321
+
Sometimes, the value of a dimensional style property isn't known until runtime.
326
322
For example, elements often have widths and heights that depend on their content or the screen size.
327
323
These properties are often challenging to animate using CSS.
328
324
329
-
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.
325
+
In these cases, you can use a special wildcard `*` property value under `style()`. The value of that particular style property is computed at runtime and then plugged into the animation.
330
326
331
327
The following example has a trigger called `shrinkOut`, used when an HTML element leaves the page.
332
328
The animation takes whatever height the element has before it leaves, and animates from that height to zero.
@@ -335,7 +331,7 @@ The animation takes whatever height the element has before it leaves, and animat
335
331
336
332
### Keyframes summary
337
333
338
-
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 should occur.
334
+
The `keyframes()` function in Angular allows you to specify multiple interim styles within a single transition. An optional `offset` can be used to define the point in the animation where each style change should occur.
339
335
340
336
## More on Angular animations
341
337
@@ -346,10 +342,4 @@ You might also be interested in the following:
0 commit comments