代码来自于github上的50projects50days,我加了一些注释
效果图展示Progress Steps (可能需要科学上网)
话不多说直接上代码(代码中的图片url和网址之类的可能需要科学上网,如果没有的话可以考虑欢称自己的图片)
大部分解释都在代码注释中,可能会随着天数的增加而减少代码(因为记住了)
HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<title>进度步骤</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="index.js"></script>
</body>
</html>
CSS
/*root为html文件,在其中定义属性,然后可以通过var(属性名)来调用该属性*/
:root {
--line-border-fill: #3498db;
--line-border-empty: #e0e0e0;
}
* {
box-sizing: border-box;
}
body {
background-color: #f6f7fb;
font-family: '宋体';
display: flex;
/* align-items 属性将所有直接子节点上的 align-self 值设置为一个组。align-self 属性设置项目在其包含块中在交叉轴方向上的对齐方式 */
align-items: center;
/* justify-content 属性定义了浏览器之间,如何分配顺着弹性容器主轴 (或者网格行轴) 的元素之间及其周围的空间 */
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
/* CSS 属性定义行内内容(例如文字)如何相对它的块父元素对齐。text-align 并不控制块元素自己的对齐,只控制它的行内内容的对齐 */
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: '';
/* 使用在root中定义好的颜色 灰 */
background-color: var(--line-border-empty);
/* 子绝父相 */
position: absolute;
top: 50%;
left: 0;
/* 与top搭配使线条中点处于父级盒子中点,也与下方的蓝线重合 */
transform: translateY(-50%);
height: 4px;
/* 一开始是整个灰色线条 */
width: 100%;
/* 放在最底部,让上面的数字压着它 */
z-index: -1;
}
.progress {
/* 使用在root中定义好的颜色 蓝 */
background-color: var(--line-border-fill);
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 4px;
/* 蓝色线条一开始为0 */
width: 0%;
z-index: -1;
transition: 0.4s ease;
}
.circle {
background-color: #fff;
color: #999;
/* 变成圆 */
border-radius: 50%;
height: 30px;
width: 30px;
display: flex;
align-items: center;
justify-content: center;
/* 默认边线颜色为灰 */
border: 3px solid var(--line-border-empty);
/* 添加动画 */
transition: all 0.4s ease;
}
/* 当circle有active时,border变色 */
.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;
}
JS
const progress = document.getElementById('progress')
const prev = document.getElementById('prev')
const next = document.getElementById('next')
const circles = document.querySelectorAll('.circle')
// console.log(circles)
// 创建一个active的标记
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的数值来给circle添加active
circles.forEach((circle, idx) => {
if(idx < currentActive) {
circle.classList.add('active')
} else {
circle.classList.remove('active')
}
})
// 获取所有带有active的circle
const actives = document.querySelectorAll('.active')
// 根据带有active的circle的个数来设置蓝色线条的长度
progress.style.width = (actives.length - 1) / (circles.length - 1) * 100 + '%'
// 根据currenActive来设置两个按钮的可选与不可选
if(currentActive === 1) {
prev.disabled = true
} else if (currentActive === circles.length) {
next.disabled = true
} else {
prev.disabled = false
next.disabled = false
}
}
本文介绍了一个简单的进度步骤条实现方案,包括HTML结构、CSS样式及JavaScript交互逻辑,展示了如何通过激活不同的步骤来更新进度条的状态。
702

被折叠的 条评论
为什么被折叠?



