1

在 Angular2 应用程序中,我正在尝试制作动画

这是组件的html:

   <div  [@playBox]="state" id="toPlay" class="playBox rounded-box">

   </div>

这是动画:

animations:[
    trigger('playBox',[
            state('closed',style({
            width: '5%',
            backgroundColor:'red',
            transform:'translateX(0)'
          })),
          state('wided',style({
                width: '100%',
                border: '3px solid red',
                backgroundColor:'transparent'
          })),transition('closed => wided',animate(4000))])
  ]

我将在页面加载时触发此动画:

export class AppComponent implements OnInit{
  state="closed";
 public ngOnInit(): any
 {

        if(this.state=="closed"){

            this.state="wided";
         }
}
4

2 回答 2

1

不确定这是最合适和最优雅的解决方案,但state在下一个刻度中更改将按预期触发动画:

public ngOnInit(): any {
  if (this.state == "closed") {
    setTimeout(() => this.state = "wided")
  }
}

否则,this.state = "wided"会重新定义初始closed状态,并且组件会被初始化wided为没有动画的初始状态。

于 2016-12-26T12:29:05.397 回答
1

你可以使用ngAfterViewInit生命周期钩子

ngAfterViewInit() {
  if (this.state == "closed") {
    this.state = "wided";
  }
}
于 2019-12-04T14:08:50.623 回答