CSS圓形進度條詳解

一、CSS圓形進度條漸變

在實現CSS圓形進度條時,我們也可以將其改造為漸變的效果。如下所示:

    <div class="progress">
        <div class="progress-circle">
            <span class="progress-text">75%</span>
        </div>
    </div>
    .progress {
        width: 200px;
        height: 200px;
        background-color: #eee;
        border-radius: 50%;
        position: relative;
        margin: 50px auto;
    }
    .progress-circle {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        clip: rect(0, 200px, 200px, 100px);
        position: absolute;
    }
    .progress-circle:before {
        content: "";
        display: block;
        margin: 0 auto;
        width: 100px;
        height: 200px;
        background-color: #09c;
        border-radius: 50%;
    }
    .progress-circle:after {
        content: "";
        display: block;
        margin: 0 auto;
        width: 100px;
        height: 200px;
        background-color: #ccc;
        border-radius: 50%;
    }
    .progress-text {
        color: #333;
        font-size: 22px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .progress-circle:nth-child(1):before, .progress-circle:nth-child(1):after {
        transform: rotate(0deg);
    }
    .progress-circle:nth-child(1):after {
        background-color: #09c;
    }
    .progress-circle:nth-child(2):before, .progress-circle:nth-child(2):after {
        transform: rotate(30deg);
    }
    .progress-circle:nth-child(2):after {
        background-color: #7f0;
    }
    .progress-circle:nth-child(3):before, .progress-circle:nth-child(3):after {
        transform: rotate(60deg);
    }
    .progress-circle:nth-child(3):after {
        background-color: #f70;
    }
    .progress-circle:nth-child(4):before, .progress-circle:nth-child(4):after {
        transform: rotate(90deg);
    }
    .progress-circle:nth-child(4):after {
        background-color: #fb0;
    }
    .progress-circle:nth-child(5):before, .progress-circle:nth-child(5):after {
        transform: rotate(120deg);
    }
    .progress-circle:nth-child(5):after {
        background-color: #f00;
    }
    .progress-circle:nth-child(6):before, .progress-circle:nth-child(6):after {
        transform: rotate(150deg);
    }
    .progress-circle:nth-child(6):after {
        background-color: #71696a;
    }
    .progress-circle:nth-child(7):before, .progress-circle:nth-child(7):after {
        transform: rotate(180deg);
    }
    .progress-circle:nth-child(7):after {
        background-color: #09c;
    }
    .progress-circle:nth-child(8):before, .progress-circle:nth-child(8):after {
        transform: rotate(210deg);
    }
    .progress-circle:nth-child(8):after {
        background-color: #7f0;
    }
    .progress-circle:nth-child(9):before, .progress-circle:nth-child(9):after {
        transform: rotate(240deg);
    }
    .progress-circle:nth-child(9):after {
        background-color: #f70;
    }
    .progress-circle:nth-child(10):before, .progress-circle:nth-child(10):after {
        transform: rotate(270deg);
    }
    .progress-circle:nth-child(10):after {
        background-color: #fb0;
    }
    .progress-circle:nth-child(11):before, .progress-circle:nth-child(11):after {
        transform: rotate(300deg);
    }
    .progress-circle:nth-child(11):after {
        background-color: #f00;
    }
    .progress-circle:nth-child(12):before, .progress-circle:nth-child(12):after {
        transform: rotate(330deg);
    }
    .progress-circle:nth-child(12):after {
        background-color: #71696a;
    }

通過before和after屬性的使用,使得CSS圓形漸變進度條呈現出多重顏色共存的效果,為圓形進度條增加了許多新穎的玩法。

二、CSS圓環進度條

與CSS圓形進度條不同的是,圓環進度條不填充顏色,而是採用邊框寬度的變化表達進程的進度。如下所示:

    <div class="progress">
        <div class="progress-circle pie">
            <div class="left-side half-circle"></div>
            <div class="right-side half-circle">
                <div class="bar"></div>
            </div>
        </div>
        <div class="progress-circle">
            <span class="progress-text">75%</span>
        </div>
    </div>
    .progress-circle {
        position: relative;
        width: 100px;
        height: 100px;
        float: left;
        margin-right: 20px;
    }
    .half-circle {
        position: absolute;
        border: 4px solid #cecece;
        border-top: 4px solid #1E90FF;
        border-radius: 50%;
        width: 100px;
        height: 100px;
    }
    .left-side {
        clip: rect(0, 50px, 100px, 0);
    }
    .right-side {
        clip: rect(0, 100px, 100px, 50px);
        animation: loading-1 1s linear forwards;
    }
    .bar {
        position: absolute;
        border: 4px solid #1E90FF;
        border-radius: 50%;
        width: 100px;
        height: 100px;
        clip: rect(0, 50px, 100px, 0);
        animation: loading-2 1s linear forwards;
    }
    .progress-text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 22px;
        color: #333;
    }
    @keyframes loading-1 {
        0% {
            transform: rotate(0deg);
        }
        100% {
            transform: rotate(180deg);
        }
    }
    @keyframes loading-2 {
        0% {
            transform: rotate(0deg);
        }
        100% {
            transform: rotate(180deg);
        }
    }
    .pie{
        border-radius: 50%;
        box-shadow: 0 0 0 5px rgba(16, 26, 60, 0.1) inset;
        margin: 0px auto;
    }

