javascript - Angular2 *ng用于推开元素的动画

标签 javascript angular animation ngfor

我看过很多关于进入或离开元素(下图中的“新元素”)的动画教程,但是被推开的其余元素(元素 1 和 2)通常只是传送到它们的新位置.

enter image description here

有没有一种方法可以让其他元素很好地移动,就像附图中所描绘的那样?

最佳答案

您可以使用 angular2 animation API实现它。

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);
  }
}

不要忘记导入 BrowserAnimationsModule

关于javascript - Angular2 *ng用于推开元素的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42376006/

相关文章:

javascript - 单引号和双引号追加

javascript - 动画功能之间的延迟/排队

javascript - 为什么 iScroll 没有初始化?

javascript - Node JSON 输出格式错误,无法设置 HTTP header : Content-Length to fix

javascript - 如何使按钮在 JavaScript 的新选项卡中打开链接?

Javascript: Uncaught ReferenceError :tb 未定义

api - angular2 http.get() 不向服务器发送请求

angular - 如何在 ngrx 效果中使用 LatestFrom 进行单元测试

ios - 自定义 UIViewController 动画问题

android - 滑动抽屉窗帘动画