vue项目性能优化(二)——首屏加loading

本文探讨了首屏优化的重要性,特别是通过在页面白屏期间添加loading动画来改善用户体验。作者分享了如何在HTML中实现一个简单的五环旋转动画,以减轻因chunk-vendors.js文件加载导致的空白屏幕问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们之所以进行性能优化,目的是为了提升用户体验。
一方面是提高网页的加载速度,另一方面也可以加载引导动画转移用户注意力,让用户觉得快。

今天我们要说的就是首屏优化——加loading动画。一般情况下,首屏加载都比较慢。因为chunk-vendors.xxx.js的体积比较大,没有加载之前页面会白屏,这样用户的体验就很差,我们可以在白屏时用动画替代。

给public\index.html中加上loading动画即可

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <style type="text/css">
      .loader {
        background: #000;
        background: radial-gradient(#222, #000);
        bottom: 0;
        left: 0;
        overflow: hidden;
        position: fixed;
        right: 0;
        top: 0;
        z-index: 99999;
      }

      .loader-inner {
        bottom: 0;
        height: 60px;
        left: 0;
        margin: auto;
        position: absolute;
        right: 0;
        top: 0;
        width: 100px;
      }

      .loader-line-wrap {
        animation: spin 2000ms cubic-bezier(.175, .885, .32, 1.275) infinite;
        box-sizing: border-box;
        height: 50px;
        left: 0;
        overflow: hidden;
        position: absolute;
        top: 0;
        transform-origin: 50% 100%;
        width: 100px;
      }
      .loader-line {
        border: 4px solid transparent;
        border-radius: 100%;
        box-sizing: border-box;
        height: 100px;
        left: 0;
        margin: 0 auto;
        position: absolute;
        right: 0;
        top: 0;
        width: 100px;
      }
      .loader-line-wrap:nth-child(1) { animation-delay: -50ms; }
      .loader-line-wrap:nth-child(2) { animation-delay: -100ms; }
      .loader-line-wrap:nth-child(3) { animation-delay: -150ms; }
      .loader-line-wrap:nth-child(4) { animation-delay: -200ms; }
      .loader-line-wrap:nth-child(5) { animation-delay: -250ms; }

      .loader-line-wrap:nth-child(1) .loader-line {
        border-color: hsl(0, 80%, 60%);
        height: 90px;
        width: 90px;
        top: 7px;
      }
      .loader-line-wrap:nth-child(2) .loader-line {
        border-color: hsl(60, 80%, 60%);
        height: 76px;
        width: 76px;
        top: 14px;
      }
      .loader-line-wrap:nth-child(3) .loader-line {
        border-color: hsl(120, 80%, 60%);
        height: 62px;
        width: 62px;
        top: 21px;
      }
      .loader-line-wrap:nth-child(4) .loader-line {
        border-color: hsl(180, 80%, 60%);
        height: 48px;
        width: 48px;
        top: 28px;
      }
      .loader-line-wrap:nth-child(5) .loader-line {
        border-color: hsl(240, 80%, 60%);
        height: 34px;
        width: 34px;
        top: 35px;
      }

      @keyframes spin {
          0%, 15% {
          transform: rotate(0);
        }
        100% {
          transform: rotate(360deg);
        }
      }
  
    </style>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    
    <div id="app">
      <div class="loader">
        <div class="loader-inner">
          <div class="loader-line-wrap">
            <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
            <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
            <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
            <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
            <div class="loader-line"></div>
          </div>
        </div>
      </div>
    </div>
    <!-- built files will be auto injected -->
  </body>
  
</html>

loading

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值