自定義 Angular 4 首屏加載動畫

默認狀況下,Angular應用程序在首次加載根組件時,會在瀏覽器的顯示一個loading... 咱們能夠輕鬆地將loading修改爲咱們本身定義的動畫。css

這是咱們要實現首次加載的效果:

loading

根組件標籤中的內容

請注意,在你的入口文件index.html中,默認的loading...只是插入到根組件標籤之間:html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Fancy Loading Screen</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>

  <app-root>Loading...</app-root>

</body>
</html>

若是您在加載完根組件檢查應用程序,則沒法找到loading... 的文字,由於它在應用加載完成後被咱們本身定義的組件替換掉。瀏覽器

這意味着咱們能夠在這些標籤之間放置任何內容,包括樣式定義,一旦Angular加載完根組件,就能夠徹底清除它們。app

<app-root>
  <style>
    app-root {
      color: purple;
    }
  </style>
  I'm a purple loading message!
</app-root>

咱們沒必要擔憂這些樣式會影響咱們的應用程序加載後的內容,由於一切都被徹底替換掉。svg

如今你能夠在那裏隨意的作任何事情。使用css或者svg實現自定義加載動畫。字體

在咱們的示例中,咱們給頁面一個粉紅色的背景,咱們使用Flexbox 將loading設置居中,給它設置一個更漂亮的字體,咱們甚至在省略號上添加一個自定義動畫:flex

<app-root>
  <style>
  app-root {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;

    color: pink;
    text-transform: uppercase;
    font-family: -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        Roboto,
        Oxygen-Sans,
        Ubuntu,
        Cantarell,
        Helvetica,
        sans-serif;
    font-size: 2.5em;
    text-shadow: 2px 2px 10px rgba(0,0,0,0.2);
  }
  body {
    background: salmon;
    margin: 0;
    padding: 0;
  }

  @keyframes dots {
    50% {
      transform: translateY(-.4rem);
    }
    100% {
      transform: translateY(0);
    }
  }

  .d {
    animation: dots 1.5s ease-out infinite;
  }
  .d-2 {
    animation-delay: .5s;
  }
  .d-3 {
    animation-delay: 1s;
  }
  </style>

  Loading<span class="d">.</span><span class="d d-2">.</span><span class="d d-3">.</span>
</app-root>

這樣咱們就實現了上圖的加載效果了,點擊這裏查看原文動畫

分享幾個loading效果的在線素材網:spa

好了,去建立屬於你本身的loading吧!

相關文章
相關標籤/搜索