angular動畫很強大,但在組件中寫一大堆比較礙眼,本例對經常使用的動畫效果進行獨立封裝,方便調用,保持基本動畫效果的一致性.減小寫動畫代碼的痛苦.html
本例定義了淡入/淡出/展開/收縮/飛入/飛出/放大/縮小效果,保存爲獨立ts文件導入組件便可使用.動畫
使用方法code
組件中orm
import { fadeIn, fadeOut, stretch, shrink, flyIn, flyOut, zoomIn, zoomOut } from './sim-anim.ts'; @Component({ // ... animations: [fadeIn, fadeOut, shrink, stretch, flyIn, flyOut, zoomIn, zoomOut] })
模板中htm
<div @flyIn @flyOut>...</div>
或ci
import { simAnim } from './sim-anim.ts'; @Component({ // ... animations: [simAnim] })
模板中animation
<div [@]simAnim="'flyIn'">...</div>
源it
//sim-anim.ts import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { trigger, state, style, transition, animate, keyframes } from '@angular/animations'; // 動畫時間線 var time = '300ms' var styles = { ease: time + ' ease ', linear: time + ' linear ', easeIn: time + ' ease-in', easeOut: time + ' ease-out', stepStart: time + ' step-start', stepEnd: time + ' step-end', easeInOut: time + ' ease-in-out', faseOutSlowIn: time + ' cubic-bezier(0.4, 0, 0.2, 1)', inOutBack: time + ' cubic-bezier(0.68, -0.55, 0.27, 1.55)', inOutCubic: time + ' cubic-bezier(0.65, 0.05, 0.36, 1)', inOutQuadratic: time + ' cubic-bezier(0.46, 0.03, 0.52, 0.96)', inOutSine: time + ' cubic-bezier(0.45, 0.05, 0.55, 0.95)' } // 動畫配置 var opts = { fadeIn: [ style({ opacity: 0 }), animate(styles.inOutBack, style({ opacity: 1 })), ], fadeOut: [ style({ opacity: 1 }), animate(styles.inOutBack, style({ opacity: 0 })) ], shrink: [ style({ height: '*' }), animate(styles.inOutBack, style({ height: 0 })) ], stretch: [ style({ height: '0' }), animate(styles.inOutBack, style({ height: '*' })) ], flyIn: [ style({ transform: 'translateX(-100%)' }), animate(styles.inOutBack, style({ transform: '*' })) ], flyOut: [ style({ transform: '*' }), animate(styles.inOutBack, style({ transform: 'translateX(-100%)' })) ], zoomIn: [ style({ transform: 'scale(.5)' }), animate(styles.inOutBack, style({ transform: '*' })) ], zoomOut: [ style({ transform: '*' }), animate(styles.inOutBack, style({ transform: 'scale(.5)' })) ] } // 導出動畫時間線定義,供自定義動畫的時候使用 export const animStyle = styles // 導出動畫 export const fadeIn = [trigger('fadeIn', [transition('void => *', opts.fadeIn)])] export const fadeOut = [trigger('fadeOut', [transition('* => void', opts.fadeOut)])] export const stretch = [trigger('stretch', [transition('void => *', opts.stretch)])] export const shrink = [trigger('shrink', [transition('* => void', opts.shrink)])] export const flyIn = [trigger('flyIn', [transition('void => *', opts.flyIn)])] export const flyOut = [trigger('flyOut', [transition('* => void', opts.flyOut)])] export const zoomIn = [trigger('zoomIn', [transition('void => *', opts.zoomIn)])] export const zoomOut = [trigger('zoomOut', [transition('* => void', opts.zoomOut)])] export const simAnim = [ trigger('simAnim', [ transition('* => fadeIn', opts.fadeIn), transition('* => fadeIn', opts.fadeOut), transition('* => shrink', opts.shrink), transition('* => stretch', opts.stretch), transition('* => flyIn', opts.flyIn), transition('* => flyOut', opts.flyOut), transition('* => zoomIn', opts.zoomIn), transition('* => zoomOut', opts.zoomOut) ]) ]