此處使用到了clip屬性,控制了左右兩個元素呈現出圓弧狀,實現了圓環進度條的效果。同時,通過對佔比的修改,可以實現不同的進度展示。

三、CSS圓形進度條中間顯示百分比

在許多情況下,圓形進度條大小過小,直接在圖形上面顯示進度文本會顯得十分擁擠。因此,我們可以將進度文本調整至圓形進度條中心位置,方便顯示、美觀大方。如下所示:

    <div id="percent"></div>

    <svg viewBox="0 0 32 32" class="circle">
        <circle class="progress-meter" cx="16" cy="16" r="15.9155" fill="none" stroke-width="1.8" />
        <circle class="progress-value" cx="16" cy="16" r="15.9155" fill="none" stroke-width="1.8" />
    </svg>
    .circle {
        width: 200px;
        height: 200px;
        transform: rotate(-90deg);
        display: block;
        margin: 0 auto;
        fill: transparent;
    }

    .progress-meter {
        stroke: #d2d3d4;
    }

    .progress-value {
        stroke: #1E90FF;
        stroke-linecap: round;
        stroke-dasharray: 0, 100;
        animation: progress 3s linear forwards;
    }

    #percent {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        color: #444;
        font-size: 40px;
    }

    @keyframes progress {
        0% {
            stroke-dasharray: 0, 100;
        }
        100% {
            stroke-dasharray: 75, 100;
        }
    }

通過將圓形進度條與文本分離計算,並通過CSS樣式進行組合,實現圓形進度條與文本視覺上的分開,同時,進度條使用stroke-dasharray屬性,控制了進度的呈現,而動畫效果則通過CSS的keyframes來實現。

四、圓形進度條

圓形進度條可謂是圓形進度條中的代表,其放射狀的效果、簡單易上手的實現方式,受到了廣泛的推崇。如下所示:

    <div class="progress">
        <div class="progress-circle">
            <div class="progress-left"></div>
            <div class="progress-right">
                <div class="progress-bar"><p>50</p></div>
            </div>
        </div>
        <div class="progress-circle">
            <p class="progress-text">50%</p>
        </div>
    </div>
    .progress {
        width: 100%;
        height: 100%;
        position: relative;
    }
    .progress-circle {
        width: 100px;
        height: 100px;
        position: relative;
        float: left;
        margin-right: 20px;
    }
    .progress-circle .progress-text {
        font-size: 22px;
        color: #333;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .progress-circle .progress-left, .progress-circle .progress-right {
        transform: rotate(-50deg);
        clip: rect(0, 55px, 110px, 0);
        animation: loading-1 1s linear forwards;
    }
    .progress-circle .progress-bar {
        position: absolute;
        transform: rotate(130deg);
        clip: rect(0, 55px, 110px, 0);
        animation: loading-2 1s linear forwards;
    }
    .progress-circle .progress-left {
        position: absolute;
        border: 5px solid #ccc;
        border-radius: 50%;
    }
    .progress-circle .progress-right {
        position: absolute;
        border: 5px solid #1E90FF;
        border-radius: 50%;
    }
    .progress-circle .progress-bar p {
        color: #1E90FF;
        position: absolute;
        top: 50%;
        left: -20px;
        transform: translateY(-50%);
        font-size: 36px;
        font-family: Arial;
    }
    @keyframes loading-1 {
        0% {
            transform: rotate(-50deg);
        }
        100% {
            transform: rotate(130deg);
        }
    }
    @keyframes loading-2 {
        0% {
            transform: rotate(130deg);
        }
        100% {
            transform: rotate(210deg);
        }
    }

通過clip屬性控制CSS圖形呈現的圓心大小,進而實現圓形進度條的效果。

五、CSS圓形加載進度條

圓形加載進度條可以更加形象地呈現出當前頁面或者應用正在加載的狀態,增強了用戶交互體驗。如下代碼所示:

<div class="circle">
<div class="loader">
<div class="loader"&gt

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/243324.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:55
下一篇 2024-12-12 12:55

相關推薦

  • CSS sans字體家族

    CSS sans字體家族是一組基於CSS的無襯線字體,具有在不同設備和瀏覽器上保持一致的特性。本文將從優勢、使用、自定義等多個方面對CSS sans字體家族進行詳細介紹。 一、優勢…

    編程 2025-04-28
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變量讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性傳感器,能夠同時測量加速度和角速度。它由三個傳感器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • C語言貪吃蛇詳解

    一、數據結構和算法 C語言貪吃蛇主要運用了以下數據結構和算法: 1. 鏈表 typedef struct body { int x; int y; struct body *nex…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分布式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25

發表回復

登錄後才能評論