前端大杂烩

【css】使用conic-gradient绘制圆环进度条

2023-10-30  本文已影响0人  写写而已
GIF 2023-10-31 12-21-17.gif
<div class="circle">
    <div class="stop"></div>
    <div class="center">
        <span>36</span>
        <label>%</label>
    </div>
</div>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    html,
    body {
        height: 100%;
        background-color: #fff;
    }
    .circle {
        margin: 40px;
        width: 120px;
        height: 120px;
        background-color: #fff;
        border-radius: 50%;
        background: conic-gradient(#189cfb 0deg, #189cfb 0deg, #eee 0deg, #eee 360deg);
        position: relative;
        /* transition: 1s linear; */
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
    }
    .circle::after {
        content: '';
        display: block;
        width: 92px;
        height: 92px;
        border-radius: 50%;
        background-color: #fff;
        transform: translate(14px, 14px);
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }

    .circle::before,
    .stop {
        content: '';
        display: block;
        width: 14px;
        height: 14px;
        border-radius: 50%;
        background-color: #189cfb;
        position: absolute;
        top: 0;
        left: 53px;
        background: linear-gradient(0, #189cfb 0%, #189cfb 50%, #168ce1 100%);
    }
    .stop {
        transform: rotate(0deg);
        /* transition: 1s linear; */
        transform-origin: 7px 60px;
    }
    .center {
        position: absolute;
        top: 14px;
        left: 14px;
        z-index: 10;
        width: 92px;
        height: 92px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Helvetica, sans-serif;
    }
    .center span {
        font-size: 32px;
        letter-spacing: -1px;
    }
    .center label {
        font-size: 16px;
        margin-left: 5px;
        transform: translateY(5px);
    }
</style>
<script>
    let circle = document.querySelector('.circle')
    let stop = document.querySelector('.stop')
    let center = document.querySelector('.center span')
    let data = 0
    function loop() {
        center.textContent = parseInt(data * 100)
        let r = data * 360
        circle.style.background = `conic-gradient(#189cfb 0deg, #189cfb ${r}deg, #eee ${r}deg, #eee 360deg)`
        stop.style.transform = `rotate(${r}deg)`
        if (data < 1) {
            data += 0.01
            window.requestAnimationFrame(loop)
        }
    }
    loop()
</script>

上一篇下一篇

猜你喜欢

热点阅读