drawStep(step, data) 方法動態更新進度。var steps = [
{ StepNum: 1, StepText: "填寫信息" },
{ StepNum: 2, StepText: "驗證手機" },
{ StepNum: 3, StepText: "設置密碼" },
{ StepNum: 4, StepText: "完成註冊" }
];
<link rel="stylesheet" href="css/step-dc-style1.css" /> <!-- 或 style2 -->
<script src="jquery.min.js"></script>
<script src="step-jquery-dc.js"></script>
<div class="step_context test"></div>
<!-- 手動控制演示 -->
當前步驟:<input id="curStep" value="2" />
<button onclick="stepTool.drawStep(+$('#curStep').val(), steps)">跳轉</button>
<script>
var stepTool = new Step_Tool_dc("test", "onStepClick");
stepTool.drawStep(2, steps); // 初始顯示第 2 步
function onStepClick(result) {
console.log("點擊步驟:", result.value, result.text);
stepTool.drawStep(result.value, steps); // 更新視圖
// 載入對應步驟的表單/內容...
}
</script>
var Step_Tool_dc = function(containerClass, callbackName) {
this.containerClass = containerClass;
this.callback = window[callbackName];
this.steps = [];
this.htmlCache = "";
};
Step_Tool_dc.prototype = {
drawStep: function(currentStep, stepList) {
this.clear();
this._buildSteps(currentStep, stepList);
this._render();
this._bindEvents();
},
_buildSteps: function(currentStep, stepList) {
for (var i = 0; i < stepList.length; i++) {
var item = stepList[i];
var stepObj = new Step(currentStep, item.StepNum, item.StepText, stepList.length);
stepObj.createHtml();
this.steps.push(stepObj);
}
},
_render: function() {
this.htmlCache = "<ul>" + this.steps.map(function(s) { return s.htmlCode; }).join("") + "</ul>";
$("." + this.containerClass).html(this.htmlCache);
},
_bindEvents: function() {
var self = this;
$("." + this.containerClass + " ul li a").click(function() {
var val = $(this).data("value");
var txt = $(this).data("text");
if (self.callback) self.callback({ value: val, text: txt });
});
},
clear: function() {
this.steps = [];
this.htmlCache = "";
$("." + this.containerClass).empty();
}
};
var Step = function(currentStep, stepNum, stepText, totalCount) {
this.currentStep = currentStep;
this.stepNum = stepNum;
this.stepText = stepText;
this.totalCount = totalCount;
this.htmlCode = "";
};
Step.prototype.createHtml = function() {
var cls = "";
var cs = this.currentStep, sn = this.stepNum, tc = this.totalCount;
if (cs > sn) cls = "finshStep"; // 已完成
else if (cs === sn) {
if (sn === 1) cls = "firstFinshStep_curr1"; // 第一步且當前
else if (sn === tc) cls = "finshlast"; // 最後一步且當前
else cls = "coressStep"; // 中間步驟當前
} else { // cs < sn
if (sn === tc) cls = "last"; // 最後一步未開始
else if (sn === 1) cls = "firstFinshStep"; // 第一步未開始(罕見)
else cls = "loadStep"; // 中間未開始
}
this.htmlCode = '<li class="' + cls + '">'
+ '<span>' + sn + '</span>'
+ '<a href="#" data-value="' + sn + '" data-text="' + stepText + '">' + stepText + '</a>'
+ '</li>';
};
.finshStep:已完成(綠色勾選/灰色).coressStep:當前進行中(高亮藍/橙).loadStep:未開始(灰色).firstFinshStep、.finshlast:首尾特殊狀態兩套皮膚分別定義上述類名的顏色、圖標、連接線樣式,切換只需換 <link>。