问题
I'm trying to have a stagger animation in a list. Up till now I have managed to have the stagger animation on load. Apart from on load I'd like to have the stagger animation trigger when new items are added to the array.
I followed this example: https://medium.com/google-developer-experts/angular-applying-motion-principles-to-a-list-d5cdd35c899e
And came up with a quick test: https://stackblitz.com/edit/angular-stagger-animation-test2
In the app.component.ts
I define the stagger animation and in the child.component.ts
I define the element's animation.
Stagger Animation:
trigger('list', [
transition(':enter', [
query('@animate',
stagger(250, animateChild())
)
])
])
The query('@animate')
gets the child component element, with the animateChild()
triggering the animation in the child component.
Child Animation:
trigger('animate', [
transition(':enter', [
style({ opacity: 0, transform: 'translateY(-10px)' }),
animate('250ms', style({ opacity: 1, transform: 'translateY(0px)' }))
]),
transition(':leave', [
style({ opacity: 1 }),
animate('250ms', style({ opacity: 0, transform: 'translateY(10px)' }))
])
])
The main difference in my case is that I'm adding multiple objects to the array at once. I don't know if it possible to have the new items enter the page in a staggered manner.
回答1:
I managed to solve this by changing the parent animation like so:
trigger('list', [
transition('* => *', [
query(':enter',
stagger(250, animateChild())
)
])
])
So the transition will trigger on any state not just when the list is in the "enter" state. Furthermore, the query now checks for ':enter'
which means that it will get any new elements that enter the list.
To trigger the animation I am saving the components in a list and so on every change in the list the animation triggers.
Parent Template
<div [@list]="list.length">
....
</div>
Updated stackblitz
https://stackblitz.com/edit/angular-stagger-animation-test2
References:
https://www.yearofmoo.com/2017/06/new-wave-of-animation-features.html#using-query-and-stagger
https://stackoverflow.com/a/47834376/2312637
来源:https://stackoverflow.com/questions/52501138/angular-stagger-animation-for-multiple-new-elements