angular2系列之路由轉場動畫的範例程式碼,angular2範例程式碼

來源:互聯網
上載者:User

angular2系列之路由轉場動畫的範例程式碼,angular2範例程式碼

Angular2的動畫系統賦予了製作各種動畫效果的能力,致力於構建出與原生CSS動畫效能相同的動畫。

Angular2的動畫主要是和@Component結合在了一起。

animations中繼資料屬性在定義@Component裝飾。就像template中繼資料屬性!這樣就可以讓動畫邏輯與其應用代碼緊緊整合在一起,這讓動畫可以更容易的出發與控制。

一.在app.mudule.ts中引入:

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

並在@NgModule中的imports添加:

imports: [BrowserAnimationsModule],

二.建立檔案定義名為animations.ts用來書寫轉場動畫

import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';// Component transition animationsexport 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';

在@Component中添加:animations:[slideInDownAnimation],

最後:

  // 添加@HostBinding屬性添加到類中以設定這個路由群組件元素的動畫和樣式  @HostBinding('@routeAnimation') routeAnimation = true;  @HostBinding('style.display') display = 'block';  @HostBinding('style.position') position = 'absolute';

四.至此你可以去瀏覽器看看效果了,如果沒有錯誤

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.