加载进度围绕一个圆的效果通常是通过SVG或者Canvas来实现的。以下是一个使用SVG和JavaScript实现的基本示例:
requestAnimationFrame
)不断更新图形属性来实现动态效果。<animate>
元素或者JavaScript来控制属性变化。canvas.getContext('2d')
来绘制,并通过JavaScript定时更新。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Loading Circle</title>
<style>
#progressCircle {
transform: rotate(-90deg);
}
</style>
</head>
<body>
<svg width="100" height="100">
<circle id="progressCircle" cx="50" cy="50" r="45" stroke="#ddd" stroke-width="10" fill="none" />
<circle id="progressRing" cx="50" cy="50" r="45" stroke="#007bff" stroke-width="10" fill="none" stroke-dasharray="0, 283" />
</svg>
<script>
const circle = document.getElementById('progressRing');
let radius = circle.r.baseVal.value;
let circumference = radius * 2 * Math.PI;
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = circumference;
function setProgress(percent) {
const offset = circumference - percent / 100 * circumference;
circle.style.strokeDashoffset = offset;
}
// 模拟加载进度
let progress = 0;
const interval = setInterval(() => {
progress += 1;
setProgress(progress);
if (progress >= 100) clearInterval(interval);
}, 50);
</script>
</body>
</html>
requestAnimationFrame
代替setInterval
来改善。通过上述方法,可以创建一个平滑的加载进度圆环,并且可以根据实际需求进行调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云