{ "id": "api/animations/transition", "title": "transition", "contents": "\n\n
\n
\n
\n \n API > @angular/animations\n
\n \n
\n \n
\n

transitionlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

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

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

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

\n
    \n
  • \n

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

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

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

\n\n
\n \n options\n AnimationOptions\n

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

\n

Optional. Default is null.

\n\n
\n\n \n
Returns
\n

AnimationTransitionMetadata: An object that encapsulates the transition data.

\n \n \n\n\n \n\n \n
\n\n\n \n\n \n\n\n\n \n
\n

Usage noteslink

\n

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

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

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

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

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

\n

Syntax exampleslink

\n

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

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

Special values for state-change expressionslink

\n
    \n
  • Catch-all state change for when an element is inserted into the page and the\ndestination state is unknown:
  • \n
\n\ntransition(\"void => *\", [\n style({ opacity: 0 }),\n animate(500)\n ])\n\n
    \n
  • \n

    Capture a state change between any states:

    \n

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

    \n
  • \n
  • \n

    Entry and exit transitions:

    \n
  • \n
\n\ntransition(\":enter\", [\n style({ opacity: 0 }),\n animate(500, style({ opacity: 1 }))\n ]),\ntransition(\":leave\", [\n animate(500, style({ opacity: 0 }))\n ])\n\n
    \n
  • Use :increment and :decrement to initiate transitions:
  • \n
\n\ntransition(\":increment\", group([\n query(':enter', [\n style({ left: '100%' }),\n animate('0.5s ease-out', style('*'))\n ]),\n query(':leave', [\n animate('0.5s ease-out', style({ left: '-100%' }))\n ])\n]))\n\ntransition(\":decrement\", group([\n query(':enter', [\n style({ left: '100%' }),\n animate('0.5s ease-out', style('*'))\n ]),\n query(':leave', [\n animate('0.5s ease-out', style({ left: '-100%' }))\n ])\n]))\n\n

State-change functionslink

\n

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

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

Animating to the final statelink

\n

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

\n

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

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

Boolean value matchinglink

\n

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

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