使用jquery 实现进度条

进度条的实现

学习自: 这个网站
原理:

在 一个页面中一般分为:header,content,sidebar,footer,每次我们访问页面时,浏览器发送请求,肯定要花费一定的时间的,以进度条的宽度来实现加载时间的变化
在header时,让进度条显示宽度,10%,
加载content时,让进度条显示一定的宽度,50%,
sidebar 70%,
footer 100%
就是这样,动画函数使用jquery的animate(),
最后,页面加载完成,使用fadeout()淡出 让进度条消失就可以了.
html 代码:

<!DOCTYPE html>    
<html lang="en">   
 <head>        
<meta charset="UTF-8">        
<title>进度条</title>    
</head>    
<body>        
     <div class="loading"></div>
     <header>header</header>
    <div class="loading"></div>
    <div class="content">some data</div>
    <div class="loading"></div>
     <div class="sidebar"></div>
     <div class="loading"></div>
     <footer><footer>
</body>      
</html> 
然后给每个loading div 加样式
.loading { 
     background: #FF6100;
      height: 5px;
      position: fixed;
      top: 0;      
      z-index: 99999;}
引入 jquery
<script src="jquery-2.1.4.min.js"></script>
加上 jquery代码
$ (".loading").animate ({"width": "10%"}, 50);   
$ (".loading").animate ({"width": "50%"}, 50);   
$ (".loading").animate ({"width": "70%"}, 50);   
$ (".loading").animate ({"width": "100%"}, 50);   
$ (function () {
      $(".loading").fadeOut();  
 });
完整代码
<!DOCTYPE html>
<html lang="en">
<head>   
<meta charset="UTF-8">   
<title>进度条</title>
   <style>
      .loading { 
        background: #FF6100;
         height: 5px;
         position: fixed;
         top: 0;
         z-index: 99999;
      }   
</style>
</head>
<body>
  <div class="loading"></div>
  <header>header</header>
  <div class="loading"></div>
  <div class="content">some data</div>
  <div class="loading"></div>
  <div class="sidebar"></div>
  <div class="loading"></div>
  <footer><footer>
  <script src="jquery.js"></script>
  <script>   
  $ (".loading").animate ({"width": "10%"}, 50);  
  $ (".loading").animate ({"width": "50%"}, 50);   
  $ (".loading").animate ({"width": "70%"}, 50);   
  $ (".loading").animate ({"width": "100%"}, 50);   
  $ (function () {
      $(".loading").fadeOut();
   });
  </script>
  </body>
</html>
最后,一个进度条就完成了,
其实 理解了之后很简单
只要知道 html 页面的每个部分都是要 时间加载的,在每个页面部分加载完成之前与之后用一个宽度表示就行了,最后页面加载完成 让进度条消失即可
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,264评论 25 708
  • 笔记参考自《响应式Web设计:HTML5和CSS3实践》,2013年出版内容说不上最新。如下是全书的章的目录:第 ...
    于晓鱼阅读 949评论 0 1
  • 在线阅读 http://interview.poetries.top[http://interview.poetr...
    前端进阶之旅阅读 114,773评论 24 450
  • 彩虹手帐,第一天更新,开启!! 前世今生 Q1:彩虹手帐的日更计划是怎么来的呢?A1: 想法源自于老大昨天分享的价...
    碧落云湮阅读 672评论 1 2