js实现自定义进度条效果


Posted in Javascript onMarch 15, 2017

效果图:

js实现自定义进度条效果

代码如下:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>Staged progress bar</title>
  <style type="text/css">
   *{margin:0;padding:0;}
   html,body{height:100%;}
   ul{list-style:none;}
   .cf:after{content:"";display:block;clear:both;height: 0;}
   #bar{height:20px;margin:100px 10px; margin-left: 50px}
   #bar div{float:left;position:relative;}
   #bar .staged, #bar .progress{border-color:#4CA8FF;}
   #bar .staged i{position:relative;overflow:hidden;display:block;width:inherit;height:inherit;}
   #bar .staged i:before{content:"";display:block;width:0;height:inherit;border-radius:50%;}
   #bar .progress {width:120px;height:inherit;margin-left:-4px;z-index:1;border-style:solid;border-width:2px 0;}
   #bar .progress:nth-child(2) i{border-left-width:2px;border-top-left-radius:6px;border-bottom-left-radius:6px;}
   #bar .progress:nth-child(2){border-left-width:2px;border-top-left-radius:10px;border-bottom-left-radius:10px;}
   #bar .progress:last-child{border-right-width:2px;border-top-right-radius:10px;border-bottom-right-radius:10px;}
   #bar .progress i{width:0;height:inherit;display:block;border-radius:0 10px 10px 0;position:relative;font-style:normal;}
   #bar .progress, #bar .staged{background:#7d7d7d;}
   #bar .sp i:before, #bar .progress i{background:#4CA8FF;}
   #bar .staged:not(:first-child){margin-left:-4px; }
   #bar .staged:before{position:absolute;content:attr(data-text);width:inherit;height:inherit;top:90%;left:0;}
   #bar .staged:after{content:attr(data-value);color:#fff;width:inherit;height:inherit;position:absolute;top:0;left:0;}
   #bar .sp i:before{width:100%;transition:width 1s;}
   .msg:before, .msg:after{display:block;position:absolute;border-style:solid;border-color:#ccc;background:#fff;top:30px;}
   .msg:before{content:attr(data-text);width:200px;height:40px;line-height:40px;font-size:12px;text-align:center;border-radius:4px;border-width:1px;top:-68px;left:calc(100% - 114px);}
   .msg:after{content:"";width:14px;height:14px;transform:rotate(45deg);border-width:0 1px 1px 0;top:-34px;left:calc(100% - 20px);}
  </style>
 </head>
 <body>
  <div id="bar" class="cf" data-value="8000" data-text="还有 [number] 点进度加载完成">
   <div class="staged sp" data-value="0" ><i></i></div>
   <div class="progress"><i></i></div>
   <div class="staged" data-value="200" "><i></i></div>
   <div class="progress"><i></i></div>
   <div class="staged" data-value="1000" ><i></i></div>
   <div class="progress"><i></i></div>
   <div class="staged" data-value="5000" ><i></i></div>
   <div class="progress"><i></i></div>
   <div class="staged" data-value="10000" ><i></i></div>
   <div class="progress"><i></i></div>
  </div>
  <script type="text/javascript">
   var progressBar = (function(){
    var $ = function(sele){
     return document.querySelectorAll(sele);
    }
    return function(a){
     var getValue = function(obj,attr){
      return Number(obj.getAttribute(a.value));
     }
     var box = $(a.box)[0];
     var value = getValue(box);
     var text = box.getAttribute(a.text);
     var staged = $(a.staged);
     var progress = $(a.progress);
     var i = 0, index = 0, num = 0, numV = 0, n = 1, af_index = 0, timeout = 0, stopAm = null;
     var af = function(fn){
      if(!requestAnimationFrame || !cancelAnimationFrame){
       clearTimeout(af_index);
       af_index = setTimeout(fn, 1000/60);
      }
      else{
       cancelAnimationFrame(af_index);
       af_index = requestAnimationFrame(fn);
      }
     }
     var setMsg = function(){
      var msg = progress[index];
      if(a.note){
        var arr = text.split(a.note);
        text = arr[0] + n + arr[1];
      }
      msg.setAttribute("data-text", text);
      msg.className = a.msg;
     }
     var animation = function(){
      if(i == 0){
       if(staged.length == index + 1){
        numV = value * 2;
       }
       else numV = getValue(staged[index + 1]);
       if(n == 0) {
        progress[index].style.width = "1%";
        return
       };
       if(n < 0 && staged.length > index + 1){
        n = numV - value;
        if(Number(progress[index].style.width.split("%")[0])<5) progress[index].style.width = "3%";
        setMsg();
        return;
       }
      }
      num = Math.ceil(++i * numV / 100);
      if(num > value){
       num = value;
       //console.log(num)
       n = numV - num;
       if(n > 0){
        if(staged.length > index + 1) setMsg();
        return;
       }
      }
      if(i == 100){
       staged[index + 1].className += " " + a.stagedProgress;
      }
      if(i > 105){ //105% 宽,自己根据样式调整。
       num = numV;
       index++;
       i = 0;
       timeout = setTimeout(function(){
        af(animation);
        clearTimeout(timeout);
       }, a.stagedSleep);
       return;
      }
      progress[index].style.width = i + "%";
      af(animation);
     }
     animation()
    }
   })();
   progressBar({
    box : "#bar",
    text : "data-text", // box 的属性
    note : "[number]", // box 的属性 (可以省略,计算好直接写入提示语句。);
    msg : "msg",
    staged : ".staged",
    progress : ".progress i",
    stagedProgress : "sp",
    value : "data-value",
    stagedSleep : "900",
    sleep : "3000"
   });
  </script>
 </body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
JavaScript函数获取事件源的小例子
May 14 Javascript
JS遍历Json字符串中键值对先转成JSON对象再遍历
Aug 15 Javascript
微信js-sdk预览图片接口及从拍照或手机相册中选图接口用法示例
Oct 13 Javascript
微信小程序实战之仿android fragment可滑动底部导航栏(4)
Apr 16 Javascript
Vue resource中的GET与POST请求的实例代码
Jul 21 Javascript
vue+axios新手实践实现登陆的示例代码
Jun 06 Javascript
微信小程序视图容器(swiper)组件创建轮播图
Jun 19 Javascript
详解vue 项目白屏解决方案
Oct 31 Javascript
node.js处理前端提交的GET请求
Aug 30 Javascript
微信小程序indexOf的替换方法(推荐)
Jan 14 Javascript
详解vue中在循环中使用@mouseenter 和 @mouseleave事件闪烁问题解决方法
Apr 07 Javascript
react的hooks的用法详解
Oct 12 Javascript
yii form 表单提交之前JS在提交按钮的验证方法
Mar 15 #Javascript
jQuery模拟窗口抖动效果
Mar 15 #Javascript
利用jQuery实现一个简单的表格上下翻页效果
Mar 14 #Javascript
基于React实现表单数据的添加和删除详解
Mar 14 #Javascript
Node.js 中exports 和 module.exports 的区别
Mar 14 #Javascript
JS检测数组类型的方法小结
Mar 14 #Javascript
轻松理解JavaScript闭包
Mar 14 #Javascript
You might like
php对mongodb的扩展(初出茅庐)
2012/11/11 PHP
Codeigniter中mkdir创建目录遇到权限问题和解决方法
2014/07/25 PHP
jQuery 1.5.1 发布,全面支持IE9 修复大量bug
2011/02/26 Javascript
js动态切换图片的方法
2015/01/20 Javascript
js随机生成字母数字组合的字符串 随机动画数字
2015/09/02 Javascript
jQuery实现页面顶部显示的进度条效果完整实例
2015/12/09 Javascript
JavaScript几种数组去掉重复值的方法推荐
2016/04/12 Javascript
jQuery 获取屏幕高度、宽度的简单实现案例
2016/05/17 Javascript
AngularJs  unit-testing(单元测试)详解
2016/09/02 Javascript
JavaScript学习笔记--常用的互动方法
2016/12/07 Javascript
纯JS实现表单验证实例
2016/12/24 Javascript
JS实现iframe自适应高度的方法示例
2017/01/07 Javascript
有关JS中的0,null,undefined,[],{},'''''''',false之间的关系
2017/02/14 Javascript
Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容
2017/06/17 Javascript
微信JS-SDK实现微信会员卡功能(给用户微信卡包里发送会员卡)
2019/07/25 Javascript
Vue 请求传公共参数的操作
2020/07/31 Javascript
Python脚本实现代码行数统计代码分享
2015/03/10 Python
Python3.2中的字符串函数学习总结
2015/04/23 Python
Python的Django框架中URLconf相关的一些技巧整理
2015/07/18 Python
浅谈关于Python3中venv虚拟环境
2018/08/01 Python
Opencv+Python 色彩通道拆分及合并的示例
2018/12/08 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
如何使用Python实现自动化水军评论
2019/06/26 Python
HTML5是什么 HTML5是什么意思 HTML5简介
2012/10/26 HTML / CSS
美国豪华时尚女性精品店:Kirna Zabête
2018/01/11 全球购物
美国流行背包品牌:JanSport(杰斯伯)
2018/03/02 全球购物
戴尔英国翻新电脑和电子产品:Dell UK Refurbished Computers
2019/07/30 全球购物
绘儿乐产品官方在线商店:Crayola.com
2019/09/07 全球购物
客服服务心得体会
2013/12/30 职场文书
文秘个人求职信范文
2014/04/22 职场文书
学生夜不归宿检讨书
2014/09/23 职场文书
2019年教师入党申请书
2019/06/27 职场文书
PHP控制循环操作的时间
2021/04/01 PHP
详解Redis瘦身指南
2021/05/26 Redis
Python趣味爬虫之用Python实现智慧校园一键评教
2021/05/28 Python
redis实现的四种常见限流策略
2021/06/18 Redis