首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular 2中执行无限动画?

如何在Angular 2中执行无限动画?
EN

Stack Overflow用户
提问于 2017-06-14 11:52:27
回答 3查看 7.6K关注 0票数 7

基本上,我想利用angular (目前是4)中的web- animations -api polyfill在元素上执行无限的动画。

让我们看一个基本的非角度示例:

代码语言:javascript
复制
var ball = document.getElementById('ball');

ball.animate([
  { transform: 'scale(0.5)' },
  { transform: 'scale(1)' }
], {
  duration: 1000,
  iterations: Infinity,
  direction: 'alternate',
  easing: 'ease-in-out'
});
代码语言:javascript
复制
.container {
  position: relative;
  width: 100%;
  height: 200px;
}

.ball {
  position: absolute;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 2px solid #E57373;
  background: #F06292;
  box-shadow: 0px 0px 15px #F06292;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.2.5/web-animations.min.js"></script>
<div class="container">
  <div class="ball" id="ball"><div>
</div>

我怎么把它翻译成Angular?

这是我到目前为止尝试过的方法,但只有效一次:

代码语言:javascript
复制
animations: [
  trigger('scale', [
    transition('* <=> *', animate('1s ease-in-out', keyframes([
      style({ transform: 'scale(0.5)' }),
      style({ transform: 'scale(1)' })
    ]))) // How do I specify the iterations, or the direction? 
  ])
]

有没有办法用@angular/动画插件来代替存储ElementRef并按照上面的例子来做呢?或者也许我误解了这个插件的用途?

提前谢谢。

EN

回答 3

Stack Overflow用户

发布于 2017-09-14 15:09:24

我在为我的项目搜索无限的动画。在angular.io中没有这方面的文档。因此,我尝试了这种方法,花了我很多小时才做到这一点。希望能对其他人有所帮助。首先在class中定义animation

代码语言:javascript
复制
animations: [
trigger('move', [
  state('in', style({transform: 'translateX(0)'})),
  state('out', style({transform: 'translateX(100%)'})),
  transition('in => out', animate('5s linear')),
  transition('out => in', animate('5s linear'))
]),
]

然后将其插入到html

代码语言:javascript
复制
<div [@move]="state" (@move.done)="onEnd($event)"></div>

然后设置state = 'in',并在ngAfterViewInit()生命周期挂钩中,使用setTimeout函数和结束回调函数更新state属性值'out',将state属性值更新为'in'并检查state属性值,如果为in,则通过D16函数更新为<代码>D15函数,如下所示

代码语言:javascript
复制
export class AppComponent implements AfterViewInit {
  state = 'in';
  ngAfterViewInit() {
    setTimeout(() => {
      this.state = 'out';
    }, 0);
  }
  onEnd(event) {
    this.state = 'in';
    if (event.toState === 'in') {
      setTimeout(() => {
        this.state = 'out';
      }, 0);
    }
  }
}

快乐编码:)

票数 19
EN

Stack Overflow用户

发布于 2018-09-03 20:23:32

在Angular中可以实现无限的动画,在上面没有太多的东西,但这对我的onenter和onleave动画来说很好。

当“mouseenter”动画循环播放时,它会在“mouseleave”停止。

我使用的是Angular 6。

Angular命令行: 6.0.8节点: 8.9.3操作系统: linux x64 Angular: 6.1.4...动画,通用,编译器,编译器-cli,核心,窗体...http、语言服务、平台浏览器...平台-浏览器-动态,路由器

rxjs 6.2.2 typescript 2.7.2 webpack 4.8.3

代码语言:javascript
复制
import {
  trigger,
  state,
  style,
  animate,
  transition
} from '@angular/animations';

@Component({
  selector: 'hot-spot',
  templateUrl: './myComp.component.html',
  styleUrls: ['./myComp.component.css'],
  animations: [
    trigger('compState', [
      state('small', style({
        opacity: '0.5',
        transform: 'scale(0.8)'
      })),
      state('large', style({
        opacity: '1',
        transform: 'scale(1.2)'
      })),
      transition('small => large', animate('0.6s 100ms ease-in')),
      transition('large => small', animate('0.7s 100ms ease-out'))
    ]),
  ]
})
export class MyComponent {

  state: string = 'none';
  isEnter: boolean = false;


  onMouseEnter(evt: MouseEvent) {
    console.log('onMouseEnter()');
    this.state = 'small';
    this.isEnter = true;
  }

  onMouseLeave(evt: MouseEvent) {
    console.log('onMouseLeave()');
    this.state = 'large';
    this.isEnter = false;
  }

  onEnd(event) {
    if (this.isEnter) {
      if (event.toState === 'small') {
        this.state = 'large';
      }
      else {
        this.state = 'small';
      }
    }
  }

}

和HTML

代码语言:javascript
复制
<div
  (mouseenter)="onMouseEnter($event)"
  (mouseleave)="onMouseLeave($event)">

  <div>
    <div
      [@compState]="state"
      (@compState.done)="onEnd($event)">

      <button
        (click)="onSceneClick($event,data.sceneId)"
        type="button"
        class="btn btn-link" >
        <i class="fa fa-angle-double-up" style="font-size:16pt;color:red">
          <br><span style="font-size:12pt">{{data.text}}</span>
        </i>

      </button>
    </div>
  </div>
票数 2
EN

Stack Overflow用户

发布于 2019-11-19 10:44:42

您可以尝试WebComponents以声明的方式使用Web Animations API (使用组件对任何html元素进行动画处理):

代码语言:javascript
复制
<!-- Add Web Animations Polyfill :) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.2/web-animations.min.js"></script>

<script type="module" src="https://unpkg.com/@proyecto26/animatable-component@1.0.0/dist/animatable-component/animatable-component.esm.js"></script>
<script nomodule="" src="https://unpkg.com/@proyecto26/animatable-component@1.0.0/dist/animatable-component/animatable-component.js"></script>

<animatable-component
  autoplay
  easing="ease-in-out"
  duration="800"
  delay="300"
  animation="zoomIn"
  iterations="Infinity"
  direction="alternate"
  style="display: flex;align-self: center;"
>
  <h1>Hello World</h1>
</animatable-component>

有很多已定义的动画,但您也可以使用keyFrames创建自定义动画。

选中此处可将其与AngularReactVueJS等一起使用:https://github.com/proyecto26/animatable-component#framework-integrations

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44535108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档