CSS3實現圓形進度條

來源:互聯網
上載者:User

標籤:

介紹

  閑來無事,去了CSS3Plus網站逛了逛,發現了一個很有意思的實現--css3實現進度條。粗略看了下代碼,發現原理其實很簡單,不難理解。

現在在此講述下原理並實現一個1s更新的進度條。

  技術細節是這樣的:進度條由兩個半圓環組成,首先我們的任務是實現左右兩個半圓環。圓環可以用border-radius實現,這樣就意味著該方

法不相容IE8.可以使用clip來完成對整圓環的剪下;使用rotate函數完成圓環的旋轉,通過設定兩個半圓環的旋轉角度來實現不同進度值的顯示。

  clip屬性是css2屬性,所有的瀏覽器都支援該屬性。對於clip有幾個要點需要注意:首先,使用clip屬性的元素必須是絕對位置或者固定定位的

元素,也就是說必須脫離文檔流;其次,clip可以使用的函數目前只有rect(top,right,bottom,left),該函數傳遞4個值,其中top為裁剪地區距離

元素頂端的距離,設為auto時預設為0,right為裁剪地區距離元素左端(左邊框)的值,auto時預設為元素的最右端,bottom為裁剪地區距離元

素頂端的值,auto時預設為元素最底端,left為距離元素左邊的距離,auto時預設為0.

實現

  

        .wrap{position: relative;width: 200px;height:200px;border-radius: 50%;background: #CCFFFF;text-align: center;}        .right-part{width:200px;height: 200px;position: absolute;clip:rect(0px,auto,auto,100px)}        .right{width: 200px;height:200px;position: absolute;border-radius: 50%;background: #003366;            clip:rect(0px,auto,auto,100px);}        .r-shadow{width: 150px;height:150px;border-radius: 50%;background: #fff;position: absolute;            left:25px;top:25px;}        .left-part{width:200px;height: 200px;position: absolute;clip:rect(0px,100px,auto,0px)}        .left{width: 200px;height:200px;position: absolute;border-radius: 50%;background: #003366;            clip:rect(0px,100px,auto,0px);}        .l-shadow{width: 150px;height:150px;border-radius: 50%;background: #fff;position: absolute;            left:25px;top:25px;}        #desc{display:inline-block;width:200px;height:200px;line-height: 200px;font-size: 56px;position: absolute;            left:0;}
     <div class="wrap">        <div class="right-part">            <div class="right" id="right"></div>            <div class="r-shadow"></div>        </div>        <div class="left-part">            <div class="left" id="left"></div>            <div class="l-shadow"></div>        </div>        <span id="desc">ad</span>    </div>
      function progressBar(percentage){            var p = Math.round(percentage * 100);            var deg = p * 3.6;            var right = document.getElementById("right"),                    left = document.getElementById("left"),                    desc = document.getElementById("desc");            if(p > 100 || p < 0) p = 100;            if(deg <= 180){                right.style.cssText = "transform:rotate("+(deg-180)+"deg);"                left.style.cssText = "background:#CCFFFF;"            }else{                right.style.cssText = "transform:none;"                left.style.cssText = "background:#003366;transform:rotate("+(deg-360)+"deg);"            }            if(desc.innerText){                desc.innerText = p+"%"            }            if(desc.textContent){                desc.textContent = p+"%";            }        }        var g = 0;        setTimeout(function _a(){            if(g>1)return;            progressBar(g);            g += 0.1            setTimeout(_a,1000)        },1000)

 

CSS3實現圓形進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.