先展示一下已经实现的效果:

预览地址: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); }
}

转载于:https://www.cnblogs.com/dtdxrk/p/4165280.html

【原生JS插件】LoadingBar页面顶部加载进度条相关推荐

  1. nprogress 插件 网页顶部加载进度条

    文章目录 效果(看顶部) 安装 使用 1.基本用法 2.高级用法 效果(看顶部) 安装 CSDN地址(我设置的免积分,推荐下载):https://download.csdn.net/download/ ...

  2. 仿YouToBe给页面顶部加一个进度条

    http://jsfiddle.net/42NYX/ 首先是css部分 我们需要一个div,也就是进度条的div div {position: absolute;top: 0px;left: 0px; ...

  3. Hexo博客NexT主题美化之顶部加载进度条

    前言 更多效果展示,请访问我的 个人博客. 效果图: 教程 进入博客文件夹的/themes/next文件夹下 cd /themes/next 复制代码 下载安装Progress module,如下 g ...

  4. js网页顶部线性页面加载进度条,jquery头部线性进度条总结

    前言 网页顶部加载进度条,近年来很流行,很多网站都采用了这种加载方式.网上也有这样类似的插件,今天我们总结一下网页顶部线性页面加载进度条. 头部LoadingBar线性进度条总结 上面的代码只是静态效 ...

  5. pace.js – 加载进度条插件

    这儿只是简单介绍一下这个插件pace.js. 在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况.此插件的兼容性很好,可以兼容 ...

  6. 浅谈前端实现页面加载进度条以及 nprogress.js 的实现

    以前在 Vue 的项目用了 nprogress 这个插件,一直对于其如何得知加载进度充满好奇,最近又看到了「前端如何实现页面加载进度条」这个问题,今天周六恰好一探究竟.以下仅为一家之言,如有异议,欢迎 ...

  7. 原生JS实现加载进度条

    分享一个原生JS实现的动态加载进度条特效,效果如下: 实现的代码如下: <!DOCTYPE html> <html><head><meta http-equi ...

  8. pace.js网页自动加载进度条插件-好东西

    pace.js – 网页自动加载进度条插件 源码地址 https://github.com/HubSpot/pace 入Pace.js以及主题文件 Pace.js公开的API列表, Pace.star ...

  9. NProgress.js - 前端全站进度条插件 - 给你的网站添加一个加载进度条

    0x00 前言 前几天给博客换了@Veen Zhao大佬的Cuteen主题,非常好看,但是因为不想让自己的博客和其他人的千篇一律,于是决定在Cuteen主题的前提下逐渐设计一些自己需要的东西.正巧前几 ...

最新文章

  1. 零基础学Java需要做哪些准备
  2. 关于Android H5混合开发遇到的问题
  3. poj1426(dfs)
  4. 一、后台首页index.php【dedecms后台源码分析】
  5. Matrix Subtraction(小米icpc邀请赛第一场)
  6. 自考计算机非笔试英语怎么考,自考中的非笔试课程是什么,怎么进行考核?
  7. 决策树(八)--随机森林及OpenCV源码分析
  8. SQLMap使用总结
  9. 高可用的分布式Hadoop大数据平台搭建,超详细,附代码。
  10. 『软件工程10』结构化系统分析:数据流图和字典案例分析
  11. 外贸公司申请一个企业邮箱,国外邮箱大全对比
  12. python爬取堆糖网每日精选图片
  13. struct tm 和 time_t 时间和日期的使用方法
  14. C#生成格林威治时间字符串
  15. android脚本模拟器,android运行模拟器脚本(批处理)
  16. 西邮Linux兴趣小组2017纳新免试题揭秘
  17. 苹果将于4月20日举行产品发布会
  18. eos节点服务器_eos区块链php开发包
  19. php7.1 phpize编译gd,centos 7 下用 phpize安装GD扩展库
  20. 计算机辅助测试普通话考试流程,必看!普通话考试全流程详解!

热门文章

  1. shutdown小程序
  2. 基于stm32f429的手写识别_关注智能手机老年用户:百度输入法手写模型迎来重磅升级...
  3. c语言最简单程序实例,C语言第一个简单实例
  4. 打开方式中选择默认方式无反映_「Windows」得看,更改文件的默认应用,告别“打开方式”...
  5. php 如何调用redis,php如何调用redis
  6. LeetCode 面试题57 - II(剑指offer) 和为s的连续正数序列
  7. GPU Gems2 - 7 带位移映射的细分表面自适应镶嵌
  8. C语言 · 前10名
  9. 【XLL 框架库函数】 TempActiveCell/TempActiveCell12
  10. UESTC_秋实大哥与花 2015 UESTC Training for Data StructuresProblem B