演示地址
Progress Steps (50projects50days.com)
實(shí)現(xiàn)思路
結(jié)構(gòu)樣式方面主要是flex彈性布局纽绍,絕對(duì)定位,并且添加過渡效果。
利用currentActive來存儲(chǔ)當(dāng)前狀態(tài)摧玫,監(jiān)聽prev、next點(diǎn)擊事件來改變進(jìn)度狀態(tài)
代碼
html結(jié)構(gòu)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>進(jìn)度步驟條</title>
<link rel="stylesheet" href="style.css">
</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="./script.js"></script>
</body>
</html>
css樣式
* {
box-sizing: border-box;
}
:root {
--line-border-fill: #3498db;
--line-border-empty: #e0e0e0;
}
body {
/* 彈性布局绑青,垂直居中 */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #f6f7fb;
}
.container {
/* 文本水平居中诬像,這里目的是讓按鈕居中 */
text-align: center;
}
.progress-container {
/* 子元素絕對(duì)定位,父元素相對(duì)定位 */
position: relative;
/* 彈性布局闸婴,水平方向分散坏挠,垂直方向居中 */
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
width: 320px;
}
.progress-container::before {
content: "";
position: absolute;
top: 50%;
left: 0;
z-index: -1;
/* Y軸負(fù)方向變換自身高度的一半,目的是與圓圈居中對(duì)齊 */
transform: translateY(-50%);
width: 100%;
height: 4px;
background-color: var(--line-border-empty);
transition: all 0.4s ease;
}
.progress {
position: absolute;
top: 50%;
left: 0;
z-index: -1;
transform: translateY(-50%);
width: 0%;
height: 4px;
background-color: var(--line-border-fill);
transition: all 0.4s ease;
}
.circle {
display: flex;
justify-content: center;
align-items: center;
width: 30px;
height: 30px;
color: #999;
background: #fff;
border-radius: 50%;
border: 3px solid var(--line-border-empty);
transition: border 0.4s ease;
}
.circle.active {
/* 激活時(shí)改變邊框顏色 */
border: 3px solid var(--line-border-fill);
}
.btn {
padding: 10px 30px;
margin: 5px;
background-color: var(--line-border-fill);
border: 0;
border-radius: 6px;
color: #fff;
font-size: 14px;
}
.btn:active {
/* 點(diǎn)擊按鈕時(shí)縮放大小邪乍,產(chǎn)生回彈的視覺 */
transform: scale(0.98);
}
/* 按鈕不可用時(shí)樣式 */
.btn:disabled {
background-color: var(--line-border-empty);
cursor: not-allowed;
}
js行為
// 定義需要用到的dom對(duì)象
const progress = document.getElementById('progress')
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const circles = document.querySelectorAll('.circle')
// 保存當(dāng)前進(jìn)度步驟數(shù)
let currentActive = 1;
prev.addEventListener('click', () => {
//當(dāng)currentActive大于1時(shí)降狠,可以回退到前一步
if (currentActive > 1) {
currentActive--;
update();
}
})
next.addEventListener('click', () => {
// 當(dāng)currentActive小于步驟數(shù)量時(shí)可以前進(jìn)一步
if (currentActive < circles.length) {
currentActive++;
update();
}
})
function update() {
// 更新步驟數(shù)
circles.forEach((circle, index) => {
if (index < currentActive) {
circle.classList.add('active')
} else {
circle.classList.remove('active')
}
})
// 更新進(jìn)度百分比,circles.length - 1是因?yàn)榉侄螖?shù)總是比步驟數(shù)少一
progress.style.width = (currentActive - 1) / (circles.length - 1) * 100 + '%';
// 更新按鈕狀態(tài)溺欧,步驟時(shí)prev按鈕不可用喊熟,當(dāng)前進(jìn)度最終步驟時(shí)next按鈕不可用
if (currentActive === 1) {
prev.disabled = true
} else if (currentActive === circles.length) {
next.disabled = true
} else {
prev.disabled = false
next.disabled = false
}
}
參考資料:50 Projects 50 Days | Traversy Media
50 unique mini-projects to sharpen your HTML, CSS & JavaScript skills