>  기사  >  웹 프론트엔드  >  js는 슬라이딩 진행률 표시줄을 구현합니다.

js는 슬라이딩 진행률 표시줄을 구현합니다.

coldplay.xixi
coldplay.xixi앞으로
2020-08-21 17:33:282803검색

js는 슬라이딩 진행률 표시줄을 구현합니다.

【관련 학습 권장 사항: javascript 비디오 튜토리얼

이 기사의 예는 참고용으로 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 p{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 p{display:block;font-size:10%;color:#fff;background:#409eff;}
 </style>
</head>
<body>
 <p class="progress-bar">
 <span class="min-num">0</span>
 <span class="max-num">100</span>
 <p class="pro-bar"></p>
 <p class="block">
  <p>0</p>
 </p>
 </p>
</body>
<script>
 (function(){
 let moveBlock = document.querySelector(&#39;.block&#39;);
 let proBar = document.querySelector(&#39;.pro-bar&#39;);
 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 = &#39;0px&#39;;
  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) + &#39;px&#39;;
   proBar.style.width = moveBlock.style.left;
   moveBlock.querySelector(&#39;p&#39;).innerText = percent + &#39;%&#39;;
  }
  };
 };
 // 鼠标松开移除事件
 moveBlock.onmouseup = function(){
  document.onmousemove = null;
 };
 })();
</script>
</html>

추천 사진 및 텍스트: js 튜토리얼(사진 및 텍스트)

위 내용은 js는 슬라이딩 진행률 표시줄을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 jb51.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제