效果
完整代码
HTML部分
<div class="progress-container"> <div class="progress-bar"> <div class="progress" id="progress"></div> <div class="step active">1<span class="step-label">步骤 1</span></div> <div class="step">2<span class="step-label">步骤 2</span></div> <div class="step">3<span class="step-label">步骤 3</span></div> <div class="step">4<span class="step-label">步骤 4</span></div> </div> <button id="prevBtn" onclick="changeStep(-1)" disabled>上一步</button> <button id="nextBtn" onclick="changeStep(1)">下一步</button> </div>
CSS部分
body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .progress-container { width: 100%; max-width: 600px; padding: 20px; } .progress-bar { display: flex; justify-content: space-between; align-items: center; position: relative; margin-bottom: 30px; } .progress-bar::before { content: ''; position: absolute; top: 50%; left: 0; transform: translateY(-50%); height: 4px; width: 100%; background-color: #ddd; z-index: 1; } .progress { position: absolute; top: 50%; left: 0; transform: translateY(-50%); height: 4px; width: 0; background-color: #4CAF50; z-index: 2; transition: width 0.5s ease; } .step { width: 30px; height: 30px; background-color: #fff; border: 3px solid #ddd; border-radius: 50%; display: flex; justify-content: center; align-items: center; font-weight: bold; color: #999; z-index: 3; transition: all 0.3s ease; } .step.active { border-color: #4CAF50; color: #4CAF50; } .step-label { position: absolute; top: 100%; left: 50%; transform: translateX(-50%); text-align: center; font-size: 14px; color: #666; margin-top: 8px; } button { padding: 10px 20px; font-size: 16px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #45a049; } button:disabled { background-color: #ddd; cursor: not-allowed; }
JS
<script> let currentStep = 1; const totalSteps = 4; const progress = document.getElementById('progress'); const prevBtn = document.getElementById('prevBtn'); const nextBtn = document.getElementById('nextBtn'); const steps = document.querySelectorAll('.step'); function updateButtons() { prevBtn.disabled = currentStep === 1; nextBtn.disabled = currentStep === totalSteps; nextBtn.textContent = currentStep === totalSteps ? '完成' : '下一步'; } function updateProgressBar() { const percent = ((currentStep - 1) / (totalSteps - 1)) * 100; progress.style.width = `${percent}%`; } function updateSteps() { steps.forEach((step, index) => { if (index < currentStep) { step.classList.add('active'); } else { step.classList.remove('active'); } }); } function changeStep(n) { currentStep += n; if (currentStep < 1) currentStep = 1; if (currentStep > totalSteps) currentStep = totalSteps; updateButtons(); updateProgressBar(); updateSteps(); } updateButtons(); </script>