50Days50Projects之day02-进度步骤效果

<!-- run -->

<style>
:root{
    --line-border-fill: #ff4d6d;
    --line-border-empty: #dee2e6;
}

.container{
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0px;
    overflow: hidden;
    box-sizing: border-box;
    text-align: center;
}

.progress-container{
    display: flex;
    justify-content: space-between;
    position: relative;
    margin-bottom: 30px;
    width: 350px;
    max-width: 100%;
}

.progress-container::before{
    content: '';
    background-color: var(--line-border-empty);
    height: 4px;
    width: 100%;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    z-index: -1;
    transition: 0.4s ease;
}

.progress{
    background-color: var(--line-border-fill);
    height: 4px;
    width: 0%;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    z-index: -1;
    transition: 0.4s ease;
}

.circle{
    background-color: #fff;
    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: 8px;
    cursor: pointer;
    font-family: inherit;
    padding: 8px 24px;
    font-size: 14px;
    margin: 5px;
}
.btn:active{
    transform: scale(0.98);
}

.btn:focus{
    outline: 0;
}

.btn:disabled{
    background-color: var(--line-border-empty);
}
</style>
<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>Önceki</button>
        <button class="btn" id="next">Sonraki</button>
   </div>
   <script>
   const progress = document.getElementById("progress");
const circles = document.querySelectorAll(".circle");
const prev = document.getElementById("prev");
const next = document.getElementById("next");


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(){
    circles.forEach((circle, index) => {
        if(index < currentActive){
            circle.classList.add('active');
        } else{
            circle.classList.remove('active');
        }
    })

    const actives = document.querySelectorAll(".active");

     //console.log(actives.length, circles.length);
     //console.log(actives.length / circles.length);
     //console.log((actives.length / circles.length) *100);
     //console.log((actives.length -1) / (circles.length -1) *100);
     //console.log((actives.length -1) / (circles.length -1) *100 + '%');

    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;
    }
}
  </script>

JS

const progress = document.getElementById("progress");
const circles = document.querySelectorAll(".circle");
const prev = document.getElementById("prev");
const next = document.getElementById("next");


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(){
    circles.forEach((circle, index) => {
        if(index < currentActive){
            circle.classList.add('active');
        } else{
            circle.classList.remove('active');
        }
    })

    const actives = document.querySelectorAll(".active");

     //console.log(actives.length, circles.length);
     //console.log(actives.length / circles.length);
     //console.log((actives.length / circles.length) *100);
     //console.log((actives.length -1) / (circles.length -1) *100);
     //console.log((actives.length -1) / (circles.length -1) *100 + '%');

    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;
    }
}

CSS

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@500&display=swap');

:root{
    --line-border-fill: #ff4d6d;
    --line-border-empty: #dee2e6;
}

*{
    box-sizing: border-box;
}

body{
    font-family: 'Inter', sans-serif;
    background-color: #f8f9fa;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0px;
    overflow: hidden;
}

.container{
    text-align: center;
}

.progress-container{
    display: flex;
    justify-content: space-between;
    position: relative;
    margin-bottom: 30px;
    width: 350px;
    max-width: 100%;
}

.progress-container::before{
    content: '';
    background-color: var(--line-border-empty);
    height: 4px;
    width: 100%;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    z-index: -1;
    transition: 0.4s ease;
}

.progress{
    background-color: var(--line-border-fill);
    height: 4px;
    width: 0%;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    z-index: -1;
    transition: 0.4s ease;
}

.circle{
    background-color: #fff;
    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: 8px;
    cursor: pointer;
    font-family: inherit;
    padding: 8px 24px;
    font-size: 14px;
    margin: 5px;
}
.btn:active{
    transform: scale(0.98);
}

.btn:focus{
    outline: 0;
}

.btn:disabled{
    background-color: var(--line-border-empty);
}
<!DOCTYPE html>
<html lang="en">
<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="style.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>Önceki</button>
        <button class="btn" id="next">Sonraki</button>
    </div>

    <script src="app.js"></script>
</body>
</html>

效果如下👇(博客园上运行指定是有点bug~)

posted @ 2022-06-09 22:31  CoderWGB  阅读(23)  评论(0)    收藏  举报