Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購(gòu)物車動(dòng)畫實(shí)例代碼
前言
在前端應(yīng)用中,動(dòng)畫是一個(gè)常見(jiàn)的場(chǎng)景。在使用了一系列的動(dòng)畫庫(kù)之后,終于需要自己來(lái)實(shí)現(xiàn)一個(gè)動(dòng)畫了。這次的動(dòng)畫則是基于 Angular 框架。我的場(chǎng)景是一個(gè)類似于添加購(gòu)物車的動(dòng)畫。在這個(gè)場(chǎng)景里,需要兩個(gè)動(dòng)畫,一個(gè)是購(gòu)物車數(shù)量的增加動(dòng)畫,一個(gè)則是折疊頁(yè)面元素的動(dòng)畫。
在實(shí)現(xiàn)的過(guò)程上,我采用了兩種不同的 Angular 動(dòng)畫的方式:
- 使用 TypeScript 控制動(dòng)畫
- 使用 @Component 中的 animations
Angular 動(dòng)畫基礎(chǔ)
如 Angular 官網(wǎng)中的示例那樣,要在 Angular 應(yīng)用中添加動(dòng)畫是比較簡(jiǎn)單的一件事——前提是我們懂得添加的法則。如下是官網(wǎng)的示例:
@Component({
selector: 'app-hero-list-basic',
template: `
<ul>
<li *ngFor="let hero of heroes"
[@heroState]="hero.state"
(click)="hero.toggleState()">
{{hero.name}}
</li>
</ul>
`,
styleUrls: ['./hero-list.component.css'],
animations: [
trigger('heroState', [
state('inactive', style({
backgroundColor: '#eee',
transform: 'scale(1)'
})),
state('active', style({
backgroundColor: '#cfd8dc',
transform: 'scale(1.1)'
})),
transition('inactive => active', animate('100ms ease-in')),
transition('active => inactive', animate('100ms ease-out'))
])
]
})
要使用動(dòng)畫,需要在模板中使用 [@heroState]語(yǔ)法,這里的 heroState 對(duì)應(yīng)著 @Component 中的 heroState 相關(guān)的動(dòng)畫。
- 在這個(gè) trigger 中,我們定義了 inactive 和 active 兩個(gè)不同的 state。即當(dāng)模板中的 hero.state 發(fā)生變化的時(shí)候,我們就會(huì)找到對(duì)應(yīng)的 state 的樣式等等的內(nèi)容。
- 在這個(gè) trigger 中,我們還定義了兩個(gè) transition,即當(dāng)我們的 state 從 inactive => active 或者 active => inactive 時(shí),我們就會(huì)執(zhí)行后面的動(dòng)畫。
原理上,大概就是這么多了。然后,我就開(kāi)始了我的動(dòng)畫之旅。
購(gòu)物車數(shù)量增加動(dòng)畫
對(duì)于我的場(chǎng)景來(lái)說(shuō),要添加這個(gè)動(dòng)畫并不難。無(wú)非就是上一個(gè)值淡出,新的值淡入:
trigger('count', [
transition('void => current', [
animate(
'400ms 150ms',
keyframes([
style({ opacity: 0.6, transform: 'translateY(0)', offset: 0 }),
style({ opacity: 0.3, transform: 'translateY(-15px)', offset: 0.5 }),
style({ opacity: 0, transform: 'translateY(-30px)', offset: 1 })
])
)
]),
transition('void => last', [
animate(
250,
keyframes([
style({ opacity: 0, transform: 'translateY(100%)', offset: 0 }),
style({ opacity: 0.3, transform: 'translateY(15px)', offset: 0.5 }),
style({ opacity: 0.8, transform: 'translateY(0)', offset: 1.0 })
])
)
])
])
代碼就是這么簡(jiǎn)單,這里用到了關(guān)鍵幀 keyframes,來(lái)進(jìn)行一些簡(jiǎn)單的動(dòng)畫轉(zhuǎn)換。
頁(yè)面縮放動(dòng)畫
隨后,我需要做的就是對(duì)頁(yè)面的元素進(jìn)行縮放等效果,這個(gè)時(shí)候就需要用到 AnimationBuilder 來(lái)實(shí)現(xiàn)了:
const myAnimation = this.animationBuilder.build([
animate(
1000,
keyframes([
style({ opacity: 0.8, transform: 'scale(0.8)', offset: 0.3 }),
style({ opacity: 0.3, transform: 'scale(0.3)', offset: 0.5 }),
style({ opacity: 0.2, transform: 'scale(0.2) translate(12000px, 8000px)', offset: 1 })
])
)
]);
const player = myAnimation.create(forkFormComponent);
player.play();
player.onDone(() => {
const nativeElement = this.cartContainer.nativeElement;
nativeElement.removeChild(nativeElement.childNodes[0]);
this.renderer.setStyle(nativeElement, 'display', 'none');
});
在那之前,我先復(fù)制了頁(yè)面元素:
const formElement = this.formElement.nativeElement; const forkFormComponent = this.cartContainer.nativeElement; forkFormComponent.appendChild(formElement.cloneNode(true)); this.renderer.setStyle(forkFormComponent, 'display', 'block'); this.renderer.setStyle(forkFormComponent, 'position', 'absolute'); this.renderer.setStyle(forkFormComponent, 'top', '-300px'); this.renderer.setStyle(forkFormComponent, 'left', '0');
這樣一來(lái),就能復(fù)制頁(yè)面的 DOM,然后實(shí)現(xiàn)縮放效果了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果
- Angular4實(shí)現(xiàn)鼠標(biāo)懸停3d傾斜效果
- 在AngularJS應(yīng)用中實(shí)現(xiàn)一些動(dòng)畫效果的代碼
- angularJS結(jié)合canvas畫圖例子
- 用AngularJS的指令實(shí)現(xiàn)tabs切換效果
- AngularJS實(shí)現(xiàn)按鈕提示與點(diǎn)擊變色效果
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫效果的方式總結(jié)
- 教你用AngularJS框架一行JS代碼實(shí)現(xiàn)控件驗(yàn)證效果
- 基于angularjs實(shí)現(xiàn)圖片放大鏡效果
- AngularJS實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)畫矩形框示例【可兼容IE8】
相關(guān)文章
AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的獲取焦點(diǎn)及失去焦點(diǎn)時(shí)的表單驗(yàn)證功能,涉及AngularJS使用ng-blur、ng-focus針對(duì)表單事件監(jiān)聽(tīng)相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
ng2學(xué)習(xí)筆記之bootstrap中的component使用教程
現(xiàn)在angular2已經(jīng)除了集成的angular-cli,建議大家可以基于這個(gè)來(lái)快速開(kāi)發(fā)ng2的項(xiàng)目,不用自己再搭建環(huán)境。接下來(lái)通過(guò)本文給大家介紹 bootstrap中的component使用教程,需要的朋友可以參考下2017-03-03
關(guān)于AngularJs數(shù)據(jù)的本地存儲(chǔ)詳解
本文主要介紹了每一個(gè)獨(dú)立的JS文件或者不同的控制器如何實(shí)現(xiàn)數(shù)據(jù)的共享與交互的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
AngularJS中如何使用$parse或$eval在運(yùn)行時(shí)對(duì)Scope變量賦值
這篇文章主要介紹了AngularJS中如何使用$parse或$eval在運(yùn)行時(shí)對(duì)Scope變量賦值的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01
AngularJS 仿微信圖片手勢(shì)縮放的實(shí)例
這篇文章主要介紹了AngularJS 仿微信圖片手勢(shì)縮放的實(shí)例的相關(guān)資料,希望大家通過(guò)本文實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
angular基于ng-alain定義自己的select組件示例
這篇文章主要介紹了angular基于ng-alain定義自己的select組件示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

