JavaScript實(shí)現(xiàn)圓形進(jìn)度條效果
本文實(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'>開(kāi)始加載</text>
</svg>
</body>
<script async type='text/javascript'>
//js代碼見(jiàn)下文
</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í)有所幫助,也希望大家多多支持腳本之家。
- javascript 進(jìn)度條的幾種方法
- js實(shí)現(xiàn)進(jìn)度條的方法
- JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)加載進(jìn)度條代碼超簡(jiǎn)單
- js 進(jìn)度條實(shí)現(xiàn)代碼
- JS 進(jìn)度條效果實(shí)現(xiàn)代碼整理
- JS實(shí)現(xiàn)環(huán)形進(jìn)度條(從0到100%)效果
- Javascript jquery css 寫(xiě)的簡(jiǎn)單進(jìn)度條控件
- 用CSS+JS實(shí)現(xiàn)的進(jìn)度條效果效果
- js實(shí)現(xiàn)音頻控制進(jìn)度條功能
- PHP中使用Session配合Javascript實(shí)現(xiàn)文件上傳進(jìn)度條功能
相關(guān)文章
JavaScript數(shù)組和對(duì)象的復(fù)制
本篇文章主要介紹了JavaScript數(shù)組和對(duì)象的復(fù)制的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
AJAX使用了UpdatePanel后無(wú)法使用alert彈出腳本
今天在做項(xiàng)目的時(shí)候發(fā)現(xiàn)個(gè)問(wèn)題。。使用UpdatePanel后alert腳本就要報(bào)錯(cuò)了。2010-04-04
js+SVG實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了js+SVG實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
JavaScript如何調(diào)試有哪些建議和技巧附五款有用的調(diào)試工具
這篇文章給大家介紹javascript如何調(diào)試有哪些建議和技巧,涉及到j(luò)avascript調(diào)試方法相關(guān)知識(shí),對(duì)javascript調(diào)試方法感興趣的朋友可以參考下本篇文章2015-10-10
js提交form表單,并傳遞參數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇js提交form表單,并傳遞參數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
JS實(shí)現(xiàn)的獲取銀行卡號(hào)歸屬地及銀行卡類型操作示例
這篇文章主要介紹了JS實(shí)現(xiàn)的獲取銀行卡號(hào)歸屬地及銀行卡類型操作,結(jié)合實(shí)例形式分析了javascript不依賴第三方接口計(jì)算銀行卡歸屬地相關(guān)信息操作技巧,需要的朋友可以參考下2019-01-01
JavaScript詳解使用Promise處理回調(diào)地獄的兩種方法
這篇文章主要介紹了JavaScript詳解使用Promise處理回調(diào)地獄的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-11-11
javascript基本數(shù)據(jù)類型和轉(zhuǎn)換
本文主要介紹了javascript的基本數(shù)據(jù)類型和轉(zhuǎn)換,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03

