Angular 4 анимация для постепенного появления и исчезновения вида
Я просто хочу, чтобы при смене маршрута вид исчезал и исчезал. Кажется, я правильно настроил компонент, но мне нужен правильный синтаксис анимации.
Это моя текущая попытка анимации. Я импортирую эту анимацию в свой компонент:
import {trigger, state, animate, style, transition} from '@angular/animations';
export function routerTransition() {
return fadeInAndOut();
}
function fadeInAndOut() {
return trigger('routerTransition', [
transition(':enter', [
style({opacity: 0}),
animate(3000, style({opacity: 1}))
]),
transition(':leave', [
animate(3000, style({opacity: 0}))
])
]);
}
Это один из моих компонентов, импортирующих переход:
import { Component } from "@angular/core";
import { routerTransition } from "../../animations/fade.animation";
@Component({
selector: "about-users",
templateUrl: "./about.component.html",
animations: [routerTransition()],
host: { '[@routerTransition]': '' }
})
export class AboutComponent {
constructor() {
}
}
2 ответа
Это работает для меня для анимации маршрутизации:
Машинопись:
....
animations: [
trigger('routerTransition', [
transition('* <=> *', [
query(':enter, :leave', style({ position: 'fixed', opacity: 1 })),
group([
query(':enter', [
style({ opacity:0 }),
animate('1000ms ease-in-out', style({ opacity:1 }))
]),
query(':leave', [
style({ opacity:1 }),
animate('1000ms ease-in-out', style({ opacity:0 }))]),
])
])
])
]
HTML:
<nav>
<a routerLink="/page1" routerLinkActive="active">Page1</a>
<a routerLink="/page2" routerLinkActive="active">Page2</a>
</nav>
<br><br>
<main [@routerTransition]="page.activatedRouteData.state">
<router-outlet #page="outlet"></router-outlet>
</main>
Я обнаружил, что вам нужно установить блокировку отображения для работы анимации, вот так:
@HostBinding('style.display') display = 'block';
Кроме того, с последней версией Angular вам необходимо использовать HostBinding
вместо host
,
Пожалуйста, смотрите мой полный файл:
import { Component, OnInit, HostBinding } from '@angular/core';
import { slideInDownAnimation, fadeInAnimation } from './../checkout-animations';
@Component({
selector: 'app-checkout-two',
templateUrl: './checkout-two.component.html',
styleUrls: ['./checkout-two.component.scss', './../checkout.component.scss'],
animations: [slideInDownAnimation]
})
export class CheckoutTwoComponent implements OnInit {
@HostBinding('@routeAnimation') routeAnimation = true;
@HostBinding('style.display') display = 'block';
constructor() { }
ngOnInit() {
}
}