欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现滑动进度条效果

程序员文章站 2022-07-24 16:00:42
本文实例为大家分享了js实现滑动进度条效果的具体代码,供大家参考,具体内容如下进度条:

本文实例为大家分享了js实现滑动进度条效果的具体代码,供大家参考,具体内容如下

js实现滑动进度条效果

进度条:

<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title>js滑动进度条效果</title>
 <style>
  *{margin:0;padding:0;user-select:none;}
  .progress-bar{position:relative;height:10px;width:400px;margin:200px auto;background:#ebeef5;border-radius:10px;}
  .progress-bar .pro-bar{position:absolute;left:0;height:10px;width:10px;background:#409eff;}
  .progress-bar .min-num{position:absolute;left:-20px;top:-5px;}
  .progress-bar .max-num{position:absolute;right:-40px;top:-5px;}
  .progress-bar .block {position:absolute;height:30px;width:10px;background:#ccc;top:-10px;border-radius:10px;}
  .progress-bar .block div{position:absolute;display:none;left:-20px;top:-45px;width:50px;height:30px;text-align:center;line-height:30px;background:#ccc;border-radius:20px;}
  .progress-bar .block:hover div{display:block;font-size:10%;color:#fff;background:#409eff;}
 </style>
</head>
<body>
 <div class="progress-bar">
 <span class="min-num">0</span>
 <span class="max-num">100</span>
 <div class="pro-bar"></div>
 <div class="block">
  <div>0</div>
 </div>
 </div>
</body>
<script>
 (function(){
 let moveblock = document.queryselector('.block');
 let probar = document.queryselector('.pro-bar');
 let flag = false;
 let startx = null;
 let movemax = (400 - 10); // 背景条宽度减去滑块的宽度
 moveblock.onmousedown = function(e){
  startx = e.pagex;
  moveblock.style.left ? moveblock.style.left : moveblock.style.left = '0px';
  let startleft = parseint(moveblock.style.left);
  document.onmousemove = function(e){
  let movex = (e.pagex - startx) > 0 ? true : false;
  let movesection = startleft + (e.pagex - startx);
  // 限定移动范围
  if (movesection >= 0 && movesection <= movemax) {
   let percent = ((startleft + (e.pagex - startx)) / movemax).tofixed(4) * 100;
   percent.tostring().length > 5 ? percent = percent.tostring().substr(0, 5) : percent = percent.tostring();
   moveblock.style.left = startleft + (e.pagex - startx) + 'px';
   probar.style.width = moveblock.style.left;
   moveblock.queryselector('div').innertext = percent + '%';
  }
  };
 };
 // 鼠标松开移除事件
 moveblock.onmouseup = function(){
  document.onmousemove = null;
 };
 })();
</script>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。