Progress bar plugin

In this example below you will see how to do a Progress bar plugin with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by peternguyen, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright peternguyen ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Progress bar plugin</title>
  


    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">

  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="wrapper">
  <div class="container">
    <div data-init="countTo" data-count-to-duration="3000">
      <div class="row">
        <div class="col-md-3">
          <div class="count">
            <div class="count__label" data-count-to-number="70">0</div>
            <div class="count__line"><span></span></div>
          </div>
        </div>
        <div class="col-md-3">
          <div class="count">
            <div class="count__label" data-count-to-number="100">0</div>
            <div class="count__line"><span></span></div>
          </div>
        </div>
        <div class="col-md-3">
          <div class="count">
            <div class="count__label" data-count-to-number="30">0</div>
            <div class="count__line"><span></span></div>
          </div>
        </div>
        <div class="col-md-3">
          <div class="count">
            <div class="count__label" data-count-to-number="50">0</div>
            <div class="count__line"><span></span></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/peternguyen/progress-bar-plugin-vGKna */
.count {
  padding: 30px 0;
}

.count__label {
  font-size: 48px;
  font-weight: 500;
}

.count__line {
  position: relative;
  background-color: #d0d0d0;
  height: 4px;
}
.count__line span {
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  background-color: red;
}


/*Downloaded from https://www.codeseek.co/peternguyen/progress-bar-plugin-vGKna */
$.fn.countTo = function() {
  return this.each(function() {
    var $init = $(this);
    var $item = $init.find('.count');
    var _duration = 10000;
    
    if($init.data('count-to-duration') != undefined) {
      if($init.data('count-to-duration').length !== 0) {
        _duration = $init.data('count-to-duration');
      }
    }
    
    $item.each(function() {
      let el = $(this);
      let label = el.find('.count__label');
      let line = el.find('.count__line');
      
      let _number = label.data('count-to-number');
      let _rate = _duration / 100;
      
      let _val = label.text();
      
      let runTimes = setInterval(() => {
        if(_val === _number) {
          clearInterval(runTimes);
          return false;
        };
        
        _val++;
        label.text(_val + '%');
        line.find('span').css('width', _val + '%');
      }, _rate);
    });
  });
};

$('[data-init="countTo"]').countTo();

Comments