简体   繁体   English

角4同时动画

[英]angular 4 simultaneous animations

I'm trying to port my webapp from AngularJS to Angular 4, and I'm having some issues with animations. 我正在尝试将我的webapp从AngularJS移植到Angular 4,我遇到了一些动画问题。 For a "Google Images" type of view, I have two animations that need to happen simultaneously when an item is clicked; 对于“Google图片”类型的视图,我有两个动画需要在单击项目时同时进行; one div needs its margin-bottom to increase, and a child of that div needs to gradually appear, using ngIf and a transition on its height. 一个div需要其margin-bottom增加,并且该div的一个孩子需要逐渐出现,使用ngIf和它的高度转换。 Both animations work separately, but if I put both animations on their respective elements, only the first one works. 这两个动画分开工作,但如果我将两个动画放在各自的元素上,那么只有第一个动画有效。

I've made a minimal plunker to demonstrate what I mean: https://plnkr.co/edit/27EFz3h16icnh3qDzQbY?p=preview 我做了一个最小的傻瓜来证明我的意思: https ://plnkr.co/edit/27EFz3h16icnh3qDzQbY?p =preview

const animationTiming = '1s ease';
@Component({
  selector: 'my-app',
  animations: [
    trigger('expand',
      [
        state('expanded', style({"margin-bottom": '392px'})),
        state('collapsed', style({"margin-bottom": '0'})),
        transition('* => *', animate(animationTiming))
      ]),
    trigger('visibility',
      [
        transition(
          ':enter', [
            style({'height': '0'}),
            animate(animationTiming, style({'height': '*'}))
          ]
        ),
        transition(
          ':leave', [
            style({'height': '*'}),
            animate(animationTiming, style({'height': '0'}))
          ]
        )
      ])
    ]
  template: `
    <button (click)="toggle()">click</button><br/>
    <div class="container" [@expand]="expanded?'expanded':'collapsed'">
      something here
      <div class="detail" [@visibility] *ngIf="expanded">something else in here</div>
    </div>
    <div class="container">something else</div>
  `,
})
export class App {
  expanded: boolean = false;
  toggle() {
    this.expanded = !this.expanded;
  }
}

If you click the button, what should happen is that the new content expands in between "something here" and "something else". 如果单击该按钮,应该发生的事情是新内容在“此处的内容”和“其他内容”之间展开。 What actually happens is that the two frames move away from each other as expected, but the new content just pops up immediately. 实际发生的是两个帧按预期相互远离,但新内容立即弹出。 If I remove the [@expand] animation from the first div, the new content does gradually grow out of nothing but obviously the two container divs don't make room for it. 如果我从第一个div中删除[@expand]动画,新内容会逐渐变得无用,但显然两个容器div不会为它腾出空间。

Is there a trick to make these animations work simultaneously? 有没有让这些动画同时工作的技巧?

I am not sure why this error occurs, but it works if you change your template like this : 我不确定为什么会出现此错误,但如果您更改模板,它会起作用:

<button (click)="toggle()">click</button><br/>
<div class="container" [@expand]="expanded?'expanded':'collapsed'">
something here</div>
<div class="detail" [@visibility] *ngIf="expanded">something else in here</div>
<div class="container">something else</div>

The behaviour of the two animations seems to work better, when the second <div> is not a sub-div of the first one. 当第二个<div>不是第一个<div>的子div时,两个动画的行为似乎更好。

And you can change the css code so the div stays between the other ones (by removing position: absolute ) : 你可以改变css代码,使div保持在其他代码之间(通过删除position: absolute ):

.detail {
  height: 360px;
  display: block;
  border: 1px solid black;
  left: 0px;
  right: 0px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM