时间:2021-08-01加入收藏
$({property: 0}).animate({property: 100}, {
duration: 5000,//进度条加载进度的速度
step: function() {
var percentage = Math.round(this.property);
$('#progress').css('width', percentage+"%");
if(percentage == 100) {
$("#progress").addClass("done");//100%后消失
}
}
});
body{
margin:0;
}
#progress {
position:fixed;
height: 2px;
background:#b91f1f;
transition:opacity 500ms linear
}
#progress.done {
opacity:0
}
#progress span {
position:absolute;
height:2px;
opacity:1;
width:150px;
right:-10px;
}
@-webkit-keyframes pulse {
30% {
opacity:.6
}
60% {
opacity:0;
}
100% {
opacity:.6
}
}
<body>
<div id="progress">
<span><span>
</div>
<body>
2、使用插件:
nprogress是一个jQuery插件,只有几K大小。使用起来非常方便和简单。
先引入
<script src='https://lib.baomitu.com/nprogress/0.2.0/nprogress.js'></script>
<link rel='stylesheet' href='https://lib.baomitu.com/nprogress/0.2.0/nprogress.css'/>
控制显示,下面这两句代码,分别放到页面开头和网页加载完成事件里面即可。
NProgress.start();//开始加载进度条
NProgress.done();//停止显示进度条
控制进度条的速度
NProgress.set(0.0); // Sorta same as .start()
NProgress.set(0.4);
NProgress.set(1.0); // Sorta same as .done()