原生js实现进度条

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }

    .app {
      width: 1200px;
      height: 50px;
      background-color: lawngreen;
      margin: 25% auto;
      display: flex;
      align-items: center;
      position: relative;
    }
    .precess {
      width: 100%;
      height: 30px;
      border-radius: 15px;
      background-color: #ebeef5 ;
    }
    .left {
      height: 30px;
      width: 0;
      border-radius: 15px;
      background-color: #f56c6c;
    }
    .bar {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: #f56c6c;
      position: absolute;
      left: 0;
      top: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      transform: translateX(-50%);
    }
    .bar-cli {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: white;
    }
  </style>
</head>
<body>
  <div class="app">
    <div class="precess">
      <div class="left"></div>
    </div>
    <div class="bar">
      <div class="bar-cli"></div>
    </div>
  </div>
  <script>
    let oPrecess = document.getElementsByClassName('precess')[0]
    let oBar = document.getElementsByClassName('bar')[0]
    let oLeft = document.getElementsByClassName('left')[0]
    let precessWidth = oPrecess.offsetWidth
    let setp = precessWidth / 100
    let i = 0
    let startX = 0
    let moveX = 0
    let timer = setInterval(() => {
      mMoveBarX()
     }, 1000);
    oBar.onmousedown = function (e) {
      clearInterval(timer)
      startX = e.clientX - oBar.offsetLeft
      document.onmousemove = function(e) {
        moveX = e.clientX - startX
        if(moveX >= precessWidth) {
          moveX = precessWidth
        } else if(moveX <= 0){
          moveX = 0
        }
        moveBarX (moveX)
      }
      document.onmouseup = function () {
        document.onmousemove = null
        startMMove()
      }
    }
    oPrecess.onmousedown = function(e) {
      clearInterval(timer)
      moveX = e.offsetX
      moveBarX(moveX)
    }

    oPrecess.onmouseup = function() {
      startMMove()
    }
    
  function startMMove() {
    timer = setInterval(() => {
          mMoveBarX()
      }, 1000);
  }

  function mMoveBarX() {
      if(moveX < precessWidth) {
        moveX = (precessWidth / 100) + moveX
        if(moveX > precessWidth) {
          moveX = precessWidth
        } 
        moveBarX(moveX)
    } else {
      clearInterval(timer)
    }
  }

  function moveBarX (moveX) {
    oBar.style.left = moveX + 'px' 
    oLeft.style.width = oBar.style.left
  }
  </script>
</body>
</html>```
posted @   coderlq  阅读(219)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示