将动画的定义放置在单独的文件中,方便多组件调用

  1. import { animate, keyframes, style, transition, trigger } from "@angular/animations"
  2. export const slide = trigger("slide", [
  3. transition(":enter", [
  4. style({ opacity: 0, transform: "translateY(40px)" }),
  5. animate(250)
  6. ]),
  7. transition(":leave", [
  8. animate(
  9. 600,
  10. keyframes([
  11. style({ offset: 0.3, transform: "translateX(-80px)" }),
  12. style({ offset: 1, transform: "translateX(100%)" })
  13. ])
  14. )
  15. ])
  16. ])
  1. import { slide } from "./animations"
  2. @Component({
  3. animations: [slide]
  4. })

抽取具体的动画定义,方便多动画调用

  1. import {animate, animation, keyframes, style, transition, trigger, useAnimation} from "@angular/animations"
  2. export const slideInUp = animation([
  3. style({ opacity: 0, transform: "translateY(40px)" }),
  4. animate(250)
  5. ])
  6. export const slideOutLeft = animation([
  7. animate(
  8. 600,
  9. keyframes([
  10. style({ offset: 0.3, transform: "translateX(-80px)" }),
  11. style({ offset: 1, transform: "translateX(100%)" })
  12. ])
  13. )
  14. ])
  15. export const slide = trigger("slide", [
  16. transition(":enter", useAnimation(slideInUp)),
  17. transition(":leave", useAnimation(slideOutLeft))
  18. ])

调用动画时传递运动总时间,延迟时间,运动形式

  1. export const slideInUp = animation(
  2. [
  3. style({ opacity: 0, transform: "translateY(40px)" }),
  4. animate("{{ duration }} {{ delay }} {{ easing }}")
  5. ],
  6. {
  7. params: {
  8. duration: "400ms",
  9. delay: "0s",
  10. easing: "ease-out"
  11. }
  12. }
  13. )
  1. transition(":enter", useAnimation(slideInUp, {params: {delay: "1s"}}))