用HTML、CSS、JS制作圆形进度条(无动画效果)

1、原理:

1、首先有一个圆:#333的纯净的圆,效果:

image.png

2、再来两个半圆,左边一个,右边一个将此黑色的圆盖住,效果:


image.png

此时将右半圆旋转30°,就会漏出底圆,效果:


image.png

此时再将右半圆的颜色改为yellow色,就会出现一个30/360的饼图:
image.png

然后我们再用一个比底圆小的圆去覆盖这个大圆不就可以出进度条效果了吗
image.png

好了,放代码,毕竟也不是我原创,我再照着作者的思路走一遍就是理解一下思路而已~

<style>
    /*支持IE9及以上*/
    .circle-bar {margin: 20px; font-size:200px; width: 1em; height: 1em; position: relative;  background-color: #333; }
    .circle-bar-left,.circle-bar-right { width: 1em; height: 1em;  }
    /*
        这里采用clip剪切了圆,实现左右两个半圆,右半圆在后面,因此在更上一层,
        clip的用法参考:http://www.w3school.com.cn/cssref/pr_pos_clip.asp
     */
    .circle-bar-right { clip:rect(0,auto,auto,.5em);background-color: red; }
    .circle-bar-left { clip:rect(0,.5em,auto,0); background-color: yellow;}

    .mask { width: 0.8em; height: 0.8em;  background-color: #fff;text-align: center;line-height: 0.2em; color:rgba(0,0,0,0.5); }
    .mask :first-child { font-size: 0.3em; height: 0.8em; line-height: 0.8em; display: block;  }
    /*所有的后代都水平垂直居中,这样就是同心圆了*/
    .circle-bar * {  position: absolute; top:0; right:0; bottom:0; left:0; margin:auto; }
    /*自身以及子元素都是圆*/
    .circle-bar, .circle-bar > * { border-radius: 50%; }

</style>
<body>
<div class="circle-bar">
    <div class="circle-bar-left"></div>
    <div class="circle-bar-right"></div>
     <!--遮罩层,显示百分比-->
    <div class="mask">
        <span class="percent">60%</span>
    </div>
</div>
<script>
    $(function(){
        var percent = parseInt($('.mask :first-child').text());
        var baseColor = $('.circle-bar').css('background-color');
        if( percent<=50 ){
            $('.circle-bar-right').css('transform','rotate('+(percent*3.6)+'deg)');
        }else {
            $('.circle-bar-right').css({
                'transform':'rotate(0deg)',
                'background-color':baseColor
            });
            $('.circle-bar-left').css('transform','rotate('+((percent-50)*3.6)+'deg)');
        }
    })
</script>

作者还使用了js原生去实现

//反正CSS3中的border-radius属性IE8是不支持了,所以这里就用新方法吧getElementsByClassName()走起
    window.onload = function(){

        var circleBar    = document.getElementsByClassName('circle-bar')[0];
        var percent      = parseInt(circleBar.getElementsByClassName('percent')[0].firstChild.nodeValue);
        var color        = circleBar.css('background-color');
        var left_circle  = circleBar.getElementsByClassName('circle-bar-left')[0];
        var right_circle = circleBar.getElementsByClassName('circle-bar-right')[0];

        if( percent <= 50 ) {
            var rotate = 'rotate('+(percent*3.6)+'deg)';
            right_circle.css3('transform',rotate);
        }else {
            var rotate = 'rotate('+((percent-50)*3.6)+'deg)';
            right_circle.css ('background-color',color);//背景色设置为进度条的颜色
            right_circle.css3('transform','rotate(0deg)');//右侧不旋转
            left_circle.css3 ('transform',rotate);//左侧旋转
        }
    }

    //封装了css3函数,主要是懒得重复书写代码,既然写了css3函数,顺便写个css吧,统一样式,好看一些
    Element.prototype.css = function(property,value){
        
        if ( value ) {
            //CSS中像background-color这样的属性,‘-’在JavaScript中不兼容,需要设置成驼峰格式
            var index = property.indexOf('-');
            if( index != -1 ) {
                var char = property.charAt(index+1).toUpperCase();
                property.replace(/(-*){1}/,char);
            }
            this.style[property] = value;
        }else{
            //getPropertyValue()方法参数类似background-color写法,所以不要转驼峰格式
            return window.getComputedStyle(this).getPropertyValue(property);
        }
    }

    //封装一个css3函数,用来快速设置css3属性
    Element.prototype.css3 = function(property,value){
        if( value ){
            property = capitalize(property.toLowerCase());
            this.style['webkit'+property] = value;
            this.style['Moz'+property] = value;
            this.style['ms'+property] = value;
            this.style['O'+property] = value;
            this.style[property.toLowerCase()] = value;
        }else{
            return window.getComputedStyle(this).getPropertyValue(
                    ('webkit'+property)||('Moz'+property)||('ms'+property)||('O'+property)||property);
                    //老实说,我不知道为什么要把不带浏览器标记的放在最后,既然都这么用,我也这么做吧。不过这样对现代浏览器来说可能并不好,判断次数变多了
        }
      
        //首字母大写
        function capitalize(word){
            return word.charAt(0).toUpperCase() + word.slice(1);
        }
    }

参考链接
查看代码效果

下一篇将讲实现动态的圆形进度条和动态条形进度条~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文约3500字,适合对Axure有一定了解的同学阅读,可跟着文中的教程同步动手操作,建议阅读20分钟。 写在前面...
    男良熊阅读 22,576评论 45 61
  • 年少的梦要好好珍藏, 因为长大后的迷茫会让你迷失了方向。 昨天的风景已成回忆, 抓住今天才能为明天导航。 没有永远...
    闲云听箫雨阅读 1,131评论 3 2
  • 闻清 01/02/2016 舍弃45道湾 奢求在拐角遇见太阳 圣洁的泉水 映出今晚的朦胧 月色娜人 一抹笑 世界为...
    闻清阅读 1,553评论 0 0
  • 每踏入一个新领域,大家都希望快速积累经验,那如何才能快速成长,积累更多的经验?这里要提的是一个大家都知道却又...
    silvia_shuang阅读 4,457评论 0 3
  • 人生有时候真讽刺,突然间想到我和他认识到现在已经四年了,从开始到结束,也只不过才见过四次面而已,就是这四次面已让我...
    今夕何夕_bdbc阅读 2,833评论 1 1