angular2 animation with variable styles

前端 未结 3 2155
情书的邮戳
情书的邮戳 2021-02-14 15:07

Using Typescript & Angular 2.0.0-rc.4

How can I specify style property values from the template so that I can re-use buttons? For example, if I wanted to specify a d

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 15:26

    It is totally possible to do it since Angular v4.2. You can find details in the article "A New Wave of Animation Features in Angular". See section #8.

    Here is a code sample from it:

    import {AnimationBuilder, AnimationPlayer} from "@angular/animations";
    @Component({
      selector: 'loader',
      template: `
     
    {{ percentage }}%
    ` }) export class LoaderComponent { @ViewChild('loadingBar') public loadingBar; public player: AnimationPlayer; private _percentage = 0; constructor(private _builder: AnimationBuilder) {} get percentage() { return this._percentage; } @Input('percentage') set percentage(p: number) { this._percentage = p; if (this.player) { this.player.destroy(); } const factory = this._builder.build([ style({ width: '*' }), animate('350ms cubic-bezier(.35, 0, .25, 1)', style({ width: (p * 100) + '%' })) ]); this.player = factory.create(this.loadingBar.nativeElement, {}); this.player.play(); } }

提交回复
热议问题