欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)圓形進(jìn)度條效果

 更新時(shí)間:2021年08月25日 09:24:43   作者:_he_dan_  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)圓形進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)圓形進(jìn)度條效果的具體代碼,供大家參考,具體內(nèi)容如下

<!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: 虛線
      stroke-dashoffset: 偏移間隔   
      */
    }
  </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'>開始加載</text>
  </svg>
</body>
<script async type='text/javascript'>
//js代碼見下文
</script>

</html>

1.原生js實(shí)現(xiàn)

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='加載完成'
    }
  },500)

2.jQuery實(shí)現(xiàn)

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('加載完成');
      clearInterval(timer)
      
    }
  }, 500)

3.最初按照自己的想法實(shí)現(xiàn)

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('加載完成')
    }
  },500)

總結(jié)

知識(shí)點(diǎn):svg繪圖、js原生操作、jQuery

  • stroke-dasharray: 虛線
  • stroke-dashoffset: 偏移間隔

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論