angular - 在运行时以 Angular 关闭动画

标签 angular animation

我知道我可以在引导应用程序时使用 BrowserAnimationModule 和 NoopAnimationsModule。如果用户出于 ADA 和其他原因请求关闭动画,推荐的运行时关闭动画的方法是什么?

最佳答案

可能有一个优雅的解决方案,但这从 angular 4.0.0 开始有效: (免责声明我为2.0找到了这段代码的基础)

在您的 app.module 中添加这些导入:

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AnimationDriver, ɵWebAnimationsDriver } from '@angular/animations/browser';

添加这个函数:

export function animationFactory(): any {
  const noop = AnimationDriver.NOOP;
  const driver = new ɵWebAnimationsDriver();
  return {
    animate: (element: any, keyframes: {
        [key: string]: string | number;
    }[], duration: number, delay: number, easing: string, previousPlayers?: any[]) => {
      if (!wantToAnimate) {
        return noop.animate(element, keyframes, duration, delay, easing, previousPlayers);
      } else {
        return driver.animate(element, keyframes, duration, delay, easing, previousPlayers);
      }
    }
  };
};

导入浏览器动画模块

imports: [
  ...,
  BrowserAnimationsModule,
] 

提供AnimationDriver,使用上面的工厂函数

providers: [
  ...,
  { provide: AnimationDriver, useFactory: animationFactory },
]

关于angular - 在运行时以 Angular 关闭动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055988/

相关文章:

Angular 7路由连接导致编译错误

css - 使用 ngStyle 定义网格区域时遇到问题

css - 动画在盒子里不起作用

javascript - ReactCSSTransitionGroup:transitionAppear 不起作用

javascript - Angular 2 中嵌套的 json 数据 - 如何过滤?

angular - 如何在 angular flex-layout 中换行,以便组件显示在新行中

javascript - JQuery 在第一次结束时调用动画函数

javascript - SVG createElementNS 'use' 这可能吗?

javascript - Velocity.js 并行运行具有持续时间的动画

angular - 如何在 Angular 4 中切换(显示/隐藏)元素?