Vue防止白屏添加首屏動畫

單頁應用有個無法避免的問題就是首屏加載慢,雖然可以通過<font color=red>gzip、路由懶加載、CDN、提高服務器帶寬</font>等手段,首屏加載速度仍然比傳統多頁應用慢一些。

為了提高用戶體驗,首屏添加loading動畫很有必要,并且實現特別簡單。

vue-cli3生成的項目中,打開index.html會發現如下代碼

<body>
  <noscript>
    <strong>We're sorry but doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  </noscript>
  <div id="app"></div>
  <!-- built files will be auto injected -->

</body>

我們只需要在這個div中插入loading代碼即可,vue初始化完成后會自動替換

<div id="app">此處插入loading代碼</div>

以下是我實現的一種動畫效果,可自行替換

image
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
  <meta http-equiv="X-UA-Compatible" content="chrome=1"/>
  <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  <link rel="stylesheet" type="text/css" >
  <title>demo</title>
  <style>
    .spinner {
      margin: 100px auto;
      width: 50px;
      height: 60px;
      text-align: center;
      font-size: 10px;
    }

    .spinner > div {
      background-color: #FE3C71;
      height: 100%;
      width: 6px;
      display: inline-block;
      -webkit-animation: stretchDelay 1.2s infinite ease-in-out;
      animation: stretchDelay 1.2s infinite ease-in-out;
    }

    .spinner .rect2 {
      -webkit-animation-delay: -1.1s;
      animation-delay: -1.1s;
    }

    .spinner .rect3 {
      -webkit-animation-delay: -1.0s;
      animation-delay: -1.0s;
    }

    .spinner .rect4 {
      -webkit-animation-delay: -0.9s;
      animation-delay: -0.9s;
    }

    .spinner .rect5 {
      -webkit-animation-delay: -0.8s;
      animation-delay: -0.8s;
    }

    @-webkit-keyframes stretchDelay {
      0%, 40%, 100% {
        -webkit-transform: scaleY(0.4)
      }
      20% {
        -webkit-transform: scaleY(1.0)
      }
    }

    @keyframes stretchDelay {
      0%, 40%, 100% {
        transform: scaleY(0.4);
        -webkit-transform: scaleY(0.4);
      }
      20% {
        transform: scaleY(1.0);
        -webkit-transform: scaleY(1.0);
      }
    }
  </style>
</head>
<body>
<noscript>
  <strong>We're sorry but demo doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app">
  <div class="spinner">
    <div class="rect1"></div>
    <div class="rect2"></div>
    <div class="rect3"></div>
    <div class="rect4"></div>
    <div class="rect5"></div>
  </div>
</div>
<!-- built files will be auto injected -->
</body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 要招一個會vue的開發者: 作為面試官的你,你還會每次都只是問這些老土的問題嗎?你對MVVM的理解是什么?你知道什...
    浪子神劍閱讀 23,132評論 0 260
  • 只要你是天鵝蛋,就是生在養雞場里也沒有什么關系。 你現在努力奮斗的樣子 大抵可以預見60歲后的模樣 女人永遠新鮮的...
    葉娜姐姐閱讀 355評論 3 4
  • 旅途中,,, 大概到江西了吧,一場由南到北的征程,穿過擁擠的人潮,在春運的大軍中奉獻自己的一份力量,火車上,兩個小...
    懶貓咪閱讀 182評論 0 1
  • 2018年08月11日 星期六 天氣 晴 今天是廈小暑假第二次返校日,早上6點半就帶了倆寶貝去學校。今天是學校...
    言書9130閱讀 451評論 1 6