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'>開始加載</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í)有所幫助,也希望大家多多支持腳本之家。
- javascript 進(jìn)度條的幾種方法
- js實(shí)現(xiàn)進(jìn)度條的方法
- JavaScript實(shí)現(xiàn)網(wǎng)頁加載進(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 寫的簡(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)文章
經(jīng)典的間隔時(shí)間滾動(dòng)新聞(圖片),可控制滾動(dòng)
經(jīng)典的間隔時(shí)間滾動(dòng)新聞(圖片),可控制滾動(dòng) 其實(shí)這個(gè)也挺多網(wǎng)站用到的,簡(jiǎn)單又實(shí)用。(2010-05-05JS使用AudioContext實(shí)現(xiàn)音頻流實(shí)時(shí)播放
這篇文章主要為大家詳細(xì)介紹了JavaScript如何使用AudioContext實(shí)現(xiàn)音頻流實(shí)時(shí)播放功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01JavaScript模塊管理的簡(jiǎn)單實(shí)現(xiàn)方式詳解
這篇文章主要介紹了JavaScript模塊管理的簡(jiǎn)單實(shí)現(xiàn)方式,它方便組織你的代碼,提高項(xiàng)目的可維護(hù)性。一個(gè)項(xiàng)目的可維護(hù)性高不高,也體現(xiàn)一個(gè)程序員的水平,在如今越來越復(fù)雜的前端項(xiàng)目,這一點(diǎn)尤為重要。,需要的朋友可以參考下2019-06-06countUp.js實(shí)現(xiàn)數(shù)字動(dòng)態(tài)變化效果
這篇文章主要為大家詳細(xì)介紹了countUp.js實(shí)現(xiàn)數(shù)字動(dòng)態(tài)變化效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Javascript實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Javascript實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07