進度條由「步驟節點」+「連接線」組成,關鍵在於三種狀態的視覺區分:
利用單張箭頭圖片 (barbg.png/stepbar.png) 配合 background-position 切換狀態圖標,background-color 控制連接線顏色,實現零圖片切換的狀態變化。
<div class="flow_steps">
<ul>
<li class="done">第一步</li>
<li class="current_prev">第二步</li>
<li class="current">第三步</li>
<li>第四步</li>
<li class="last">第五步</li>
</ul>
</div>
.flow_steps ul {
display: flex;
list-style: none;
margin: 0; padding: 0;
}
.flow_steps ul li {
flex: 1;
height: 46px; /* 含箭頭高度 */
line-height: 23px;
padding: 0 40px 0 30px; /* 左右留白容納箭頭 */
text-align: center;
font-weight: bold;
color: #333;
position: relative;
/* 統一背景:箭頭圖片 + 灰色連接線色 */
background: url(../images/barbg.png) no-repeat 100% 0 #e4e4e4;
}
/* 狀態變體 */
.flow_steps ul li.done,
.flow_steps ul li.current_prev {
background-position: 100% -46px; /* 箭頭亮起位置 */
background-color: #ffeda2; /* 連接線變亮色 */
}
.flow_steps ul li.current {
color: #fff;
background-color: #990d1b; /* 當前步驟高亮色 */
/* 當前節點不需要箭頭圖片,或用單獨圖片 */
}
.flow_steps ul li.last {
background-image: none; /* 最後一步無箭頭 */
padding-right: 30px;
}
/* 箭頭圖片 barbg.png 結構說明:
寬度 = 步驟寬度 + 箭頭寬度
高度 = 3 狀態 × 23px = 69px
y=0: 默認灰箭頭
y=-23px: current_prev 亮箭頭
y=-46px: done 亮箭頭
*/
<div class="steps_bar">
<ul>
<li class="done"><span>STEP 1</span></li>
<li class="current_prev"><span>STEP 2</span></li>
<li class="current"><span>STEP 3</span></li>
<li><span>STEP 4</span></li>
<li class="last"><span>STEP 5</span></li>
</ul>
</div>
.steps_bar ul {
display: flex;
border-left: 1px solid #ccc;
overflow: hidden;
}
.steps_bar ul li {
flex: 1;
height: 60px;
padding-right: 19px; /* 箭頭寬度 */
line-height: 60px;
text-align: center;
font-weight: bold;
background: url(../images/stepbar.png) no-repeat 100% 0 #f1f1f1;
}
.steps_bar ul li span {
display: block;
height: 58px;
padding: 0 30px;
line-height: 58px;
background: #f1f1f1;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
/* 狀態樣式 */
.steps_bar ul li.done,
.steps_bar ul li.current_prev {
color: #fff;
background-color: #646464;
background-position: 100% -60px; /* current_prev */
}
.steps_bar ul li.done { background-position: 100% -120px; } /* done */
.steps_bar ul li.done span,
.steps_bar ul li.current_prev span {
background: #646464;
border-color: #646464;
}
.steps_bar ul li.current {
color: #fff;
background-color: #b70a06;
}
.steps_bar ul li.current span {
background: #b70a06;
border-color: #b70a06;
}
.steps_bar ul li.last {
background-position: 100% -180px;
background-color: transparent;
}
.steps_bar ul li.last span { background: #f1f1f1; }
.steps_bar ul li.last.current {
background-position: 100% -240px;
}
.steps_bar ul li.last.current span { background: #b70a06; }
function updateSteps(currentStep, totalSteps = 5) {
const $lis = $('.flow_steps li, .steps_bar li');
$lis.removeClass('done current_prev current');
$lis.each(function(index) {
const step = index + 1;
if (step < currentStep) {
$(this).addClass('done');
} else if (step === currentStep) {
$(this).addClass('current');
if (step > 1) {
$lis.eq(step - 2).addClass('current_prev');
}
}
});
// 特殊:第一步
if (currentStep === 1) {
$lis.first().addClass('current').removeClass('current_prev');
}
}
// 使用範例
updateSteps(3); // 顯示第三步進行中
--color-done: #ffeda2; --color-current: #990d1b;<ol> 語義化有序列表,配合 counter-reset/increment 自動編號aria-current="step" 無障礙屬性transition 實現狀態切換動畫