【原生JS插件】LoadingBar页面顶部加载进度条
先展示一下已经实现的效果:
预览地址:http://dtdxrk.github.io/js-plug/LoadingBar/index.html

看到手机上的浏览器内置了页面的加载进度条,想用在pc上。
网上搜了一下,看到几种页面loading的方法:
1.在body头部加入loading元素,在body页脚写入脚本让loading元素消失。
2.基于jquery,在页面的不同位置插入脚本,设置滚动条的宽度。
简单分析一下:
第一个明显不是我想要的。
第二个要在body前加载jquery,然后还要使用到jquery的动画方法,性能肯定达不到最优的状态。
自己的解决方法:原生JS+css3
上面的方法2其实是可以使用的方法,但是我不想在页面前加载jquery,怎么办?
很简单,自己用原生的方法写一个。
给元素添加css3的动画属性,让他能在改变宽度的时候有动画效果。
transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s;
在页面插入一段style,里面有元素的css和一个css3动画暂停的类
.animation_paused{
-webkit-animation-play-state:paused;
-moz-animation-play-state:paused;
-ms-animation-play-state:paused;
animation-play-state:paused;
}
然后在页面里不同的位置调用方法,设置滚动条的宽度即可,需要注意的是方法的引用要在<head></head>里
<div id="top"></div>
<script>LoadingBar.setWidth(1)</script> <div id="nav"></div>
<script>LoadingBar.setWidth(20)</script> <div id="banner"></div>
<script>LoadingBar.setWidth(40)</script> <div id="main"></div>
<script>LoadingBar.setWidth(60)</script> <div id="right"></div>
<script>LoadingBar.setWidth(90)</script> <div id="foot"></div>
<script>LoadingBar.setWidth(100)</script>
插件源码:
/*
========================================================================
LoadingBar 页面加载进度条
@auther LiuMing
@blog http://www.cnblogs.com/dtdxrk
demo 在body里填写需要加载的进度
LoadingBar.setWidth(number)
========================================================================
*/
var LoadingBar = {
/*初始化*/
init:function(){
this.creatStyle();
this.creatLoadDiv();
}, /*记录当前宽度*/
width:0, /*页面里LoadingBar div*/
oLoadDiv : false, /*开始*/
setWidth : function(w){
if(!this.oLoadDiv){this.init();}
var oLoadDiv = this.oLoadDiv,
width = Number(w) || 100;
/*防止后面写入的width小于前面写入的width*/
(width<this.width) ? width=this.width : this.width = width;
oLoadDiv.className = 'animation_paused';
oLoadDiv.style.width = width + '%';
oLoadDiv.className = ''; if(width === 100){this.over(oLoadDiv);}
}, /*页面加载完毕*/
over : function(obj){
setTimeout(function(){
obj.style.display = 'none';
},1000);
}, /*创建load条*/
creatLoadDiv : function(){
var div = document.createElement('div');
div.id = 'LoadingBar';
document.body.appendChild(div);
this.oLoadDiv = document.getElementById('LoadingBar');
}, /*创建style*/
creatStyle : function(){
var nod = document.createElement('style'),
str = '#LoadingBar{transition:all 1s;-moz-transition:all 1s;-webkit-transition:all 1s;-o-transition:all 1s;background-color:#f90;height: 3px;width:0; position: fixed;top: 0;z-index: 99999;left: 0;font-size: 0;z-index:9999999;_position:absolute;_top:expression(eval(document.documentElement.scrollTop));}.animation_paused{-webkit-animation-play-state:paused;-moz-animation-play-state:paused;-ms-animation-play-state:paused;animation-play-state:paused;};'
nod.type = 'text/css';
//ie下
nod.styleSheet ? nod.styleSheet.cssText = str : nod.innerHTML = str;
document.getElementsByTagName('head')[0].appendChild(nod);
}
}
【原生JS插件】LoadingBar页面顶部加载进度条的更多相关文章
- react封装简单的浏览器顶部加载进度条全局组件
在项目中经常会有在请求前后加loading或者加加载进度条,一般这些组件都会抽离出来作为全局组件 进度条的插件貌似都不是很符合自己项目中的需求,于是.. 参考nprogress样式,自己在项目中封装组 ...
- js实现页面图片加载进度条
//html <div id="loading" class="loading"> <div class="load"&g ...
- pace.js – 加载进度条插件
这儿只是简单介绍一下这个插件pace.js. 在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况.此插件的兼容性很好,可以兼容 ...
- pace.js – 网页自动加载进度条插件
网站顶部的页面加载进度条是怎么实现的,页面的加载进度百分比,有时候获取是比较麻烦的,当然也可以利用一些优秀的JavaScript插件来实现,今天就为大家介绍这样子的一款插件:pace.js. [官方网 ...
- js和jquery页面初始化加载函数的方法及先后顺序
运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...
- js页面加载进度条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的
js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样 ...
- 网站顶部显示预加载进度条preload.js
网站加载的速度快的话,不会显示进度条加载时候的样式. 支持性主流浏览器都支持,ie浏览器需要9以上9也支持. 使用方法 <script src="http://code.jquery. ...
- ajax页面加载进度条插件
下面两个都是youtube视频的加载进度条效果的ajax插件 一.官网:http://ricostacruz.com/nprogress/官网 github:https://github.com/rs ...
随机推荐
- 洛谷 P1373 小a和uim之大逃离 题解
每日一题 day30 打卡 Analysis f[i][j][p][q]表示他们走到(i,j),且两人魔瓶内魔液量的差为p时的方法数.q=0表示最后一步是小a走的,q=1表示最后一步是uim走的.题目 ...
- TPC-H 下载参考
CSDN免费下载地址 tpc-h-v2.17.3.zip tools https://download.csdn.net/download/zes2014/10558251
- learning java AWT Dialog
import java.awt.*; public class DialogTest { Frame f = new Frame("test"); Dialog d1 = new ...
- noi.ac #36 模拟
\(des\) 存在 \(1000 \times 1000\) 的矩阵,保证元素互不相同,\(2e5\) 次询问,每次询 问给定 \(x, y\) 问存在多少点 \((a, b)\) 满足该元素是 \ ...
- word 插入表格,位置不在最左边
首先想到的是 样式的问题.
- SSM 整合 ehcache 报错
异常: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springfra ...
- Prometheus告警规则增删改自动化
Prometheus告警规则增删改自动化 前言: 随着容器技术的发展,zabbix监控方式与k8s的结合不完善,导致不得不放弃zabbix,而新的监控工具prometheus的使用就越来越多了.但是经 ...
- P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)
题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...
- 线程池(3)-参数-实现ThreadFactory
1.介绍 ThreadFactory用来创建线程,需要实现newThread方法. 2.常用场景 线程重命名 设置守护进程 设置优先级 3.示例(线程重命名) public class ThreadF ...
- JavaScript substr() 方法
定义和用法 substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符. 语法 stringObject.substr(start,length) 参数 描述 start 必需.要 ...