Angular4.0動(dòng)畫(huà)操作實(shí)例詳解
本文實(shí)例講述了Angular4.0動(dòng)畫(huà)操作。分享給大家供大家參考,具體如下:
粗略的記錄一下angular4的動(dòng)畫(huà)
先看一下angular中文網(wǎng)關(guān)于這個(gè)給的例子。
有兩個(gè)組件home,about。 路徑配置什么的這里就不細(xì)說(shuō)了,之前的博文有說(shuō)過(guò),我就貼一下代碼,很好理解的,
需要import的東西我先說(shuō)一下,我只貼了使用動(dòng)畫(huà)要用的東西,其他的我省略了,
app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; @NgModule({ ... imports: [ BrowserModule, BrowserAnimationsModule, AppRouting ], ... })
在這個(gè)簡(jiǎn)單的例子里我要對(duì)app.component.html里的內(nèi)容進(jìn)行animate,所以我的
app.component.ts
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], animations: [] // 這里代碼我省略了,先說(shuō)一下結(jié)構(gòu),后面說(shuō)具體實(shí)現(xiàn)。 })
以上就是需要寫(xiě)動(dòng)畫(huà)實(shí)現(xiàn)的基本結(jié)構(gòu),下面貼實(shí)現(xiàn)這個(gè)例子的代碼。為了方便閱讀,我把代碼解釋就貼在代碼旁邊
例一:
這是路由配置:
import {RouterModule, Routes} from '@angular/router'; import {HomeComponent} from "./home/home.component"; import {AboutComponent} from "./about/about.component"; const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent, data: { state: 'home' } }, { path: 'about', component: AboutComponent, data: { state: 'about' } } ]; export const AppRouting = RouterModule.forRoot(routes, { useHash: true });
app.component.html
<nav> <a routerLink="home" routerLinkActive="active">Home</a> <a routerLink="about" routerLinkActive="active">About</a> </nav> <main [@routerTransition] = "gg(o)"> <router-outlet #o="outlet"></router-outlet> </main> <div [@queryAnimation]="goAnimate()"> <div class="content"> Blah blah blah </div> <h1>Title</h1> </div> <!-- [@routerTransition]="gg(o)" ,api:transition declares the sequence of animation steps that will be run when the provided stateChangeExpr value is satisfied. stateChangeExpr即等號(hào)左邊即動(dòng)畫(huà)名稱,注意中括號(hào)和@符不能省略,等號(hào)右邊是一個(gè)函數(shù),也可以是變量,滿足條件便可以讓動(dòng)畫(huà)進(jìn)行,一個(gè)動(dòng)畫(huà)可以多次使用 -->
app.component.ts
import { Component } from '@angular/core'; import {routerTransition} from './router.animation'; import {animate, group, query, stagger, style, transition, trigger} from "@angular/animations"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], animations: [ trigger('routerTransition', [ // 第一個(gè)參數(shù)是動(dòng)畫(huà)名稱 stateChangeExpr transition('* <=> *', [ // 指定什么時(shí)候執(zhí)行動(dòng)畫(huà),狀態(tài)的來(lái)源可以是簡(jiǎn)單的對(duì)象屬性,也可以是由方法計(jì)算出來(lái)的值。重點(diǎn)是,我們得能從組件模板中讀取它。官網(wǎng)上有提供一些通配符,[傳送門(mén)](https://angular.cn/api/animations/transition) query(':enter, :leave', style({ position: 'fixed', width: '100%' }), { optional: true }), query('.block', style({ opacity: 0 }), { optional: true }), group([ // block executes in parallel query(':enter', [style({ transform: 'translateX(100%)' }), animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))], { optional: true }), query(':leave', [style({ transform: 'translateX(0%)' }), animate('0.5s ease-in-out', style({ transform: 'translateX(-100%)' }))], { optional: true }) ]), query(':enter .block', stagger(400, [style({ transform: 'translateY(100px)' }), animate('1s ease-in-out', style({ transform: 'translateY(0px)', opacity: 1 })), ]), { optional: true }), ]) ]), ] }) export class AppComponent { public exp = ''; gg(outlet) { // 傳遞進(jìn)入的組件的信息 console.log(outlet.activatedRouteData.state); return outlet.activatedRouteData.state; } }
效果動(dòng)圖在最后。
比對(duì)著官網(wǎng)給的API,總結(jié)一下動(dòng)畫(huà)部分~
我是按自己的理解說(shuō)的,有不對(duì)的地方還請(qǐng)多多指教,共勉!O(∩_∩)O~
- stateChangeExpr
即動(dòng)畫(huà)名稱,它的屬性值可以是字符串也可以是函數(shù),若是函數(shù),則每次狀態(tài)發(fā)生改變都會(huì)重新執(zhí)行,若函數(shù)返回true,則對(duì)應(yīng)的動(dòng)畫(huà)就會(huì)執(zhí)行。
- transition
它里面的動(dòng)畫(huà)只在滿足條件時(shí)執(zhí)行,過(guò)了這個(gè)點(diǎn)它就變回原始樣式了,
- state
可以保持動(dòng)畫(huà)樣式
- :enter 、 :leave
即對(duì)應(yīng)void => * 、 * => void 狀態(tài)
例子二
app.component.html
<div [@queryAnimation]="goAnimate()"> <h1>Title</h1> <div class="content"> Blah blah blah </div> </div>
app.component.ts
import { Component } from '@angular/core'; import {animate, group, query, stagger, state, style, transition, trigger} from "@angular/animations"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], animations: [ trigger('queryAnimation', [ transition('* => *', [ query('h1', style({ opacity: 0 , color: 'red'})), query('.content', style({ opacity: 0, color: 'green', width: '100px', height: '100px', border: '1px solid red' })), query('h1', animate(1000, style({ opacity: 1, color: ' blue' }))), query('.content', animate(1000, style({ opacity: 1, width: '50px', height: '100px', border: '10px solid green'})), {optional: true}), ]), transition(':leave', [ style({color: 'pink'}), animate(2000) ]) ]), ] }) export class AppComponent { public gg: string; constructor() { } goAnimate() { return true; } }
這個(gè)gif有點(diǎn)卡,但是可以大概看出路由切換時(shí)是有動(dòng)畫(huà)的。這是上面兩個(gè)例子的效果圖
state只能放在trigger里,不能擱在transition里
目前就這么點(diǎn),才看了一點(diǎn),以后慢慢補(bǔ)充~~~
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門(mén)與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
- Angular4如何自定義首屏的加載動(dòng)畫(huà)詳解
- Angular2搜索和重置按鈕過(guò)場(chǎng)動(dòng)畫(huà)
- 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫(huà)實(shí)例代碼
- 給angular加上動(dòng)畫(huà)效遇到的問(wèn)題總結(jié)
- 利用CSS3在Angular中實(shí)現(xiàn)動(dòng)畫(huà)
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫(huà)效果的方式總結(jié)
- 使用ngView配合AngularJS應(yīng)用實(shí)現(xiàn)動(dòng)畫(huà)效果的方法
- 在AngularJS應(yīng)用中實(shí)現(xiàn)一些動(dòng)畫(huà)效果的代碼
- 詳解Angular路由動(dòng)畫(huà)及高階動(dòng)畫(huà)函數(shù)
相關(guān)文章
Angular.JS去掉訪問(wèn)路徑URL中的#號(hào)詳解
最近天天都在用AngularJS,各類文檔也都看過(guò)好幾遍,但總是些編程上的事找不到優(yōu)雅的解決辦法。今天終于把AngularJS的項(xiàng)目訪問(wèn)路徑URL里的#號(hào)去掉了,這個(gè)問(wèn)題不見(jiàn)得有多難,關(guān)鍵是花多長(zhǎng)時(shí)間去理解AngularJS框架本身,下面來(lái)看看詳細(xì)介紹,需要的朋友可以參考下。2017-03-03angular $watch 一個(gè)變量的變化(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇angular $watch 一個(gè)變量的變化(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解
這篇文章主要介紹了AngularJS基于factory創(chuàng)建自定義服務(wù)的方法,結(jié)合實(shí)例形式分析了AngularJS使用factory創(chuàng)建自定義服務(wù)的具體步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05Angular 4依賴注入學(xué)習(xí)教程之Injectable裝飾器(六)
這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之Injectable裝飾器的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-06-06angular實(shí)現(xiàn)頁(yè)面打印局部功能的思考與方法
這篇文章主要給大家介紹了關(guān)于angular實(shí)現(xiàn)頁(yè)面打印局部功能的思考與方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04簡(jiǎn)單談?wù)勱P(guān)于Angular Cli打包的事
使用過(guò)angular2人都應(yīng)該知道,angular2提供的Angular CLI來(lái)快速搭建,快速生成serives、component、derective、modulet各種模板...下面這篇文章主要給大家介紹了關(guān)于Angular Cli打包的一些事,需要的朋友可以參考下。2017-09-09在 Angular2 中實(shí)現(xiàn)自定義校驗(yàn)指令(確認(rèn)密碼)的方法
這篇文章給大家探索 Angular 2 內(nèi)建的自定義驗(yàn)證,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01angularJS與bootstrap結(jié)合實(shí)現(xiàn)動(dòng)態(tài)加載彈出提示內(nèi)容
這篇文章主要介紹了angularJS與bootstrap結(jié)合實(shí)現(xiàn)動(dòng)態(tài)加載彈出提示內(nèi)容,通過(guò)bootstrp彈出提示。感興趣的朋友可以參考下本篇文章2015-10-10詳解angularjs中如何實(shí)現(xiàn)控制器和指令之間交互
本篇文章主要介紹了詳解angularjs中如何實(shí)現(xiàn)控制器和指令之間交互,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05