css 如何在浏览网页的容器部分后启动进度条计数

jexiocij  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(94)

如何开始我的进度条计数时,我会去我的网页容器部分。我想我必须改变我的javascript代码有点请帮助我。
HTML代码

<div class="container">
        <div class="circular-progress">
            <span class="progress-value">100%</span>
        </div>

        <span class="text">HTML & CSS</span>
    </div>

    <!-- JavaScript -->
    <script src="js/script.js"></script>

CSS代码

/* Google Fonts - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}
body{
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #7d2ae8;
}
.container{
    display: flex;
    width: 420px;
    padding: 50px 0;
    border-radius: 8px;
    background: #fff;
    row-gap: 30px;
    flex-direction: column;
    align-items: center;
}
.circular-progress{
    position: relative;
    height: 250px;
    width: 250px;
    border-radius: 50%;
    background: conic-gradient(#7d2ae8 3.6deg, #ededed 0deg);
    display: flex;
    align-items: center;
    justify-content: center;
}
.circular-progress::before{
    content: "";
    position: absolute;
    height: 210px;
    width: 210px;
    border-radius: 50%;
    background-color: #fff;
}
.progress-value{
    position: relative;
    font-size: 40px;
    font-weight: 600;
    color: #7d2ae8;
}
.text{
    font-size: 30px;
    font-weight: 500;
    color: #606060;
}

Javascript程式码

let circularProgress = document.querySelector(".circular-progress"),
    progressValue = document.querySelector(".progress-value");

let progressStartValue = 0,    
    progressEndValue = 90,    
    speed = 100;
    
let progress = setInterval(() => {
    progressStartValue++;

    progressValue.textContent = `${progressStartValue}%`
    circularProgress.style.background = `conic-gradient(#7d2ae8 ${progressStartValue * 3.6}deg, #ededed 0deg)`

    if(progressStartValue == progressEndValue){
        clearInterval(progress);
    }    
}, speed);

当我加载我的网页在第一次然后progressbar是计数值,但我想开始计数后,我去该网页的该部分。

erhoui1w

erhoui1w1#

你可以使用Intersection Observer。你选择一个HTML元素来观察,一旦这个元素出现在视图中,就会触发进度条的启动。

相关问题