javascript - 具有计算时间的 Angular 2 动画

标签 javascript angular angular2-directives

使用 TypeScript 和 Angular 2.4

我有一个带有滚动文本的组件(如字幕),我有一个指定滚动速度(像素/秒)的参数。

如何使用组件中的方法动态指定动画长度?

假设有以下组件:

import {Component, Input, ElementRef, trigger, state, style, transition, animate} from '@angular/core';
import * as $ from 'jquery';

const SCROLL_SPEED_DEFAULT = 25;

@Component({
  selector: 'text-scroll',
  template: '<span><ng-content></ng-content></span>',
  animations: [
    trigger('hover', [
      state('idle', style({transformX: '0'})),
      state('scrolled', style({transformX: '-100%'})),
      transition('idle => scrolled', [
        animate(this.getAnimationSpeed())
      ])
    ])
  ]
})
export class TextScrollComponent {
  @Input('speed') speedTime: number = SCROLL_SPEED_DEFAULT;
  private element: HTMLElement;

  constructor(private el: ElementRef) {
    this.element = this.el.nativeElement;
  }

  public getAnimationSpeed(): number {
    let element = this.element;
    let elementWidth = $(element).width();
    let difference = this.el.nativeElement.scrollWidth - elementWidth;

    return (difference / Number(this.speedTime)) * 1000;
  }
}

模板:

<text-scroll>My very <b>loooong..</b> text</text-scroll>

最佳答案

目前,我找不到使用@Component 装饰器来做到这一点的方法,但也许 Renderer.animate 是合适的

关于javascript - 具有计算时间的 Angular 2 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42786892/

相关文章:

Angular 2 : Pass value from attribute directive as a component variable

angular - 检测变量的变化?

javascript - css 属性在 ngFor 上的 Angular 4 中使用 ngStyle 应用于错误的元素

javascript 异常,在使用 VS 2013 安装 typescript 1.4 后未被调试器捕获

javascript - Angular2管道动态值不取

javascript - CSS 中的文本不会在图像下对齐

javascript - 即使短语确实存在于数组中,正则表达式搜索也会返回 -1

javascript - Internet Explorer (10, 11) 加载 Angular 2 应用程序非常慢

javascript - 错误类型错误 : Cannot read property 'formData' of undefined

javascript - 添加事件监听错误