這篇文章給大家分享的是有關(guān)Angular如何實現(xiàn)動畫的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站主營沁陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),沁陽h5成都微信小程序搭建,沁陽網(wǎng)站營銷推廣歡迎沁陽等地區(qū)企業(yè)咨詢
在實現(xiàn)的過程上,我采用了兩種不同的 Angular 動畫的方式:
使用 TypeScript 控制動畫
使用 @Component 中的 animations
Angular 動畫基礎(chǔ)
如 Angular 官網(wǎng)中的示例那樣,要在 Angular 應(yīng)用中添加動畫是比較簡單的一件事——前提是我們懂得添加的法則。如下是官網(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'))
])
]
})要使用動畫,需要在模板中使用 [@heroState]語法,這里的 heroState 對應(yīng)著 @Component 中的 heroState 相關(guān)的動畫。
在這個 trigger 中,我們定義了 inactive 和 active 兩個不同的 state。即當(dāng)模板中的 hero.state 發(fā)生變化的時候,我們就會找到對應(yīng)的 state 的樣式等等的內(nèi)容。
在這個 trigger 中,我們還定義了兩個 transition,即當(dāng)我們的 state 從 inactive => active 或者 active => inactive 時,我們就會執(zhí)行后面的動畫。
原理上,大概就是這么多了。然后,我就開始了我的動畫之旅。
購物車數(shù)量增加動畫
對于我的場景來說,要添加這個動畫并不難。無非就是上一個值淡出,新的值淡入:
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 })
])
)
])
])代碼就是這么簡單,這里用到了關(guān)鍵幀 keyframes,來進(jìn)行一些簡單的動畫轉(zhuǎn)換。
頁面縮放動畫
隨后,我需要做的就是對頁面的元素進(jìn)行縮放等效果,這個時候就需要用到 AnimationBuilder 來實現(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ù)制了頁面元素:
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');
這樣一來,就能復(fù)制頁面的 DOM,然后實現(xiàn)縮放效果了。
感謝各位的閱讀!關(guān)于“Angular如何實現(xiàn)動畫”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
新聞標(biāo)題:Angular如何實現(xiàn)動畫
網(wǎng)站地址:http://chinadenli.net/article12/jigsdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)頁設(shè)計公司、用戶體驗、網(wǎng)站制作、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)