Help Angular by taking a 1 minute survey!Go to surveyHome

transition

Declares an animation transition as a sequence of animation steps to run when a given condition is satisfied. The condition is a Boolean expression or function that compares the previous and current animation states, and returns true if this transition should occur. When the state criteria of a defined transition are met, the associated animation is triggered.

transition(stateChangeExpr: string | ((fromState: string, toState: string, element?: any, params?: { [key: string]: any; }) => boolean), steps: AnimationMetadata | AnimationMetadata[], options: AnimationOptions = null): AnimationTransitionMetadata
      
      transition(stateChangeExpr: string | ((fromState: string, toState: string, element?: any, params?: { [key: string]: any; }) => boolean), steps: AnimationMetadata | AnimationMetadata[], options: AnimationOptions = null): AnimationTransitionMetadata
    
Parameters
stateChangeExpr string | ((fromState: string, toState: string, element?: any, params?: { [key: string]: any; }) => boolean)

A Boolean expression or function that compares the previous and current animation states, and returns true if this transition should occur. Note that "true" and "false" match 1 and 0, respectively. An expression is evaluated each time a state change occurs in the animation trigger element. The animation steps run when the expression evaluates to true.

  • A state-change string takes the form "state1 => state2", where each side is a defined animation state, or an asterix (*) to refer to a dynamic start or end state.

    • The expression string can contain multiple comma-separated statements; for example "state1 => state2, state3 => state4".
    • Special values :enter and :leave initiate a transition on the entry and exit states, equivalent to "void => " and " => void".
    • Special values :increment and :decrement initiate a transition when a numeric value has increased or decreased in value.
  • A function is executed each time a state change occurs in the animation trigger element. The animation steps run when the function returns true.
steps AnimationMetadata | AnimationMetadata[]

One or more animation objects, as returned by the animate() or sequence() function, that form a transformation from one state to another. A sequence is used by default when you pass an array.

options AnimationOptions

An options object that can contain a delay value for the start of the animation, and additional developer-defined parameters. Provided values for additional parameters are used as defaults, and override values can be passed to the caller on invocation.

Optional. Default is null.

Returns

AnimationTransitionMetadata: An object that encapsulates the transition data.

Usage notes

The template associated with a component binds an animation trigger to an element.

<!-- somewhere inside of my-component-tpl.html --> <div [@myAnimationTrigger]="myStatusExp">...</div>
      
      <!-- somewhere inside of my-component-tpl.html -->
<div [@myAnimationTrigger]="myStatusExp">...</div>
    

All transitions are defined within an animation trigger, along with named states that the transitions change to and from.

trigger("myAnimationTrigger", [ // define states state("on", style({ background: "green" })), state("off", style({ background: "grey" })), ...]
      
      trigger("myAnimationTrigger", [
 // define states
 state("on", style({ background: "green" })),
 state("off", style({ background: "grey" })),
 ...]
    

Note that when you call the sequence() function within a group() or a transition() call, execution does not continue to the next instruction until each of the inner animation steps have completed.

Syntax examples

The following examples define transitions between the two defined states (and default states), using various options:

// Transition occurs when the state value // bound to "myAnimationTrigger" changes from "on" to "off" transition("on => off", animate(500)) // Run the same animation for both directions transition("on <=> off", animate(500)) // Define multiple state-change pairs separated by commas transition("on => off, off => void", animate(500))
      
      // Transition occurs when the state value
// bound to "myAnimationTrigger" changes from "on" to "off"
transition("on => off", animate(500))
// Run the same animation for both directions
transition("on <=> off", animate(500))
// Define multiple state-change pairs separated by commas
transition("on => off, off => void", animate(500))
    

Special values for state-change expressions

  • Catch-all state change for when an element is inserted into the page and the destination state is unknown:
transition("void => *", [ style({ opacity: 0 }), animate(500) ])
      
      transition("void => *", [
 style({ opacity: 0 }),
 animate(500)
 ])
    
  • Capture a state change between any states:

    transition("* => *", animate("1s 0s"))

  • Entry and exit transitions:

transition(":enter", [ style({ opacity: 0 }), animate(500, style({ opacity: 1 })) ]), transition(":leave", [ animate(500, style({ opacity: 0 })) ])
      
      transition(":enter", [
  style({ opacity: 0 }),
  animate(500, style({ opacity: 1 }))
  ]),
transition(":leave", [
  animate(500, style({ opacity: 0 }))
  ])
    
  • Use :increment and :decrement to initiate transitions:
transition(":increment", group([ query(':enter', [ style({ left: '100%' }), animate('0.5s ease-out', style('*')) ]), query(':leave', [ animate('0.5s ease-out', style({ left: '-100%' })) ]) ])) transition(":decrement", group([ query(':enter', [ style({ left: '100%' }), animate('0.5s ease-out', style('*')) ]), query(':leave', [ animate('0.5s ease-out', style({ left: '-100%' })) ]) ]))
      
      
  1. transition(":increment", group([
  2. query(':enter', [
  3. style({ left: '100%' }),
  4. animate('0.5s ease-out', style('*'))
  5. ]),
  6. query(':leave', [
  7. animate('0.5s ease-out', style({ left: '-100%' }))
  8. ])
  9. ]))
  10.  
  11. transition(":decrement", group([
  12. query(':enter', [
  13. style({ left: '100%' }),
  14. animate('0.5s ease-out', style('*'))
  15. ]),
  16. query(':leave', [
  17. animate('0.5s ease-out', style({ left: '-100%' }))
  18. ])
  19. ]))

State-change functions

Here is an example of a fromState specified as a state-change function that invokes an animation when true:

transition((fromState, toState) => { return fromState == "off" && toState == "on"; }, animate("1s 0s"))
      
      transition((fromState, toState) =>
 {
  return fromState == "off" && toState == "on";
 },
 animate("1s 0s"))
    

Animating to the final state

If the final step in a transition is a call to animate() that uses a timing value with no style data, that step is automatically considered the final animation arc, for the element to reach the final state. Angular automatically adds or removes CSS styles to ensure that the element is in the correct final state.

The following example defines a transition that starts by hiding the element, then makes sure that it animates properly to whatever state is currently active for trigger:

transition("void => *", [ style({ opacity: 0 }), animate(500) ])
      
      transition("void => *", [
  style({ opacity: 0 }),
  animate(500)
 ])
    

Boolean value matching

If a trigger binding value is a Boolean, it can be matched using a transition expression that compares true and false or 1 and 0. For example:

// in the template <div [@openClose]="open ? true : false">...</div> // in the component metadata trigger('openClose', [ state('true', style({ height: '*' })), state('false', style({ height: '0px' })), transition('false <=> true', animate(500)) ])
      
      // in the template
<div [@openClose]="open ? true : false">...</div>
// in the component metadata
trigger('openClose', [
  state('true', style({ height: '*' })),
  state('false', style({ height: '0px' })),
  transition('false <=> true', animate(500))
])