Angular2的動(dòng)畫(huà)系統(tǒng)賦予了制作各種動(dòng)畫(huà)效果的能力,致力于構(gòu)建出與原生CSS動(dòng)畫(huà)性能相同的動(dòng)畫(huà)。
Angular2的動(dòng)畫(huà)主要是和@Component結(jié)合在了一起。
animations元數(shù)據(jù)屬性在定義@Component裝飾。就像template元數(shù)據(jù)屬性!這樣就可以讓動(dòng)畫(huà)邏輯與其應(yīng)用代碼緊緊集成在一起,這讓動(dòng)畫(huà)可以更容易的出發(fā)與控制。
一.在app.mudule.ts中引入:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';并在@NgModule中的imports添加:
imports: [BrowserAnimationsModule],
二.創(chuàng)建文件定義名為animations.ts用來(lái)書(shū)寫(xiě)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';// Component transition animationsexport const slideInDownAnimation: AnimationEntryMetadata =// 動(dòng)畫(huà)觸發(fā)器名稱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%)' })) ])]);三.在需要添加轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的頁(yè)面操作
引入import {HostBinding } from '@angular/core';(如果引入過(guò)直接將HostBinding添加進(jìn)去就好,不要重復(fù)引入,多嘴了...)
再引入你寫(xiě)好的動(dòng)畫(huà)模板:import { slideInDownAnimation } from '../animation';
在@Component中添加:animations:[slideInDownAnimation],
最后:
// 添加@HostBinding屬性添加到類中以設(shè)置這個(gè)路由組件元素的動(dòng)畫(huà)和樣式 @HostBinding('@routeAnimation') routeAnimation = true; @HostBinding('style.display') display = 'block'; @HostBinding('style.position') position = 'absolute';四.至此你可以去瀏覽器看看效果了,如果沒(méi)有錯(cuò)誤
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選