angular2系列之路由轉場動畫的示例代碼
Angular2的動畫系統(tǒng)賦予了制作各種動畫效果的能力,致力于構建出與原生CSS動畫性能相同的動畫。
Angular2的動畫主要是和@Component結合在了一起。
animations元數據屬性在定義@Component裝飾。就像template元數據屬性!這樣就可以讓動畫邏輯與其應用代碼緊緊集成在一起,這讓動畫可以更容易的出發(fā)與控制。
一.在app.mudule.ts中引入:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
并在@NgModule中的imports添加:
imports: [BrowserAnimationsModule],
二.創(chuàng)建文件定義名為animations.ts用來書寫轉場動畫
import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';
// Component transition animations
export const slideInDownAnimation: AnimationEntryMetadata =
// 動畫觸發(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%)'
}))
])
]);
三.在需要添加轉場動畫的頁面操作
引入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';
四.至此你可以去瀏覽器看看效果了,如果沒有錯誤
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Angular實踐之將Input與Lifecycle轉換成流示例詳解
這篇文章主要為大家介紹了Angular實踐之將Input與Lifecycle轉換成流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
AngularJS 在同一個界面啟動多個ng-app應用模塊詳解
這篇文章主要介紹了AngularJS 在同一個界面啟動多個ng-app應用模塊詳解的相關資料,需要的朋友可以參考下2016-12-12
關于angular js_$watch監(jiān)控屬性和對象詳解
下面小編就為大家?guī)硪黄P于angular js_$watch監(jiān)控屬性和對象詳解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
AngularJs入門教程之環(huán)境搭建+創(chuàng)建應用示例
這篇文章主要介紹了AngularJs入門教程之環(huán)境搭建+創(chuàng)建應用的方法,較為詳細的分析了AngularJS的功能、下載及應用創(chuàng)建方法,需要的朋友可以參考下2016-11-11

