angular2系列之動畫-路由轉場動畫

一.在app.mudule.ts中引入:瀏覽器

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

並在@NgModule中的imports添加:app

imports: [BrowserAnimationsModule],

二.建立文件定義名爲animations.ts用來書寫轉場動畫ide

import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';
// Component transition animations
export const slideInDownAnimation: AnimationEntryMetadata =
// 動畫觸發器名稱
trigger('routeAnimation', [
    state('*',
        style({
            opacity: 1,
            transform: 'translateX(0)'
        })
    ),
    transition(':enter', [
        style({
            opacity: 0,
            transform: 'translateX(-100%)'
        }),
        animate('0.2s ease-in')
    ]),
    transition(':leave', [
        animate('0.5s ease-out', style({
            opacity: 0,
            transform: 'translateY(100%)'
        }))
    ])
]);

三.在須要添加轉場動畫的頁面操做動畫

引入import {HostBinding } from '@angular/core';(若是引入過直接將HostBinding添加進去就好,不要重複引入,多嘴了...)

再引入你寫好的動畫模板:import { slideInDownAnimation } from '../animation';code

在@Component中添加:animations:[slideInDownAnimation],
最後:
    // 添加@HostBinding屬性添加到類中以設置這個路由組件元素的動畫和樣式
    @HostBinding('@routeAnimation') routeAnimation = true;
    @HostBinding('style.display') display = 'block';
    @HostBinding('style.position') position = 'absolute';

四.至此你能夠去瀏覽器看看效果了,若是沒有錯誤orm

相關文章
相關標籤/搜索