环形进度条
jQuery + CSS3
實現(xiàn)原理
原理非常的簡單,在這個方案中,最主要使用了CSS3的transform中的rotate和CSS3的clip兩個屬性。用他們來實現(xiàn)半圓和旋轉(zhuǎn)效果。
半環(huán)的實現(xiàn)
先來看其結(jié)構(gòu)。
html
<div class="pie_right"><div class="right"></div><div class="mask"><span>0</span>%</div> </div>css
.pie_right {width:200px; height:200px;position: absolute;top: 0;left: 0;background:#0cc; } .right {width:200px; height:200px;background:#00aacc;border-radius: 50%;position: absolute; top: 0;left: 0; } .pie_right, .right {clip:rect(0,auto,auto,100px); } .mask {width: 150px;height: 150px;border-radius: 50%;left: 25px;top: 25px; background: #FFF;position: absolute;text-align: center;line-height: 150px;font-size: 20px;background: #0cc;/* mask 是不需要剪切的,此處只是為了讓大家看到效果*/clip:rect(0,auto,auto,75px); }實現(xiàn)半圓還是挺簡單的,利用border-radius做出圓角,然后利用clip做出剪切效果,(clip使用方法,詳見站內(nèi)介紹),mask的部分是為了遮擋外面的容器,致使在視覺上產(chǎn)生圓環(huán)的效果:
旋轉(zhuǎn)的話,那更容易實現(xiàn)了,就是在CSS的right中加入.right { transform: rotate(30deg); }
這樣就可以看到一個半弧旋轉(zhuǎn)的效果了。
整環(huán)的實現(xiàn)
同樣道理,拼接左半邊圓環(huán)
<div class="circle"><div class="pie_left"><div class="left"></div></div><div class="pie_right"><div class="right"></div></div><div class="mask"><span>0</span>%</div> </div>.circle {width: 200px;height: 200px; position: absolute;border-radius: 50%;background: #0cc; } .pie_left, .pie_right {width: 200px; height: 200px;position: absolute;top: 0;left: 0; } .left, .right {display: block;width:200px; height:200px;background:#00aacc;border-radius: 50%;position: absolute;top: 0;left: 0;transform: rotate(30deg); } .pie_right, .right {clip:rect(0,auto,auto,100px); } .pie_left, .left {clip:rect(0,100px,auto,0); } .mask {width: 150px;height: 150px;border-radius: 50%;left: 25px;top: 25px;background: #FFF;position: absolute;text-align: center;line-height: 150px;font-size: 16px; }
圓環(huán)最終效果
Ok了,基本上我們的圓環(huán)已經(jīng)實現(xiàn)完成了,下面是加入JS效果。
首先,我們需要考慮的是,圓環(huán)的轉(zhuǎn)動幅度大小,是由圓環(huán)里面數(shù)字的百分比決定的,從0%-100%,那么圓弧被分成了每份3.6°;而在180°,也就是50%進(jìn)度之前,左側(cè)的半弧是不動的,當(dāng)超過50%,左邊的半弧才會有轉(zhuǎn)動顯示。
$(function() {$('.circle').each(function(index, el) {var num = $(this).find('span').text() * 3.6;if (num<=180) {$(this).find('.right').css('transform', "rotate(" + num + "deg)");} else {$(this).find('.right').css('transform', "rotate(180deg)");$(this).find('.left').css('transform', "rotate(" + (num - 180) + "deg)");};});});
?
轉(zhuǎn)載于:https://www.cnblogs.com/zshh/p/5577457.html
總結(jié)
- 上一篇: 常见的项目文件介绍
- 下一篇: angularJs中的发送请求例子