How to make a progress bar

后端 未结 19 1535
失恋的感觉
失恋的感觉 2020-12-04 18:09

How would one go about making a progress bar in html/css/javascript. I don\'t really want to use Flash. Something along the lines of what can be found here: http://dustincur

19条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 18:45

    Though one can build a progress bar using setInterval and animating its width

    For best performance while animating a progress bar one has to take into account compositor only properties and manage layer count.

    Here is an example:

    function update(e){
      var left = e.currentTarget.offsetLeft;
      var width = e.currentTarget.offsetWidth
      var position = Math.floor((e.pageX - left) / width * 100) + 1;
      var bar = e.currentTarget.querySelector(".progress-bar__bar");
      bar.style.transform = 'translateX(' + position + '%)';
    }
    body {
      padding: 2em;
    }
    
    .progress-bar {
      cursor: pointer;
      margin-bottom: 10px;
      user-select: none;
    }
    
    .progress-bar {
      background-color: #669900;
      border-radius: 4px;
      box-shadow: inset 0 0.5em 0.5em rgba(0, 0, 0, 0.05);
      height: 20px;
      margin: 10px;
      overflow: hidden;
      position: relative;
      transform: translateZ(0);
      width: 100%;
    }
    
    .progress-bar__bar {
      background-color: #ececec;
      box-shadow: inset 0 0.5em 0.5em rgba(0, 0, 0, 0.05);
      bottom: 0;
      left: 0;
      position: absolute;
      right: 0;
      top: 0;
      transition: all 500ms ease-out;
    }
    Click on progress bar to update value
    
    

提交回复
热议问题