JavaScript implements circular progress bar effect

JavaScript implements circular progress bar effect

This article example shares the specific code of JavaScript to achieve the circular progress bar effect for your reference. The specific content is as follows

<!DOCTYPE html>
<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>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <style>
    .itemwait{
      position:absolute;
      top : 0;
      bottom:0;
      left:0;
      right:0;
      margin: 0 auto; 
    }
    .progress{
      stroke-dasharray: 251; 
      stroke-dashoffset: 0;
      /* 
      stroke-dasharray: dashed line stroke-dashoffset: offset interval*/
    }
  </style>
</head>
<body>
  <svg width="200" height="200" version="1.1" class='itemwait'>
    <circle class='progress' cx="100" cy="50" r="40" stroke="pink" stroke-width="5" fill="transparent" />
    <text class='text' x="100" y="50" fill='pink' text-anchor='middle' alignment-baseline='middle'>Start loading</text>
  </svg>
</body>
<script async type='text/javascript'>
//js code see below</script>

</html>

1. Native js implementation

const loadingArr = [1,2,10,20,40,70,90,100]
  let index=0
  var timer = setInterval(()=>{
    let total = document.querySelector('.progress').getTotalLength();
    let progress = document.querySelector('.progress')
    console.log(typeof total)
    progress.style.strokeDashoffset = (total) * (1-loadingArr[index]/100)
    if(index<=loadingArr.length){
      document.querySelector('.text').textContent=`${loadingArr[index]}%`   
  }
    index++
    if(index===loadingArr.length){
      clearInterval(timer)
      document.querySelector('.text').textContent='Loading completed'
    }
  },500)

2. jQuery Implementation

let index = 0
  var $circle = $('.progress');
  var r = $circle.attr('r');
  var timer = setInterval(() => {
    var total = Math.PI * (r * 2);
    var pct = (1-index / 100) * total;
    console.log(typeof pct,pct)
    if (index <= 100) {
      $('.text').text(`${index}%`);
      $circle.css({ strokeDashoffset: pct });
    }
    index = index + 10
    if (index > 100) {
      $('.text').text('Loading completed');
      clearInterval(timer)
      
    }
  }, 500)

3. Initially implement it according to your own ideas

const loadingArr = [1,2,10,20,40,70,90,100]
  let index=0
  var timer = setInterval(()=>{
    let total = document.querySelector('.progress').getTotalLength();
    let progress = document.querySelector('.progress')
    console.log(typeof total)
    progress.style.strokeDashoffset = (total) * (1-loadingArr[index]/100)
    $('.text').text(function(){
      if(index<=loadingArr.length){
       return `${loadingArr[index]}%`
      }
    })
    index++
    if(index===loadingArr.length){
      clearInterval(timer)
      $('.text').text('Loading completed')
    }
  },500)

Summarize

Knowledge points: svg drawing, js native operation, jQuery

  • stroke-dasharray: dashed line
  • stroke-dashoffset: offset interval

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Several methods of javascript progress bar
  • How to implement progress bar in js
  • JavaScript to achieve web page loading progress bar code is super simple
  • js progress bar implementation code
  • JS progress bar effect implementation code organization
  • JS realizes the effect of circular progress bar (from 0 to 100%)
  • Simple progress bar control written in Javascript jquery css
  • Progress bar effect implemented with CSS+JS
  • js realizes audio control progress bar function
  • Using Session with Javascript in PHP to implement file upload progress bar function

<<:  MySQL query specifies that the field is not a number and comma sql

>>:  Common usage of regular expressions in Mysql

Recommend

Detailed explanation of Vue's live broadcast function

Recently, the company happened to be doing live b...

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

Markup language - specify CSS styles for text

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...

MySQL database operations (create, select, delete)

MySQL Create Database After logging into the MySQ...

Vue encapsulates the public function method of exporting Excel data

vue+element UI encapsulates a public function to ...

Summary of things to pay attention to in the footer of a web page

Lots of links You’ve no doubt seen a lot of sites ...

How to try to add sticky effect to your CSS

Written in front I don’t know who first discovere...

Incredible CSS navigation bar underline following effect

The first cutter in China github.com/chokcoco Fir...

Install nvidia graphics driver under Ubuntu (simple installation method)

Install the nvidia graphics card driver under Ubu...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

CocosCreator general framework design resource management

Table of contents Problems with resource manageme...

15 important variables you must know about MySQL performance tuning (summary)

Preface: MYSQL should be the most popular WEB bac...