我已经阅读了有关Angular的路由器过渡动画的这篇文章:
和:
Angular 2 "slide in animation" of a routed component
但是,这太静止了。我希望它根据选项卡的顺序左右滑动。
是否可以为此创建路由器动画?我的意思是下面的例子:
https://material.angular.io/components/tabs/examples
看看它如何根据您所使用的标签自然地左右滑动。
这必须是动态的,因为选项卡将在运行时添加。
今天的事情有点简单,因为新的动画别名存在:increment和:decrement。 Angular 5中引入了别名。
所以我修改的解决方案是:
@Component({
selector: 'app-workspace-container',
templateUrl: './workspace-container.component.html',
styleUrls: ['./workspace-container.component.scss'],
animations: [
trigger('animRoutes', [
transition(':increment', right),
transition(':decrement', left),
]),
],
})
export class ComponentContainingRouterOutlet implements OnDestroy, OnInit {
//... ngOnInit,ngOnDestroy
constructor( private route: ActivatedRoute ) { }
animationState: number;
onActivate($event) {
this.animationState = this.route.firstChild.snapshot.data['routeIdx'];
}
}
在路由器出口位置调用动画:
<div [@animRoutes]="animationState">
<router-outlet (activate)="onActivate($event)"></router-outlet>
</div>
修改路由定义为例,看看data: { routeIdx: X }
:
const routes: Routes = [
{
path: 'routeOne',
component: ComponentOne,
data: { routeIdx: 0 }
},
{
path: 'routeTwo',
component: ComponentTwo,
data: { routeIdx: 1}
},
{
path: 'routeThree',
component: ComponentThree,
data: { routeIdx: 2 }
},
{
path: 'routeFour',
component: ComponentFour,
data: { routeIdx: 3 }
},
{
path: '',
redirectTo: 'routeOne',
pathMatch: 'full'
}
]
转换与Dolan's帖子中的转换相同:
const left = [
query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }),
group([
query(':enter', [style({ transform: 'translateX(-100%)' }), animate('.3s ease-out', style({ transform: 'translateX(0%)' }))], {
optional: true,
}),
query(':leave', [style({ transform: 'translateX(0%)' }), animate('.3s ease-out', style({ transform: 'translateX(100%)' }))], {
optional: true,
}),
]),
];
const right = [
query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }),
group([
query(':enter', [style({ transform: 'translateX(100%)' }), animate('.3s ease-out', style({ transform: 'translateX(0%)' }))], {
optional: true,
}),
query(':leave', [style({ transform: 'translateX(0%)' }), animate('.3s ease-out', style({ transform: 'translateX(-100%)' }))], {
optional: true,
}),
]),
];
我已经设法通过“伪造”它所处的状态来实现这一目标。
在component.html
:
<div [@animRoutes]="pageState">
<router-outlet></router-outlet>
</div>
pageState
是component.ts
文件中的变量。
每当我点击一个我想要向右移动的标签时,我会将pageState
设置为right
,同样为left
设置,并让Angular接管其余部分。
注意:你必须创建一个right
和right1
状态作为黑客,因为Angular目前不支持right => right
状态转换!同样适用于left
当然。
我的@Component
注释如下:
@Component({
selector: 'app-workspace-container',
templateUrl: './workspace-container.component.html',
styleUrls: ['./workspace-container.component.scss'],
animations: [
trigger('animRoutes', [
transition('* => right', right),
transition('* => left', left),
transition('* => right1', right),
transition('* => left1', left),
]),
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
其中left
,left1
,right
,right1
是:
const left = [
query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }),
group([
query(':enter', [style({ transform: 'translateX(-100%)' }), animate('.3s ease-out', style({ transform: 'translateX(0%)' }))], {
optional: true,
}),
query(':leave', [style({ transform: 'translateX(0%)' }), animate('.3s ease-out', style({ transform: 'translateX(100%)' }))], {
optional: true,
}),
]),
];
const right = [
query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }),
group([
query(':enter', [style({ transform: 'translateX(100%)' }), animate('.3s ease-out', style({ transform: 'translateX(0%)' }))], {
optional: true,
}),
query(':leave', [style({ transform: 'translateX(0%)' }), animate('.3s ease-out', style({ transform: 'translateX(-100%)' }))], {
optional: true,
}),
]),
];
TL; DR:将您要进入的状态变为变量,这样您就可以动态设置您希望的状态。