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/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

发表回复

登录后才能评论