iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >使用 css3怎么实现一个圆形进度条
  • 554
分享到

使用 css3怎么实现一个圆形进度条

2023-06-08 17:06:08 554人浏览 薄情痞子
摘要

使用 css3怎么实现一个圆形进度条?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。html代码<div class="progressbar&qu

使用 css3怎么实现一个圆形进度条?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

html代码

<div class="progressbar">    <div class="left-container">        <div class="left-circle"></div>    </div>    <div class="right-container">        <div class="right-circle"></div>    </div></div>

CSS代码:

.progressbar{    position: relative;    margin: 100px auto;    width: 100px;    height: 100px;    border: 20px solid #ccc;    border-radius: 50%;}.left-container,.right-container{    position: absolute;    width: 70px;    height: 140px;    top:-20px;    overflow: hidden;    z-index: 1;}.left-container{    left: -20px;}.right-container{    right: -20px;}.left-circle,.right-circle{    position: absolute;    top:0;    width: 100px;    height: 100px;    border:20px solid transparent;       border-radius: 50%;    transfORM: rotate(-135deg);    transition: all .5s linear;    z-index: 2;}.left-circle{    left: 0;    border-top-color: 20px solid blue;    border-left-color: 20px solid blue;}.right-circle{    border-right-color: 20px solid blue;    border-bottom-color: 20px solid blue;    right: 0;}

二:控制进度条的js

为了使进度条的逻辑更健壮,js部分的实现需要考虑四中情况:

基础值个更改后的值在同在右边进度,

基础值在右边,更改后的值在左边,

基础值更改后的值同在左边,

基础值在左边,更改后的值在右边。

不管在那种情况下,核心需要考虑只有两点:角度的变化和使用时间的多少。

第一种情况下,比较简单,可以很简单计算出角度的变化和使用时间的多少。首先,需要设定改变整个半圆的所需的时间值。设定之后,只要根据比例计算出改变的角度所需要的时间值即刻。代码如下:

time = (curPercent - oldPercent)/50 * baseTime;     //确定时间值为正     curPercent - oldPercent > 0 ? '' : time = - time;     deg = curPercent/50*180-135;

第二种情况,稍微麻烦一点。因为中间有一个从右边进度,到左边进度的过渡。为了让进度顺畅的改变,这里我们需要使用定时器,在改变完右边的部分之后,再修改左边的部分。代码如下:

//设置右边的进度  time = (50 - oldPercent)/50 * baseTime;deg = (50 - oldPercent)/50*180-135;$rightBar .css({    transform: 'rotate('+ deg+ 'deg)',    transition : 'all '+ time + 's linear'})//延时设置左边进度条的改变setTimeout(function(){    time = (curPercent - 50)/50;    deg = (curPercent - 50)/50*180 -135;    $leftBar.css({        transform: 'rotate('+ deg+ 'deg)',        transition : 'all '+ time + 's linear'    })}, Math.floor(time*1000));000));

第三种情况和第四种情况同前面情况类似,这里不再讨论。

完整的控制进度条的函数的代码如下(使用Jquery实现):

    function setProgessbar(oldPercent, curPercent){        var $leftBar = $('#left-bar');        var $rightBar = $('#right-bar');        //将传入的参数转换,允许字符串表示的数字        oldPercent =  + oldPercent;        curPercent =  + curPercent;        //检测参数,如果不是number类型或不在0-100,则不执行        if(typeof oldPercent ==='number' && typeof curPercent ==='number'            && oldPercent >= 0 && oldPercent <= 100 && curPercent <= 100 && curPercent >= 0){                var baseTime = 1;    //默认改变半圆进度的时间,单位秒               var time = 0;    //进度条改变的时间            var deg = 0;     //进度条改变的角度            if(oldPercent > 50){//原来进度大于50                if(curPercent>50){                    //设置左边的进度                    time = (curPercent - oldPercent)/50 * baseTime;                    //确定时间值为正                    curPercent - oldPercent > 0 ? '' : time = - time;                    deg = curPercent/50*180-135;                    $leftBar .css({                        transform: 'rotate('+ deg+ 'deg)',                        transition : 'all '+ time + 's linear'                    })                    }else{                    //设置左边的进度                    time = (oldPercent - 50)/50 * baseTime;                    deg = (oldPercent - 50)/50*180-135;                    $leftBar .css({                        transform: 'rotate('+ deg+ 'deg)',                        transition : 'all '+ time + 's linear'                    })                    //延时设置右边进度条的改变                    setTimeout(function(){                        time = (50 - curPercent)/50;                        deg = (50 - curPercent)/50*180 -135;                        $rightBar.css({                            transform: 'rotate('+ deg+ 'deg)',                            transition : 'all '+ time + 's linear'                        })                    }, Math.floor(time*1000));                }            }else{//原来进度小于50时                    if(curPercent>50){                    //设置右边的进度                    time = (50 - oldPercent)/50 * baseTime;                    deg = (50 - oldPercent)/50*180-135;                    $rightBar .css({                        transform: 'rotate('+ deg+ 'deg)',                        transition : 'all '+ time + 's linear'                    })                    //延时设置左边进度条的改变                    setTimeout(function(){                        time = (curPercent - 50)/50;                        deg = (curPercent - 50)/50*180 -135;                            $leftBar.css({                            transform: 'rotate('+ deg+ 'deg)',                            transition : 'all '+ time + 's linear'                        })                    }, Math.floor(time*1000));                }else{                    //设置右边的进度                    time = (curPercent - oldPercent)/50 * baseTime;                    //确定时间值为正                    curPercent - oldPercent > 0 ? '' : time = - time;                    deg = curPercent/50*180-135;                    $rightBar .css({                        transform: 'rotate('+ deg+ 'deg)',                        transition : 'all '+ time + 's linear'                    })                    }                return true;            }        }else{            return false;        }    }

关于使用 css3怎么实现一个圆形进度条问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网精选频道了解更多相关知识。

--结束END--

本文标题: 使用 css3怎么实现一个圆形进度条

本文链接: https://www.lsjlt.com/news/254816.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作