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

在使用 Vant UI 时添 `loading` 效果,可以通过 Vant 提供的 `Loading` 组件来实现。Vant 提供了多种方式来展示载状态,包括全载、局部载以及自定义内容和样式的载提示。 ### 展示全 Loading 可以使用 `Toast.loading` 方法来展示一个全载提示。该方法会返回一个 `Toast` 实例,可以通过调用 `.clear()` 方法来关闭载提示。 ```javascript import { Toast } from 'vant'; // 显示全 loading const toast = Toast.loading({ message: '载中...', forbidClick: true, // 是否禁止背景点击 duration: 0 // 持续时间,默认单位为毫秒,设为 0 表示不自动关闭 }); // 在某个事件后关闭 loading setTimeout(() => { toast.clear(); }, 3000); ``` ### 展示局部 Loading 如果只想在某个区域显示载状态,可以使用 `<van-loading>` 标签,并将其放置在需要展示载状态的位置。 ```html <template> <div class="loading-container"> <van-loading v-if="isLoading" /> <div v-else> <!-- 页面内容 --> </div> </div> </template> <script> export default { data() { return { isLoading: true }; } }; </script> <style scoped> .loading-container { text-align: center; padding: 20px; } </style> ``` ### 自定义 Loading 内容和样式 Vant 的 `Toast.loading` 支持自定义 `message`,同时也可以通过 CSS 自定义 `<van-loading>` 的样式。 ```javascript Toast.loading({ message: '自定义文案', duration: 2000 }); ``` ```html <van-loading type="spinner" color="black" /> ``` ### 结合路由拦截器使用 Loading 在实际项目中,`loading` 效果常常用于页面载或数据请求过程中。结合路由拦截器,可以在路由切换时展示载效果,待数据载完成后隐藏。 ```javascript // 示例:路由拦截器中使用 loading router.beforeEach((to, from, next) => { const toast = Toast.loading({ message: '载中', duration: 0 }); // 模拟数据载 setTimeout(() => { toast.clear(); next(); }, 1500); }); ``` 通过以上方式,可以灵活地在 Vant UI 中添 `loading` 效果,以提升用户体验并明确告知用户当前处于载状态[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值