css+js实现环形进度条的完整代码
时间: 2023-12-23 12:03:45 浏览: 208
使用CSS3实现环形进度条效果
HTML代码:
```
<div class="progress-wrapper">
<div class="progress-ring">
<div class="progress-bar"></div>
</div>
<div class="progress-text">
<span class="progress-percent">0</span>%
</div>
</div>
```
CSS代码:
```
.progress-wrapper {
position: relative;
width: 200px;
height: 200px;
margin: 0 auto;
}
.progress-ring {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #e6e6e6;
}
.progress-bar {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
clip: rect(0, 100px, 200px, 0);
background-color: #ff6b6b;
}
.progress-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
font-weight: bold;
color: #ff6b6b;
}
.progress-percent {
font-size: 36px;
}
```
JavaScript代码:
```
function setProgress(percent) {
const circle = document.querySelector('.progress-bar');
const text = document.querySelector('.progress-percent');
const radius = circle.r.baseVal.value;
const circumference = 2 * Math.PI * radius;
const offset = circumference - percent / 100 * circumference;
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = offset;
text.textContent = percent;
}
setProgress(50); // 设置进度为50%
```
这段代码实现了一个环形进度条,包括HTML、CSS和JavaScript部分。其中,HTML部分定义了进度条的结构,CSS部分定义了进度条和文本的样式,JavaScript部分实现了设置进度的功能。你可以根据需要修改这些代码。
阅读全文