用HTML、CSS、JS制作圓形進(jìn)度條(無(wú)動(dòng)畫(huà)效果)

1、原理:

1、首先有一個(gè)圓:#333的純凈的圓,效果:

image.png

2、再來(lái)兩個(gè)半圓,左邊一個(gè),右邊一個(gè)將此黑色的圓蓋住,效果:


image.png

此時(shí)將右半圓旋轉(zhuǎn)30°,就會(huì)漏出底圓,效果:


image.png

此時(shí)再將右半圓的顏色改為yellow色,就會(huì)出現(xiàn)一個(gè)30/360的餅圖:
image.png

然后我們?cè)儆靡粋€(gè)比底圓小的圓去覆蓋這個(gè)大圓不就可以出進(jìn)度條效果了嗎
image.png

好了,放代碼,畢竟也不是我原創(chuàng),我再照著作者的思路走一遍就是理解一下思路而已~

<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剪切了圓,實(shí)現(xiàn)左右兩個(gè)半圓,右半圓在后面,因此在更上一層,
        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原生去實(shí)現(xiàn)

//反正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);//背景色設(shè)置為進(jìn)度條的顏色
            right_circle.css3('transform','rotate(0deg)');//右側(cè)不旋轉(zhuǎn)
            left_circle.css3 ('transform',rotate);//左側(cè)旋轉(zhuǎn)
        }
    }

    //封裝了css3函數(shù),主要是懶得重復(fù)書(shū)寫(xiě)代碼,既然寫(xiě)了css3函數(shù),順便寫(xiě)個(gè)css吧,統(tǒng)一樣式,好看一些
    Element.prototype.css = function(property,value){
        
        if ( value ) {
            //CSS中像background-color這樣的屬性,‘-’在JavaScript中不兼容,需要設(shè)置成駝峰格式
            var index = property.indexOf('-');
            if( index != -1 ) {
                var char = property.charAt(index+1).toUpperCase();
                property.replace(/(-*){1}/,char);
            }
            this.style[property] = value;
        }else{
            //getPropertyValue()方法參數(shù)類似background-color寫(xiě)法,所以不要轉(zhuǎn)駝峰格式
            return window.getComputedStyle(this).getPropertyValue(property);
        }
    }

    //封裝一個(gè)css3函數(shù),用來(lái)快速設(shè)置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);
                    //老實(shí)說(shuō),我不知道為什么要把不帶瀏覽器標(biāo)記的放在最后,既然都這么用,我也這么做吧。不過(guò)這樣對(duì)現(xiàn)代瀏覽器來(lái)說(shuō)可能并不好,判斷次數(shù)變多了
        }
      
        //首字母大寫(xiě)
        function capitalize(word){
            return word.charAt(0).toUpperCase() + word.slice(1);
        }
    }

參考鏈接
查看代碼效果

下一篇將講實(shí)現(xiàn)動(dòng)態(tài)的圓形進(jìn)度條和動(dòng)態(tài)條形進(jìn)度條~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文約3500字,適合對(duì)Axure有一定了解的同學(xué)閱讀,可跟著文中的教程同步動(dòng)手操作,建議閱讀20分鐘。 寫(xiě)在前面...
    男良熊閱讀 22,783評(píng)論 45 61
  • 年少的夢(mèng)要好好珍藏, 因?yàn)殚L(zhǎng)大后的迷茫會(huì)讓你迷失了方向。 昨天的風(fēng)景已成回憶, 抓住今天才能為明天導(dǎo)航。 沒(méi)有永遠(yuǎn)...
    閑云聽(tīng)簫雨閱讀 198評(píng)論 3 2
  • 聞清 01/02/2016 舍棄45道灣 奢求在拐角遇見(jiàn)太陽(yáng) 圣潔的泉水 映出今晚的朦朧 月色娜人 一抹笑 世界為...
    聞清閱讀 257評(píng)論 0 0
  • 每踏入一個(gè)新領(lǐng)域,大家都希望快速積累經(jīng)驗(yàn),那如何才能快速成長(zhǎng),積累更多的經(jīng)驗(yàn)?這里要提的是一個(gè)大家都知道卻又...
    silvia_shuang閱讀 1,092評(píng)論 0 3
  • 人生有時(shí)候真諷刺,突然間想到我和他認(rèn)識(shí)到現(xiàn)在已經(jīng)四年了,從開(kāi)始到結(jié)束,也只不過(guò)才見(jiàn)過(guò)四次面而已,就是這四次面已讓我...
    今夕何夕_bdbc閱讀 410評(píng)論 1 1

友情鏈接更多精彩內(nèi)容