萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

多步驟進度條 CSS 實現原理與兩種風格完整代碼

多步驟進度條設計核心

進度條由「步驟節點」+「連接線」組成,關鍵在於三種狀態的視覺區分:

  • Done(已完成):高亮色、打鉤圖標、連接線點亮
  • Current(進行中):高亮色、脈衝動畫、當前節點放大
  • Default(未開始):灰色、虛線連接、節點縮小

核心 CSS 技巧:background-image + background-color 聯動

利用單張箭頭圖片 (barbg.png/stepbar.png) 配合 background-position 切換狀態圖標,background-color 控制連接線顏色,實現零圖片切換的狀態變化。

Demo 1:純色塊風格(淘寶風)

HTML 結構

<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>

核心 CSS

.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 亮箭頭
*/

Demo 2:帶邊框風格(更立體)

HTML 結構

<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>

核心 CSS

.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; }

背景圖片切片規格

  • barbg.png:寬 = 步驟寬 + 箭頭寬,高 = 3 × 步驟高(默認、current_prev、done)
  • stepbar.png:寬 = 步驟寬 + 箭頭寬,高 = 4 × 60px = 240px(默認、current_prev、done、last、last.current)
  • 必須使用 PNG 透明背景,箭頭部分透明、主體實色

JavaScript 動態狀態控制

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);  // 顯示第三步進行中

現代化改良建議

  • 使用 CSS 變數管理顏色:--color-done: #ffeda2; --color-current: #990d1b;
  • 改用 <ol> 語義化有序列表,配合 counter-reset/increment 自動編號
  • 添加 aria-current="step" 無障礙屬性
  • 配合 transition 實現狀態切換動畫
  • 響應式:小屏改為垂直步驟條,連接線變垂直
copyright © 萬盛學電腦網 all rights reserved