18

Here is a random animation I've made

    import { trigger, animate, transition, style, keyframes} from '@angular/animations';

    export const customAnimation=
        trigger('customAnimation', [
            transition(':leave', [
                style({position: 'relative'}),
                animate("250ms ease-in", keyframes([
                    style({top: 0}),
                    style({top: -300}),
                ]))
            ])
        ])

I am then importing it into my components. (animations: [customAnimation] ) However, I'd like to use the new arguments features : (http://sudheerjonna.com/blog/2017/06/30/angular-4-2-release-an-improved-version-for-animation-package/).

By instance, the -300 would become a parameter, and I would call it on me template elements by doing :

<div [@customAnimation]="{pixels: -300}">

Since I don't want to use animation() and useAnimation() as I want to use on specific dom element (not using a query() either) I simply didn't manage to do it.

EDIT:

Got it working with :

export const leavingTowardsTop=
    trigger('leavingTowardsTop', [
        transition(':leave', [
            style({position: 'relative'}),
            animate("250ms ease-in", keyframes([
                style({top: 0}),
                style({top: "{{pixels}}"})
            ]))
        ], {params : { pixels: "-30px" }})
    ])

only issue, I can't specify another value than the default one (-30). I tried :

[@leavingTowardsTop]="{params : { pixels: '-300px' }}"

and

[@leavingTowardsTop]="{ pixels: '-300px' }"

I also tried not specifying the ' or px but it still takes 30px

2 Answers 2

22

You need to parameterize the top style like so:

export const customAnimation=
    trigger('customAnimation', [
        transition(':leave', [
            animate("500ms ease-in", keyframes([
                style({'margin-top': "-{{pixels}}px", 'height': "{{pixels}}px", 'margin-bottom': "0px"}),
            ]))
        ], {params : { pixels: "30" }})
    ]);

Then call it in the view like so:

[@customAnimation]="{value: ':leave', params: { pixels: 100}}"

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

can I add a default value ?
12

Here is the solution I needed

[@leavingTowardsTop]="{value: ':leave', params : {topPixels: '-300px'}}"

5 Comments

is this syntax documented anywhere? what does this look live if there are several transitions to provide values for?
can you explain what 'value' means? are you selecting a state? overriding parameters for a particular state? and what if you had more than just one.
One thing to note, store the object in a class variable, it doesn't seem to update if you just change a property, you have to reassign a new object reference
@Simon_Weaver: value is the “trigger value” of the simpler parameterless form: [@someAnim]="foo" => [@someAnim]="{value: 'foo', params: {...}}"
Like @Sam I am also looking for an documentation. An interface would be great.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.