Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 36bf09d

Browse files
committed
docs(ngAnimate): describe ng-[event]-prepare class and usage
1 parent 5f8bb47 commit 36bf09d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/content/guide/animations.ngdoc

+31
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,37 @@ myModule.directive('my-directive', ['$animate', function($animate) {
274274
}]);
275275
```
276276

277+
## Preventing flicker before an animation starts
278+
279+
When nesting elements with structural animations such as `ngIf` into elements that have class-based
280+
animations such as `ngClass`, it sometimes happens that before the actual animation starts, there is a brief flicker or flash of content
281+
where the animated element is briefly visible.
282+
283+
To prevent this, you can apply styles to the `ng-[event]-prepare` class, which is added as soon as an animation is initialized,
284+
but removed before the actual animation starts (after waiting for a $digest). This class is only added for *structural*
285+
animations (the built-in animations `enter`, `move`, `leave`, but also custom animations).
286+
287+
Here's an example where you might see flickering:
288+
289+
```html
290+
<div ng-class="{red: myProp}">
291+
<div ng-class="{blue: myProp}">
292+
<div class="message" ng-if="myProp"></div>
293+
</div>
294+
</div>
295+
```
296+
297+
It is possible that during the `enter` event, the `.message` div will be briefly visible before it starts animating.
298+
In that case, you can add styles to the CSS that make sure the element stays hidden before the animation starts:
299+
300+
```css
301+
.message.ng-enter-prepare {
302+
opacity: 0;
303+
}
304+
305+
/* Other animation styles ... */
306+
```
307+
277308
## More about animations
278309

279310
For a full breakdown of each method available on `$animate`, see the {@link ng.$animate API documentation}.

src/ngAnimate/module.js

+30
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,36 @@
254254
* the CSS class once an animation has completed.)
255255
*
256256
*
257+
* ### The `ng-[event]-prepare` class
258+
*
259+
* This is a special class that can be used to prevent unwanted flickering / flash of content before
260+
* the actual animation starts.
261+
* The class is added as soon as an animation is initialized, but removed before the actual animation
262+
* starts (after waiting for a $digest).
263+
* It is also only added for *structural* animations (the built-in animations `enter`, `move`, `leave`,
264+
* but also custom animations).
265+
*
266+
* In practice, flickering can appear when nesting elements with structural animations such as `ngIf`
267+
* into elements that have class-based animations such as `ngClass`.
268+
*
269+
* ```html
270+
* <div ng-class="{red: myProp}">
271+
* <div ng-class="{blue: myProp}">
272+
* <div class="message" ng-if="myProp"></div>
273+
* </div>
274+
* </div>
275+
* ```
276+
*
277+
* It is possible that during the `enter` animation, the `.message` div will be briefly visible before it starts animating.
278+
* In that case, you can add styles to the CSS that make sure the element stays hidden before the animation starts:
279+
*
280+
* ```css
281+
* .message.ng-enter-prepare {
282+
* opacity: 0;
283+
* }
284+
*
285+
* ```
286+
*
257287
* ## JavaScript-based Animations
258288
*
259289
* ngAnimate also allows for animations to be consumed by JavaScript code. The approach is similar to CSS-based animations (where there is a shared

0 commit comments

Comments
 (0)