繁体   English   中英

Angular App中index.html中的动画

[英]animation in index.html in Angular App

Angular加载应用程序时,我正在尝试在“应用程序加载”屏幕中设置淡入淡出效果的动画。 但是,我找不到解决方法。 这是代码:

<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动画,但无法正常工作。 有什么建议吗??

我不确定是否要fadeOut “ 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 演示 希望这可以帮助!

谢谢Nehal的宝贵时间。 我使用jQuery解决了问题,但使用了另一种方法。 我为“ my-app”选择器分配了一个“ id”,并在jQuery中使用了它。

我用这个:

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

暂无
暂无

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

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