角动画的目的是什么?

我一直想知道现在 为什么我应该使用角动画超过 CSS 动画。我看到几个领域可以考虑之前使用它们:


表演

在第一步中,我发现这个 有个问题只处理性能方面的事情。接受的答案对我来说并不令人满意,因为它指出 只要有可能,就应该使用 CSS 动画,这样可以应用诸如在单独的线程中运行动画这样的优化。这似乎不是真的,因为 角度文件状态

角动画是 构建在标准 Web 动画 API 之上,在支持它的浏览器上运行。

(强调我的)

当我们查看 网络动画 API 草案时,我们发现对于表中指定的 CSS,可以对 Web 动画进行相同的优化。

虽然可以使用 ECMAScript 使用 requestAnimationFrame [ HTML ]来执行动画,但这些动画在 CSS 级联中的表示方式和性能优化方面与声明性动画有所不同,比如在单独的线程上执行动画。使用网络动画编程接口,它是 可以创建动画从脚本 具有与声明性动画相同的行为和性能特征

(再次强调我的)

除了像 IE 这样的浏览器不支持 Web Animations 之外,是否有任何理由在角动画上使用 CSS 声明或反之亦然?在性能方面是可交换的。


对动画的更多控制

这可能看起来像一个角动画的参数,因为你可以 暂停动画或使用它等 JS 变量,但同样是真实的,而使用如。CSS animation-play-state: pause或使用 JS 中指定的 CSS 变量,请参见 文件

现在我可以看到,在 JS 代码中设置 CSS 变量可能不太方便,但是在使用角动画时也是如此。这些通常是在 @Component animations字段中声明的,除了通过动画状态数据绑定属性之外,没有访问实例字段的权限(当然,如果你不通过 AnimationBuilder创建动画,顺便说一句,这也不是很方便或漂亮)。

另外一点是,通过 Web 动画 API,检查、调试或测试动画是可能的,但是我不认为角动画可以做到这一点。如果是的话,你能告诉我怎么做吗?如果不是,我真的 也看不出为了控制而使用角动画比使用 CSS 动画有什么好处


更干净的代码

我读过例如 给你的一个段落,其中说明将动画从“正常”样式中分离出来实际上就是将行为和表现分离出来。在样式表中声明动画真的混合了这些职责吗?我总是看到相反的情况,特别是在 @Component动画中看到的 CSS 规则给了我一种在很多地方都有 CSS 声明的感觉。


角动画怎么样?

  • 从其他样式中提取动画仅仅是一个方便的实用程序吗? 或者它带来了任何有价值的特性?
  • 角动画的使用是否只在特殊情况下才有回报,还是一个团队选择一直沿用这种惯例?

我很乐意在这里谈谈使用角动画的实际优势。谢谢大家!

7029 次浏览

So I did some research and although I didn't find any argument for nor against Angular animations performance-wise (as already stated in the question above), there are very good arguments to use Angular animations feature-wise which should be good enough for purists who want to have CSS only in sheets, at least in certain cases.

Let me list some useful features where each alone makes a convincing case for Angular animations. Most of them can be found in Angular animations documentation:

  1. Transition styles - these styles are only applied during transition from one state to another - only while an element is being animated, and one uses them like this:

    transition('stateA => stateB', [style({...}), animate(100)])
    

    Trying to do the same with CSS only might not be as expressive in terms of which previous state led to the next. And it can be outright clunky if the animation has to differ based on the initial state but the end state is the same.

  2. The void state together with :enter and :leave aliases (void documentation, :leave and :enter documentation) - Let you animate elements being added or removed from the DOM.

    transition('stateA => void', animate(...))
    

    This is very cool because previously, although it was easy enough to animate the addition, the removal was more complicated and required to trigger animation, wait till its end and only after that remove the element from the DOM, all with JS.

  3. Automatic property calculation '*' (documentation) - Allows for performing traditionally difficult animations like height transitions for elements with dynamic height. This problem required either to set fixed height on element (inflexible), use max-height with tuned transition function (not perfect) or query element's height with JS (potentially causing unnecessary reflows). But now with Angular it is as easy as this:

    trigger('collapsible', [
    state('expanded', style({ height: '*' })),
    state('collapsed', style({ height: '0' })),
    transition('expanded <=> collapsed', animate(100))
    ])
    

    And the animation is smooth and "complete" because the actual height of the element is used for the transition.

  4. Animation callbacks (documentation) - this is something that wasn't possible with CSS animations (if not maybe emulated with setTimeout) and is handy eg. for debugging.

  5. Unlike stated in the question, it is actually possible to use instance fields as params in Angular animations, see this question. I find it much easier to use than manipulating CSS variables through DOM API as shown here on MDN, not mentioning the limited support in some browsers.

If you need any of the features listed above, Angular can be a better tool for the task. Also when there is many animations to manage in a component, and this is just my personal opinion, I find it easier to organize animations the Angular way than have them in sheets, where it is harder too see the relationships between them and various element states.