這篇文章給大家分享的是有關(guān)Angular如何實現(xiàn)動畫的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
在海港等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網(wǎng)站設(shè)計制作、做網(wǎng)站 網(wǎng)站設(shè)計制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)網(wǎng)站制作,海港網(wǎng)站建設(shè)費用合理。在實現(xiàn)的過程上,我采用了兩種不同的 Angular 動畫的方式:
使用 TypeScript 控制動畫
使用 @Component 中的 animations
Angular 動畫基礎(chǔ)
如 Angular 官網(wǎng)中的示例那樣,要在 Angular 應用中添加動畫是比較簡單的一件事——前提是我們懂得添加的法則。如下是官網(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 對應著 @Component 中的 heroState 相關(guān)的動畫。
在這個 trigger 中,我們定義了 inactive 和 active 兩個不同的 state。即當模板中的 hero.state 發(fā)生變化的時候,我們就會找到對應的 state 的樣式等等的內(nèi)容。
在這個 trigger 中,我們還定義了兩個 transition,即當我們的 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,來進行一些簡單的動畫轉(zhuǎ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'); });
在那之前,我先復制了頁面元素:
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');
這樣一來,就能復制頁面的 DOM,然后實現(xiàn)縮放效果了。
感謝各位的閱讀!關(guān)于“Angular如何實現(xiàn)動畫”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
標題名稱:Angular如何實現(xiàn)動畫-創(chuàng)新互聯(lián)
文章鏈接:http://aaarwkj.com/article46/jcshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、動態(tài)網(wǎng)站、企業(yè)建站、域名注冊、營銷型網(wǎng)站建設(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)
猜你還喜歡下面的內(nèi)容