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