Angular - 列表元素的错开动画

时间:2017-09-01 08:12:57

标签: css angular typescript angular-animations

我有一个像这样定义的转换:

transition('show-content => hide-content', [
    query('.project-view-item', [
        style({
            opacity: 1,
            transform: 'translateX(0px)'
        }),
        stagger('0.3s', [
            animate(
                '0.2s',
                keyframes([
                    style({
                        opacity: 0,
                        transform: 'translateX(-40px)',
                        offset: 1
                    })
                ])
            )
        ])
    ])
])

一切都按预期工作。物品向左和淡出。问题是,在隐藏所有项目后,样式将被重置,并且它们会立即再次显示。我需要拥有名为animation-fill-mode: forwards的css3属性。

我怎么能以角度做到这一点?如何保留动画的最后状态?

Plnkr https://plnkr.co/edit/te0tJ76GkhkaRxF2LI2B

2 个答案:

答案 0 :(得分:2)

完成动画启动和动画将有助于此:

<强> HTML

    <div class="project-view" (@content.start)="start($event)" 
    (@content.done)="done($event)" [@content]="isShowContent ? 'show-content': 'hide-content'">
        <div class="myClass">
            <div class="project-view-item">Hello</div>
            <div class="project-view-item">Hi</div>
            <div class="project-view-item">Hello</div>
            <div class="project-view-item">Hi</div>
            <div class="project-view-item">Hello</div>
            <div class="project-view-item">Hi</div>
            <div class="project-view-item">Hello</div>
            <div class="project-view-item">Hi</div>
        </div>
    </div>

<强>打字稿:

  hideContent(callback?) {
    console.log("HIDE CONTENT CALLED");
    this.isShowContent = false;
    this.callback = callback;
  }

  showContent(callback?) {
    console.log("SHOW CONTENT CALLED");
    this.isShowContent = true;
    this.callback = callback;
  }

  done(event) {
    if (this.isShowContent) {
      document.querySelector('.myClass').setAttribute('style',
        "opacity:1;transform: 'translateX(0)')");
    }
    else {
      document.querySelector('.myClass').setAttribute('style',
        "opacity:0;transform: 'translateX(-40px)')");
    }

  }

  start(event) {
    document.querySelector('.myClass').setAttribute('style',
      "opacity:1; transform: 'translateX(-40px)')");
  }

<强> Demo

答案 1 :(得分:0)

此外,您可以在动画中添加state声明,这样您就不需要使用动画逻辑污染模板和ts文件:

transition('show-content => hide-content', [
    ....
]
state('hide-content', style({opacity: 0})),