美文网首页
GitHub前端50天50个项目,第2 进度步骤条

GitHub前端50天50个项目,第2 进度步骤条

作者: Justin小贾同学 | 来源:发表于2022-08-02 18:52 被阅读0次

    演示地址

    Progress Steps (50projects50days.com)

    实现思路

    结构样式方面主要是flex弹性布局,绝对定位,并且添加过渡效果。
    利用currentActive来存储当前状态,监听prev、next点击事件来改变进度状态

    代码

    html结构

    <!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>进度步骤条</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 {
        /* 子元素绝对定位,父元素相对定位 */
        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轴负方向变换自身高度的一半,目的是与圆圈居中对齐 */
        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 {
        /* 激活时改变边框颜色 */
        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 {
        /* 点击按钮时缩放大小,产生回弹的视觉 */
        transform: scale(0.98);
    }
    
    /* 按钮不可用时样式 */
    .btn:disabled {
        background-color: var(--line-border-empty);
        cursor: not-allowed;
    }
    

    js行为

    // 定义需要用到的dom对象
    const progress = document.getElementById('progress')
    const prev = document.getElementById('prev');
    const next = document.getElementById('next');
    const circles = document.querySelectorAll('.circle')
    
    // 保存当前进度步骤数
    let currentActive = 1;
    
    prev.addEventListener('click', () => {
        //当currentActive大于1时,可以回退到前一步
        if (currentActive > 1) {
            currentActive--;
            update();
        }
    })
    
    next.addEventListener('click', () => {
        // 当currentActive小于步骤数量时可以前进一步
        if (currentActive < circles.length) {
            currentActive++;
            update();
        }
    })
    
    function update() {
        // 更新步骤数
        circles.forEach((circle, index) => {
            if (index < currentActive) {
                circle.classList.add('active')
            } else {
                circle.classList.remove('active')
            }
        })
        // 更新进度百分比,circles.length - 1是因为分段数总是比步骤数少一
        progress.style.width = (currentActive - 1) / (circles.length - 1) * 100 + '%';
    
        // 更新按钮状态,步骤时prev按钮不可用,当前进度最终步骤时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

    相关文章

      网友评论

          本文标题:GitHub前端50天50个项目,第2 进度步骤条

          本文链接:https://www.haomeiwen.com/subject/niyjwrtx.html