求助:如何编写JavaScript进度步骤组件的update函数
进度步骤组件的Update函数实现方案
问题说明
我是JavaScript新手,需要编写一个update函数,实现点击Prev或Next按钮时更新HTML界面,同时要用到已定义的progress CSS类。目前的代码框架已经写好,但update函数是空的,不知道怎么实现。
现有代码
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style8.css" /> <title>Progress Steps</title> </head> <body> <div class="container"> <div class="progress-container"> <div class="progress" id="progress"></div> <div class="circle active">1</div> <div class="circle">2</div> <div class="circle">3</div> <div class="circle">4</div> </div> <button class="btn" id="prev" disabled>Prev</button> <button class="btn" id="next">Next</button> </div> <script src="script8.js"></script> </body> </html>
CSS代码
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap'); :root { --line-border-fill: #3498db; --line-border-empty: #383838; } * { box-sizing: border-box; } body { background-color: #1f1f1f; font-family: 'Muli', sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; overflow: hidden; margin: 0; } .container { text-align: center; } .progress-container { display: flex; justify-content: space-between; position: relative; margin-bottom: 30px; max-width: 100%; width: 350px; } .progress-container::before { content: ''; background-color: var(--line-border-empty); position: absolute; top: 50%; left: 0; transform: translateY(-50%); height: 4px; width: 100%; z-index: -1; } .progress { background-color: var(--line-border-fill); position: absolute; top: 50%; left: 0; transform: translateY(-50%); height: 4px; width: 0%; z-index: -1; transition: 0.4s ease; } .circle { background-color: #1f1f1f; color:#e2e2e2; border-radius: 50%; height: 30px; width: 30px; display: flex; align-items: center; justify-content: center; border: 3px solid var(--line-border-empty); transition: 0.4s ease; } .circle.active { border-color: var(--line-border-fill); } .btn { background-color: var(--line-border-fill); color: #fff; border: 0; border-radius: 6px; cursor: pointer; font-family: inherit; padding: 8px 30px; margin: 5px; font-size: 14px; } .btn:active { transform: scale(0.98); } .btn:focus { outline: 0; } .btn:disabled { background-color: var(--line-border-empty); cursor: not-allowed; }
现有JavaScript代码
const progress = document.getElementById("progress"); const prev = document.getElementById("prev"); const next = document.getElementById("next"); const circles = document.querySelectorAll(".circle"); let currentActive = 1; next.addEventListener("click", () => { currentActive++; if (currentActive > circles.length) { currentActive = circles.length; } update(); }); prev.addEventListener("click", () => { currentActive--; if (currentActive < 1) { currentActive = 1; } update(); }); function update() { //这是我需要帮助的部分 } const actives = document.querySelectorAll(".active"); progress.style.width = ((actives.length - 1) / (circles.length - 1)) * 100 + "%"; if (currentActive === 1) { prev.disabled = true; } else if (currentActive === circles.length) { next.disabled = true; } else { prev.disabled = false; next.disabled = false; }
解决方案
你现在的问题是:原本的更新逻辑(进度条宽度计算、按钮状态控制)写在了update函数外面,不会被点击按钮时触发;同时缺少给圆圈切换active类的逻辑。
把所有更新逻辑移到update函数里,同时添加圆圈的类切换代码,完整的JavaScript代码如下:
const progress = document.getElementById("progress"); const prev = document.getElementById("prev"); const next = document.getElementById("next"); const circles = document.querySelectorAll(".circle"); let currentActive = 1; next.addEventListener("click", () => { currentActive++; if (currentActive > circles.length) { currentActive = circles.length; } update(); }); prev.addEventListener("click", () => { currentActive--; if (currentActive < 1) { currentActive = 1; } update(); }); function update() { // 遍历所有圆圈,根据currentActive添加/移除active类 circles.forEach((circle, idx) => { if (idx + 1 <= currentActive) { circle.classList.add("active"); } else { circle.classList.remove("active"); } }); // 更新进度条宽度 const actives = document.querySelectorAll(".active"); progress.style.width = ((actives.length - 1) / (circles.length - 1)) * 100 + "%"; // 控制按钮禁用状态 if (currentActive === 1) { prev.disabled = true; } else if (currentActive === circles.length) { next.disabled = true; } else { prev.disabled = false; next.disabled = false; } }
代码解释
- 圆圈状态更新:遍历每个圆圈,索引从0开始,所以
idx+1对应圆圈的序号。如果序号小于等于当前激活的currentActive,就添加active类,否则移除,这样就能正确显示哪些步骤是已完成的。 - 进度条宽度计算:根据已激活的圆圈数量,计算出进度条应该占总宽度的百分比,比如4个圆圈时,激活2个的话,进度条宽度就是(2-1)/(4-1)*100 = 33.33%。
- 按钮状态控制:当在第一个步骤时,禁用Prev按钮;在最后一个步骤时,禁用Next按钮;中间步骤则两个按钮都可用。
内容的提问来源于stack exchange,提问作者slprbrck
相关产品推荐
相关产品推荐

