angular2系列之路由轉(zhuǎn)場動(dòng)畫的示例代碼
Angular2的動(dòng)畫系統(tǒng)賦予了制作各種動(dòng)畫效果的能力,致力于構(gòu)建出與原生CSS動(dòng)畫性能相同的動(dòng)畫。
Angular2的動(dòng)畫主要是和@Component結(jié)合在了一起。
animations元數(shù)據(jù)屬性在定義@Component裝飾。就像template元數(shù)據(jù)屬性!這樣就可以讓動(dòng)畫邏輯與其應(yīng)用代碼緊緊集成在一起,這讓動(dòng)畫可以更容易的出發(fā)與控制。
一.在app.mudule.ts中引入:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
并在@NgModule中的imports添加:
imports: [BrowserAnimationsModule],
二.創(chuàng)建文件定義名為animations.ts用來書寫轉(zhuǎn)場動(dòng)畫
import { animate, AnimationEntryMetadata, state, style, transition, trigger } from'@angular/core';
// Component transition animations
export const slideInDownAnimation: AnimationEntryMetadata =
// 動(dòng)畫觸發(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)場動(dòng)畫的頁面操作
引入import {HostBinding } from '@angular/core';(如果引入過直接將HostBinding添加進(jìn)去就好,不要重復(fù)引入,多嘴了...)
再引入你寫好的動(dòng)畫模板:import { slideInDownAnimation } from '../animation';
在@Component中添加:animations:[slideInDownAnimation],
最后:
// 添加@HostBinding屬性添加到類中以設(shè)置這個(gè)路由組件元素的動(dòng)畫和樣式
@HostBinding('@routeAnimation') routeAnimation = true;
@HostBinding('style.display') display = 'block';
@HostBinding('style.position') position = 'absolute';
四.至此你可以去瀏覽器看看效果了,如果沒有錯(cuò)誤
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動(dòng)畫的方法
- Angular4如何自定義首屏的加載動(dòng)畫詳解
- Angular2搜索和重置按鈕過場動(dòng)畫
- 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼
- AngularJS 實(shí)現(xiàn)JavaScript 動(dòng)畫效果詳解
- AngularJS中實(shí)現(xiàn)動(dòng)畫效果的方法
- AngularJS入門之動(dòng)畫
- 給angular加上動(dòng)畫效遇到的問題總結(jié)
- 利用CSS3在Angular中實(shí)現(xiàn)動(dòng)畫
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫效果的方式總結(jié)
相關(guān)文章
AngularJS基礎(chǔ) ng-hide 指令用法及示例代碼
本文主要介紹AngularJS ng-hide 指令,這里整理了ng-hide指令的基礎(chǔ)資料,并附實(shí)例代碼,有興趣的小伙伴參考下2016-08-08
強(qiáng)大的 Angular 表單驗(yàn)證功能詳細(xì)介紹
本篇文章主要介紹了強(qiáng)大的 Angular 表單驗(yàn)證功能詳細(xì)介紹,使用 Angular 的內(nèi)置表單校驗(yàn)?zāi)軌蛲瓿山^大多數(shù)的業(yè)務(wù)場景的校驗(yàn)需求,有興趣的可以了解一下2017-05-05
Angularjs渲染的 using 指令的星級評分系統(tǒng)示例
本篇文章主要介紹了Angularjs渲染的 using 指令的星級評分系統(tǒng)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
AngularJS學(xué)習(xí)第二篇 AngularJS依賴注入
這篇文章主要為大家詳細(xì)介紹了AngularJS學(xué)習(xí)第二篇,理解什么是AngularJS依賴注入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
AngularJS基礎(chǔ) ng-submit 指令簡單示例
本文主要介紹AngularJS ng-submit 指令,這里對ng-submit 指令的基礎(chǔ)資料做了詳細(xì)介紹整理,并附有代碼示例,有需要的小伙伴可以參考下2016-08-08

