【发布时间】:2019-09-19 14:08:43
【问题描述】:
我一直坚持使用 CDK 为 Angular 材质自定义步进器的步进变化设置动画。 我已经按照tutorial 了解如何实现自定义步进器。
我有演示here
我的模板看起来像:
<section class="container">
<header>
<h2>Step {{selectedIndex + 1}}/{{steps.length}}</h2>
<div>
<button class="pure-button" cdkStepperPrevious>←</button>
<button
class="pure-button"
*ngFor="let step of steps; let i = index;"
[ngClass]="{'pure-button-primary': selectedIndex === i}"
(click)="onClick(i)"
>
Step {{i + 1}}
</button>
<button class="pure-button" cdkStepperNext>→</button>
</div>
</header>
<div [@stepTransition]="_getAnimationDirection(current)" *ngFor="let step of _steps; let i = index">
<div [ngTemplateOutlet]="selected.content"></div>
</div>
</section>
我的组件 TS 文件看起来像:
import { Component, OnInit } from '@angular/core';
import { CdkStepper } from '@angular/cdk/stepper';
import {
trigger,
state,
style,
animate,
transition,
} from '@angular/animations';
@Component({
selector: 'app-my-stepper',
templateUrl: './my-stepper.component.html',
styleUrls: ['./my-stepper.component.css'],
providers: [{ provide: CdkStepper, useExisting: MyStepperComponent }],
animations: [trigger('stepTransition', [
state('previous', style({transform: 'translate3d(-100%, 0, 0)', visibility: 'hidden'})),
state('current', style({transform: 'none', visibility: 'visible'})),
state('next', style({transform: 'translate3d(100%, 0, 0)', visibility: 'hidden'})),
transition('* => *', animate('500ms cubic-bezier(0.35, 0, 0.25, 1)'))
])]
})
export class MyStepperComponent extends CdkStepper implements OnInit {
current = 0;
onClick(index: number): void {
this.current = index;
this.selectedIndex = index;
}
ngOnInit() {
console.log(this._getFocusIndex())
}
}
动画只对最后一步起作用,原因是动画状态(previous, current, next)值在onClick时没有变化。
我怎样才能做到这一点?任何想法都非常感谢,谢谢。
更新 查看 stackblitz repo 以获取最新代码
【问题讨论】:
标签: angular angular-material angular-animations angular-cdk