#Animating one style in a different duration

23 messages · Page 1 of 1 (latest)

odd wind
#

I have the followings in the animations array of a component:

trigger('...', [
  state('off', style({ color: 'red', opacity: 0 })),
  state('on', style({ color: 'green', opacity: 1 })),
  transition('off <=> on', animate(500)),
])

Right now, both rules are animated at 500ms, however I'd like to animate opacity at a different time (i.e. 250ms). How do I do that?

merry musk
#

Try this

trigger('...', [
  transition('off <=> on', animate(500), keyframes([
     style({ color: 'red', opacity: 0 }),
     style({ opacity: 1, offset: 0.5 }),
     style({ color: 'green' })
  ])),
])
odd wind
merry musk
#

Did you try that?

odd wind
merry musk
#
trigger('...', [
  transition('off <=> on', [
    animate('500ms', keyframes([
      style({ color: 'red', opacity: 0 }),
      style({ opacity: 1, offset: 0.5 }),
      style({ color: 'green' })
    ]))
  ]),
])
odd wind
merry musk
#
trigger('...', [
  transition('off <=> on', [
    animate('500ms', keyframes([
      style({ color: 'red', opacity: 0, offset: 0 }),
      style({ opacity: 1, offset: 0.5 }),
      style({ color: 'green', offset: 1 })
    ]))
  ]),
])
odd wind
merry musk
#

Animations in angular qre quite tricky, and at the moment I got no way to test, I can only suggest code out of my mind.
What exactly does not work as you'd like?

odd wind
merry musk
#

Do you mean that at half transition colour should have completed changing, while opacity should take 500ms?

odd wind
#

In my actual app, there are other styles too, I just kept it short here.

merry musk
#
trigger('...', [
  transition('off <=> on', [
    animate('1s', keyframes([
      style({ color: 'red', opacity: 0, offset: 0 }),
      style({ color: 'green', offset: 0.5 }),
      style({ opacity: 1, offset: 1 })
    ]))
  ]),
])
#

I suspect the effect you want cannot be symmetric, so you need to split your bidirectional transition into two unidirectional ones.

odd wind
merry musk
#
trigger('...', [
  transition('off => on', [
    animate('1s', keyframes([
      style({ color: 'red', opacity: 0, offset: 0 }),
      style({ color: 'green', offset: 0.5 }),
      style({ opacity: 1, offset: 1 })
    ]))
  ]),
  transition('on => off', [
    animate('1s', keyframes([
      style({ color: 'green', opacity: 1, offset: 0 }),
      style({ color: 'red', offset: 0.5 }),
      style({ opacity: 0, offset: 1 })
    ]))
  ]),
])
odd wind
#

So keyframes was the magic wand here... 👌

merry musk
#

Animating style directly is like having just offset: 0 and offset: 1.
If you need to differentiate properties transition, or simply don't want it be linear, you need to specify intermediate frames.
(For the latter you can use easing too, but it was not your case).

odd wind
merry musk
#

Unfortunately is a really confusing topic.
Can't suggest any specific.