淺談一下關於使用css3來製做圓環進度條

最近PC端項目要作一個這樣的頁面出來,其餘的都很簡單,關鍵在於百分比的圓環效果。我最初打算是直接使用canvas來實現的,由於canvas實現一個圓是很簡便的。css

下面貼出canvas實現圓環的代碼,有須要的能夠拿去嘗試,由於今天主要是講css3的方法,canvas我就很少解釋了html

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
</head>  
<body>  
    <canvas id="canvas" width="200" height="200"></canvas>  
    <script>  
            var canvas = document.getElementById('canvas');  
            var process = 0;  
            var context = canvas.getContext('2d');  
  
            // 畫外圓  
            context.beginPath();  
            context.arc(100, 100, 80, 0, Math.PI*2);  
            context.closePath();  
            context.fillStyle = '#666';  
            context.fill();  
  			drawCricle(context, process);
            
            function drawCricle(ctx, percent){  
                // 進度環  
                ctx.beginPath();  
                ctx.moveTo(100, 100);    
                ctx.arc(100, 100, 80, Math.PI * 1.5, Math.PI * (1.5 + 2 * percent / 100 ));  
                ctx.closePath();  
                ctx.fillStyle = 'red';  
                ctx.fill();  
  
                // 內圓
                ctx.beginPath();  
                ctx.arc(100, 100, 75, 0, Math.PI * 2);  
                ctx.closePath();  
                ctx.fillStyle = 'white';  
                ctx.fill();  
  
                // 填充文字  
                ctx.font= "bold 30px microsoft yahei";   
                ctx.fillStyle = "black";  
                ctx.textAlign = "center";    
                ctx.textBaseline = 'middle';    
                ctx.moveTo(100, 100);    
                ctx.fillText(process + '%', 100, 100);  
            }  
   
    </script>  
</body>  
</html>  

後來之因此是由於沒有去使用canvas去實現是由於產品和我說這個任務之後會很是多,我問會不會超過99個?他說有可能,你上限設999吧。jquery

要是999個canvas的圓環去渲染。。。上百個都夠嗆了吧,無奈之下只好去選用css3,至少這樣會快不少。可是css貌似沒有直接畫個進度環的方法吧。css3

我稍後會貼出完整的代碼,這裏先講述一下大概的結構。canvas

要實現進度條的樣式用css的話咱們能想到的方法好像只有用大小不一樣的圓去疊加,若是是要那種動畫不停旋轉的loading效果那太簡單了,我會很開心的,惋惜。。學習

 

首先咱們要來個背景圓,好比這樣動畫

接着來個內圓去遮罩spa

有點像樣子了,那咱們接下來就是重點了,如何讓它跟着百分好比動態顯示改變。js是必須的,我先講樣式3d

下一步咱們要建立兩個半圓好比這樣orm

css實現半圓的方法有不少,你們能夠自行百度,我是採用clip:rect();這個方法去裁剪成半圓的 ,作完這些 咱們就只須要用js去控制左右兩邊的半圓rotate()的旋轉角度就行了。

 

 記得最後把左右兩個半圓的顏色統一一下就能夠了,下面我會貼出源代碼,你們引入一個jq就能夠直接用了

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			.circle {
				width: 200px;
				height: 200px;
				position: relative;
				border-radius: 50%;
				background: red;
			}
				
			.clip_left,.clip_right{
				width:200px;
				height:200px;
				position: absolute;
				top: 0px;left: 0px;
			}
			.circle_left, .circle_right{
				width:200px;
				height:200px;
				position: absolute;
				border-radius: 50%;
				top: 0px;left: 0px;
				background: green;
			}
			/*出於展現用的改變背景色*/
			/*.circle_left{
				background: green;
			}
			.circle_right{
				background: lightblue;
			}*/
			.circle_right,.clip_right {
				clip:rect(0,auto,auto,100px);
			}
			.circle_left , .clip_left{
				clip:rect(0,100px,auto,0);
			}
				
			/*
			*當top和left取值爲auto時,至關於0
			*當bottom和right取值爲auto時,至關於100%
			*/
			.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;
			}

		</style>
	</head>
	<body>
		<!--背景圓-->
		<div class="circle">
			<!--左半邊圓-->
			<div class="circle_left">
				<div class="clip_left">
					
				</div>
			</div>
			<!--右半邊圓-->
			<div class="circle_right">
				<div class="clip_right"></div>
			</div>
			<div class="mask">
				<span>10</span>%
			</div>
		</div>
		<script src="../jquery-2.2.3.min.js"></script>
		<script>
			$(function(){
				if( $('.mask span').text() <= 50 ){
					$('.circle_right').css('transform','rotate('+($('.mask span').text()*3.6)+'deg)');
				}else{
					$('.circle_right').css({
						'transform':'rotate(0deg)',
						"background":"red"
					});
					$('.circle_left').css('transform','rotate('+(($('.mask span').text()-50)*3.6)+'deg)');
				}
			})
		</script>
	</body>
</html>

隨筆很潦草,有什麼不足的地方還請你們多多交流學習。  

郵箱:506117150@qq.com

相關文章
相關標籤/搜索