Angular App中index.html中的动画

时间:2017-06-06 20:07:15

标签: angular css-animations

当Angular加载应用程序时,我试图在“App Loading”屏幕中设置fadeout效果的动画。但是,我找不到办法。这是代码:

<div class="container">
<my-app>
  <div class="loadingComp">
    <div class="sk-folding-cube">
      <div class="sk-cube1 sk-cube"></div>
      <div class="sk-cube2 sk-cube"></div>
      <div class="sk-cube4 sk-cube"></div>
      <div class="sk-cube3 sk-cube"></div>
    </div>
    <span>App loading, please wait.</span>
  </div>
  </my-app>    

尝试下面的CSS,但徒劳无功:

my-app:empty + .loadingComp {
  opacity: 1;
}

my-app:not(:empty) + .loadingComp {
 opacity: 0;
 animation: fadeOut 0.5s ease-in-out;
}

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

甚至尝试使用Angular Animations,但没有工作。有什么建议??

2 个答案:

答案 0 :(得分:0)

我不确定您是否要fadeOut'App Loading ...'消息或加载的第一个组件。但我会介绍这两种情况。

要在加载根组件之前为“App Loading”消息设置动画,需要添加一个可以运行动画的脚本。我正在使用jQuery fadeTo()功能(doc)。

的index.html:

<!DOCTYPE html>
<html>

  <head>
    <base href="." />
    <title>angular playground</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js/dist/zone.js"></script>
    <script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
      System.import('app')
        .catch(console.error.bind(console));
    </script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  </head>

  <body>
    <style>
      #animate {
        width: 100%;
        height: 250px;
        position: absolute;
        background-color: red;
      }
    </style>

    <my-app>
        <div id="animate"><h1>App loading, please wait.</h1></div>
    </my-app>

    <script>
      $( "#animate" ).fadeTo( 750 , 0.1, function() {
          // Animation complete.
        });
   </script>

  </body>

</html>

要向组件添加动画,我添加了animations元数据。

app.ts:

import {Component, NgModule, Input, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import {
  trigger,
  state,
  style,
  animate,
  transition
} from '@angular/animations';

@Component({
  selector: 'my-app',
  template: `
    <div [@flyInOut]="'out'">
      <h2>Hello {{name}}</h2>
    </div>
  `,
  animations: [
    trigger('flyInOut', [
      state('out', style({opacity: 0, transform: 'translateX(0)'})),
      transition('void => *', [
        style({
          opacity: 1,
          //transform: 'translateX(-100%)'
        }),
        animate('1s ease-in')
      ]),
      transition('* => void', [
        animate('0.2s 10 ease-out', style({
          opacity: 0,
          transform: 'translateX(100%)'
        }))
      ])
    ])
  ]
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule, BrowserAnimationsModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

以下是Plunker demo以供参考。希望这有帮助!

答案 1 :(得分:0)

感谢Nehal的时间。我用jQuery解决了它,但是一个不同的方法。我分配了一个&#39; id&#39;到我的应用程序&#39;选择器并在jQuery中使用它。

我用过这个:

$("body").on('DOMSubtreeModified', "#myApp", function () {
  $('.loadingComp').fadeOut(function () {
    $(this).remove();
  });
});