Technicalarticles
之前在项目中遇到一个环形进度条的需求,要求能实时更新进度,脑海中瞬间便蹦出css,svg,canvas3中方案,对于3种方案个人更偏向于svg,用法简单,代码量也很少,同时也便于实时控制。具体效果如下图:
代码非常简单:
<svg width="150px" height="150px" class="svg"> <circle r="70" cy="75" cx="75" stroke-width="8" stroke="#EAEFF4" stroke-linejoin="round" stroke-linecap="round" fill="none"/> <circle class="progress" r="70" cy="75" cx="75" stroke-width="8" stroke="#1593FF" stroke-linejoin="round" stroke-linecap="round" fill="none" stroke-dashoffset="0px" stroke-dasharray="471px" /> </svg>
为了便于演示,我们先用css动画控制:
svg { transform: rotate(-90deg); } .progress { animation: rotate 1500ms linear both; } @keyframes rotate { from { stroke-dashoffset: 471px; } to { stroke-dashoffset: 0px; } }
let path = document.querySelector('#path');
// 可获取路径的长度
let len = path.getTotalLength();
path.style.cssText = `stroke-dasharray:"${number}"`;
DO U LIKE?