实现思路:
1.定義一個流程數組和一个步骤状态
2.遍历这个流程数组,如果步骤状态大于流程,checked=true,
3.页面输出遍历的流程数组,checked的div点亮
最终效果
<!DOCTYPE html>
<html>
<title>js原生实现步骤条</title>
<head>
<style type="text/css">
#wrapper {
/* background-color: pink; */
}
#stepBox {
display: flex;
align-items: center;
}
#stepBox .step-item {
display: flex;
align-items: center;
margin-left: 2px;
/* border: 1px solid #000; */
}
.step-checked {
color: #67c23a;
}
.step-item .line{
display: inline-block;
width: 100px;
height: 2px;
background-color: #c0c4cc;
}
.step-item .line-actived{
width: 100px;
height: 2px;
background-color: #67c23a;
}
.button-box {
display: flex;
}
.button {
width: 80px;
height: 40px;
margin-top: 80px;
line-height: 40px;
text-align: center;
border: 1px solid #000;
cursor: pointer;
border-radius: 5px;
user-select: none;
}
.next-btn {
margin-left: 20px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="stepBox"></div>
<div class="button-box">
<div class="button" onclick="backLastStep()">上一步</div>
<div class="button next-btn" onclick="nextStep()">下一步</div>
<div>
</div>
</body>
<script>
var currentStep = 1; // 目前到达的步骤
var someArray = [{
stepName: '步骤1',
}, {
stepName: '步骤2',
}, {
stepName: '步骤3',
}]
function renderStepList() {
var elementArray = []
for(let index=0;index<someArray.length; index++){
var isChecked = false
var isChecked = ''
if(index<currentStep) {
isChecked = true
}
str = `<div class="step-item ${isChecked?'step-checked':''}">
<span>${someArray[index].stepName}</span>
${index<someArray.length-1?
`<i class="line ${isChecked?'line-actived':''}"></i>`:''
}
</div>`;
elementArray.push(str)
}
document.getElementById("stepBox").innerHTML = elementArray.join('')
}
function nextStep () { // 点击下一步
if(currentStep > someArray.length-1) {
alert('没有下一步了')
return
}
currentStep = currentStep + 1
this.renderStepList()
}
function backLastStep () { // 点击上一步
if(currentStep < 0) {
alert('没有上一步了')
return
}
currentStep = currentStep - 1
this.renderStepList()
}
renderStepList();
</script>
</html>