angular 2 animate 筆記

很久沒有在這裏寫點筆記了。時隔已久,angular1 去 angular2 咯javascript

筆記來源https://angular.cn/docs/ts/latest/guide/animations.htmlcss

動畫基於這標準:https://w3c.github.io/web-animations/html

 

如下是基本設置java

template: `
    <button (click)="heroState = 'active'">enter</button>
    <button (click)="heroState = null">leave</button>
    <button (click)="changeAnimate()">animate</button>
    <div *ngIf="heroState" [@heroState]="heroState" 
      (@heroState.start)="animationStarted($event)"
      (@heroState.done)="animationDone($event)"
      style="width:100px; height:100px;">example</div>
      {{heroState}}
`,
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'))
    ])
  ]

 

在animate 中,須要理解幾樣東西就能明白真個筆記。css3

1. @trigger (綁定elem)
2. 狀態 (經過狀態轉換改變style)
3. 過渡 (動畫)

狀態與過渡 :state 是狀態,代表樣式。過渡是動畫,聲明某個狀態去到某個狀態git

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'))

合拼過渡github

transition('inactive <=> active', animate('100ms ease-out'))

轉換不保留樣式:在過渡添加style,意思是當狀態轉換時,會使用指定樣式,接着執行動畫,結束後移除樣式web

transition('inactive => active', [
  style({
    backgroundColor: '#cfd8dc',
    transform: 'scale(1.3)'
  }),
  animate('80ms ease-in', style({
    backgroundColor: '#eee',
    transform: 'scale(1)'
  }))
]),

通配符*,進場,出場 : 都是狀態轉換的方式angular2

    transition('inactive => *', [
      style({transform: 'translateX(-100%)'}),
      animate(100)
    ])

    transition('* => inactive', [
      animate(100, style({transform: 'translateX(100%)'}))
    ])


    transition('* => void', [ //:leave  省略寫法
      animate(100, style({transform: 'translateX(100%)'}))
    ])


    transition('void => *', [ //:enter  省略寫法
      animate(100, style({transform: 'translateX(100%)'}))
    ])

動畫變量 : duration, delay,緩動(easing)函數ide

animate('2000ms 10 ease-in', style({
  backgroundColor: '#f00',
  transform: 'translateX(100%)'
})),

高級寫法:keyframe (css3 原理)

      animate(300, keyframes([
        style({opacity: 0, transform: 'translateX(-100%)', offset: 0}),
        style({opacity: 1, transform: 'translateX(15px)',  offset: 0.3}),
        style({opacity: 1, transform: 'translateX(0)',     offset: 1.0})
      ]))

組合animate : 處理混合動畫

      group([
        animate('0.3s 0.1s ease', style({
          transform: 'translateX(0)',
          width: 120
        })),
        animate('0.3s ease', style({
          opacity: 1
        }))
      ])

回調 : $event 能夠獲得 fromStatetoStatetotalTime

template: `
  <ul>
    <li *ngFor="let hero of heroes"
        (@flyInOut.start)="animationStarted($event)"
        (@flyInOut.done)="animationDone($event)"
        [@flyInOut]="'in'">
      {{hero.name}}
    </li>
  </ul>
`,
相關文章
相關標籤/搜索