js实现圆形进度条
js实现圆形进度条,源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js实现圆形进度条</title> <style> .container { width: 300px; height: 300px; margin: 100px auto; /* border: 1px solid #000; */ } .circle-progressbar { position: relative; width: 100%; height: 100%; } .circle-progressbar .title { margin: 0; text-align: center; line-height: 300px; } .circle-progressbar div { box-sizing: border-box; } .circle-progressbar .wrapper { position: absolute; top: 0; width: 150px; height: 300px; /* border: 1px solid red; */ overflow: hidden; } .circle-progressbar .wrapper.left-wrapper { left: 0; } .circle-progressbar .wrapper.right-wrapper { right: 0; } .circle-progressbar .wrapper .circle-bar { position: absolute; width: 300px; height: 300px; border: 30px solid transparent; border-radius: 50%; transform: rotate(-135deg); transition: transform .3s; } .circle-progressbar .left-wrapper .circle-bar { left: 0; border-left-color: turquoise; border-bottom-color: turquoise; } .circle-progressbar .right-wrapper .circle-bar { right: 0; border-right-color: turquoise; border-top-color: turquoise; } </style> </head> <body> <div class="container"> <div class="circle-progressbar"> <h1 class="title">0%</h1> <div class="wrapper left-wrapper"> <div class="circle-bar"></div> </div> <div class="wrapper right-wrapper"> <div class="circle-bar"></div> </div> </div> </div> <script> const circleProgressBar = CircleProgressBar(); let p = 0; let t = setInterval(() => {circleProgressBar( ++ p)}, 200); function CircleProgressBar() { const oLeftCircleBar = getCircleBar('left'), oRightCircleBar = getCircleBar('right'), oTitle = document.querySelector('.circle-progressbar .title'); return function(percent) { if(percent >= 0 && percent <= 50) { setRotate(oRightCircleBar, percent) }else if(percent >= 50 && percent <= 100) { setRotate(oLeftCircleBar, percent - 50) } if(percent >= 0 && percent <= 100) { oTitle.innerText = percent + '%'; } if(percent == 100) { t=window.clearInterval(t); } } function formatDegree(percent) { return `rotate(${ -135 + (360 / 100 * percent) }deg)` } function setRotate(node, percent) { node.style.transform = formatDegree(percent) } function getCircleBar(dir) { return document.querySelector(`.circle-progressbar .${ dir }-wrapper .circle-bar`); } } </script> </body> </html> |