【问题标题】:Animating List in Angular 5Angular 5 中的动画列表
【发布时间】:2017-11-16 01:04:14
【问题描述】:

在 Angular 5 中添加和删除列表中的项目时创建动画。

添加项目时,它从顶部出现,慢慢缓入并放入列表中,当删除一个项目时,该项目缓慢缓出到顶部并消失。我试图解决的问题是,当一个项目被删除时,它会缓和缓慢并消失,然后列表中的剩余项目就会消失。我需要剩余的物品平稳移动而不是折断。我该怎么做?

这是我的代码:

app.component.ts

import { Component } from '@angular/core';
import { trigger, state, style, transition, animate, group } from '@angular/animations'
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  animations: [
    trigger('itemAnim', [
      transition(':enter', [
    style({ transform: 'translateY(-20%)' }),
    animate(500)
  ]),
  transition(':leave', [
    group([
          animate('0.1s ease', style({ transform: 'translateY(-20%)' })),
          animate('0.5s 0.2s ease', style({ opacity: 0 }))
        ])
      ])
    ])
  ]
})
export class AppComponent {
  title = 'Item';
  items:string[]=[];
  i=0;
  addItem(){
    this.items.push(this.title+this.i++);
  }
  removeItem(){
    this.items.shift();
  }
}

app.component.html

<button (click)="addItem()">Add</button>
<button (click)="removeItem()">Remove</button>
<br/>
<ul>
  <li [@itemAnim]="items.length" *ngFor="let item of items">
    {{item}}
  </li>
</ul>

这里是工作的 plunker Click here

【问题讨论】:

    标签: javascript angular angular-animations


    【解决方案1】:

    您可以使用&lt;li&gt; 元素的高度,因此当您将其更改为 0px 以使其消失时,它会更平滑地移动下面的元素:

    transition(':leave', [
       group([
          animate('0.5s ease', style({ transform: 'translateY(-20%)', 'height':'0px' })),
          animate('0.5s 0.2s ease', style({ opacity: 0 }))
       ])
    ])
    

    我还将过渡持续时间从 0.1s 增加到 0.5s 以使其更加明显。

    【讨论】:

    • 请再看一下 plunkr,我添加了引导样式,它破坏了您的解决方案。
    • 这是因为引导类为元素添加了一些填充(10px 15px 10px 15px)。所以一个解决方案是在你的过渡中也改变填充:transform: 'translateY(-20%)', 'height':'0px', 'padding':'0px 15px 0px 15px' }transform: 'translateY(-20%)', 'height':'0px', 'padding':'0px' }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多