2017-02-21 26 views
6

Widziałem wiele samouczków animacji dla elementów wchodzących lub wychodzących ("Nowy element" na poniższym obrazku), ale pozostałe elementy (element 1 i 2), które są wypychane oprócz zwykle po prostu teleportuj się do ich nowej lokalizacji.Angular2 * ngDo animacji wypychanych elementów

enter image description here

Czy istnieje sposób, aby animować inne elementy odchodzić ładnie, jak przedstawiono w załączonym obrazu?

Odpowiedz

11

Możesz użyć angular2 animation API, aby to osiągnąć.

Plunker Example

enter image description here

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div class="container"> 
     <div *ngFor="let item of items; let i = index" class="item" (click)="remove(i)" 
     [@anim]="item.state"> 
     {{ item.name }} 
     </div> 
    </div> 
    <div class="aside"> 
     <button (click)="push()">Push</button> 
    </div> 
    `, 
    animations: [ 
    trigger('anim', [ 
     transition('* => void', [ 
      style({ height: '*', opacity: '1', transform: 'translateX(0)', 'box-shadow': '0 1px 4px 0 rgba(0, 0, 0, 0.3)'}), 
      sequence([ 
      animate(".25s ease", style({ height: '*', opacity: '.2', transform: 'translateX(20px)', 'box-shadow': 'none' })), 
      animate(".1s ease", style({ height: '0', opacity: 0, transform: 'translateX(20px)', 'box-shadow': 'none' })) 
      ]) 
     ]), 
     transition('void => active', [ 
      style({ height: '0', opacity: '0', transform: 'translateX(20px)', 'box-shadow': 'none' }), 
      sequence([ 
      animate(".1s ease", style({ height: '*', opacity: '.2', transform: 'translateX(20px)', 'box-shadow': 'none' })), 
      animate(".35s ease", style({ height: '*', opacity: 1, transform: 'translateX(0)', 'box-shadow': '0 1px 4px 0 rgba(0, 0, 0, 0.3)' })) 
      ]) 
     ]) 
    ]) 
    ] 
}) 
export class AppComponent { 
    items: any[] = [ 
    { name: 'Element 1' }, 
    { name: 'Element 2' } 
    ]; 

    push() { 
    this.items.splice(1, 0, { name: 'New element', state: 'active' }); 
    } 

    remove(index) { 
    this.items.splice(index, 1); 
    } 
} 

Nie zapomnij importować BrowserAnimationsModule

+1

Twoja odpowiedź była prawie to, co potrzebne, i to z pewnością pomogło mi znaleźć rozwiązanie uważam za doskonały. Kluczem, którego mi brakowało, nie wiedziałem, że ** wzrost: "*" ** jest rzeczą. Dodałem margines-dolne przejście, aby wyeliminować tę skaczącą animację, gdy usuwam element i zmieniam sekwencję na grupę (być może wydawało się, że jest sekwencjonowany na gif, oczekuję, że animacje będą równoległe, ale z różnym czasem trwania). Wielkie dzięki i oto moja [Plunker] (https://plnkr.co/edit/cjCXnuOCrNH9b2I7XnLD?p=preview) – abfarid

+1

Oto dokumentacja dotycząca 'height:" * "' https://angular.io/docs/ts /latest/guide/animations.html#!#automatic-property-calculation – yurzui