You need to enable JavaScript to run this app. 如何基于选中的单选按钮实现红黄绿三色分段进度条
优惠活动
大模型
产品
解决方案
定价
更多

如何基于选中的单选按钮实现红黄绿三色分段进度条

三色练习状态进度条实现方案

实现逻辑

  • 新增顶部进度条容器,内部三个子元素分别对应未完成(红)、完成困难(黄)、*顺利完成(青绿)*三个状态的占比块
  • 编写统计逻辑:每次单选按钮切换时,遍历所有练习的选中状态,分别统计三类状态的总数量,除以练习总数得到对应占比,同步更新进度条各块的宽度
  • 页面加载完成后自动执行一次统计,保证初始状态所有练习默认选红色时,进度条100%为红色符合需求

完整修改后代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<style>
/* 新增进度条样式 */
.progress-wrap {
  width: 100%;
  height: 24px;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 30px;
  display: flex;
}
.progress-item {
  height: 100%;
  transition: width 0.3s ease-in-out;
}
.progress-red {
  background: #E74C3C;
  width: 100%;
}
.progress-yellow {
  background: #ffdc70;
  width: 0;
}
.progress-turq {
  background: #1ABC9C;
  width: 0;
}
.exercise-item {
  margin-bottom: 15px;
}

/* 原有样式修正后保留 */
body { margin: 20px 50px; }
h1 { font-size: 1.5em; }
p { margin: 0; }

input[type="radio"] {
  margin-top: 7px;
  vertical-align: middle;
}

label {
  display: inline-block;
  font-family: Arial;
}    

/* 颜色定义 */
.turq {background:#16A085;}
.turq:checked {background:#1ABC9C;}
.turq:hover {background:#1ABC9C;}

.yellow {background:#eeca5a;}
.yellow:checked {background:#ffdc70;}
.yellow:hover {background:#ffdc70;}

.red {background:#C0392B;}
.red:checked {background:#E74C3C;}
.red:hover {background:#E74C3C;}

.radio {
  position:relative;
  -webkit-appearance:none;
  -moz-appearance:none;
  width:30px;
  height:30px;
  margin:5px;
  margin-top:0px;
  border-radius:100px;
  transition: all .3s ease-in-out;
}

.radio:checked:after{
  background:transparent;
}

.radio:after {
  position:absolute;
  top:5px;
  left:5px;
  width:20px;
  height:20px;
  background:#fff;
  content:"";
  border-radius:100px;
}
</style>
</head>
<body>
<!-- 进度条结构 -->
<div class="progress-wrap">
  <div class="progress-item progress-red"></div>
  <div class="progress-item progress-yellow"></div>
  <div class="progress-item progress-turq"></div>
</div>

<!-- 修正原有单选按钮的标签错误 -->
<div class="exercise-item">
  <input class="radio red" type="radio" name="radio1" id="radio1.1" checked>
  <input class="radio yellow" type="radio" name="radio1" id="radio2">
  <input class="radio turq" type="radio" name="radio1" id="radio3">
  <label for="radio3">Exercise 1</label>
</div>
    
<div class="exercise-item">
  <input class="radio red" type="radio" name="radio2" id="radio4" checked>
  <input class="radio yellow" type="radio" name="radio2" id="radio5">
  <input class="radio turq" type="radio" name="radio2" id="radio6">
  <label for="radio6">Exercise 2</label>
</div>

<script>
const allRadios = document.querySelectorAll('.radio');
const progressRed = document.querySelector('.progress-red');
const progressYellow = document.querySelector('.progress-yellow');
const progressTurq = document.querySelector('.progress-turq');

// 统计状态更新进度条
function updateProgress() {
  const totalExercises = document.querySelectorAll('.exercise-item').length;
  let redCount = 0, yellowCount = 0, turqCount = 0;

  // 遍历每个练习组的选中状态
  document.querySelectorAll('.exercise-item').forEach(item => {
    const checkedRadio = item.querySelector('input[type="radio"]:checked');
    if (checkedRadio.classList.contains('red')) redCount++;
    if (checkedRadio.classList.contains('yellow')) yellowCount++;
    if (checkedRadio.classList.contains('turq')) turqCount++;
  });

  // 计算占比更新宽度
  progressRed.style.width = `${(redCount / totalExercises) * 100}%`;
  progressYellow.style.width = `${(yellowCount / totalExercises) * 100}%`;
  progressTurq.style.width = `${(turqCount / totalExercises) * 100}%`;
}

// 监听所有单选按钮切换事件
allRadios.forEach(radio => {
  radio.addEventListener('change', updateProgress);
});

// 初始加载触发一次更新
updateProgress();
</script>
</body>
</html>

功能说明

  • 初始状态所有练习默认选中红色单选框,进度条全红符合需求
  • 切换任意练习的状态后,进度条会自动按三类状态的练习数量占比更新对应颜色的宽度
  • 后续新增练习只要保持.exercise-item的类名结构,不需要修改JS代码即可自动适配统计逻辑

内容的提问来源于stack exchange,提问作者Prodence King

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 17:45:03