实现原理:该方法通过在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况。

1、源码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页加载进度条</title>
<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/pace/1.2.4/pace.min.js" type="application/javascript"></script>
<link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/pace/1.2.4/pace-theme-default.min.css" type="text/css" rel="stylesheet" />
<style>
.pace {
    z-index:999;
    pointer-events:none;
    user-select:none;
    z-index:2;
    position:fixed;
    margin:auto;
    top: 5px;
    left:0;
    right:0;
    height: 12px;
    border-radius:8px;
    width: 8rem;
    background:#eaecf2;
    overflow:hidden
}
.pace-inactive .pace-progress {
    opacity:0;
    transition:.3s ease-in
}
.pace.pace-inactive {
    opacity:0;
    transition:.3s;
    top: -8px
}
.pace .pace-progress {
    box-sizing:border-box;
    transform:translate3d(0,0,0);
    position:fixed;
    z-index:999;
    display:block;
    position:absolute;
    top:0;
    right:100%;
    height:100%;
    width:100%;
    background:#49b1f5;
    background:linear-gradient(to right,#12c2e9,#c471ed,#f64f59);
    animation:gradient 2s ease infinite;
    background-size:200%
}
</style>
</head>
<body>
<br>
请按下 Ctrl + F5 或 F5 刷新您的网页!

</body>
</html>

2、如果上面的源代码失效,请将源代码 head 标签之内的代码替换为以下代码之一即可:

<head>
    <script src="https://cdn.jsdelivr.net/npm/pace-js@latest/pace.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js@latest/pace-theme-default.min.css">
</head>

或者

<script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/pace/1.2.4/pace.min.js" type="application/javascript"></script>
<link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/pace/1.2.4/pace-theme-default.min.css" type="text/css" rel="stylesheet" />

原文地址