怎么實(shí)現(xiàn)這樣一個(gè)圓環(huán)進(jìn)度條的效果呢决乎,可以使用canvas、svg佃迄、GIF等等方式泼差,今天我們來說下使用css3怎么來實(shí)現(xiàn)贵少。
實(shí)現(xiàn)思路
圓環(huán)很簡單,一行cssborder-radius:50%
即可實(shí)現(xiàn)堆缘,而且沒有兼容性問題滔灶,什么,你說IE吼肥,讓它滾...
我們這里需要三個(gè)圓環(huán)录平,一個(gè)整的,兩個(gè)半的缀皱。大概畫了下圖
這里半圓環(huán)我使用了clip
進(jìn)行裁剪斗这,主要代碼如下,
.left{
width: 200px;
height: 200px;
border-radius: 50%;
border: 10px solid lightblue;
position:absolute;
top: -10px; /* 10的原因是因?yàn)檫吙蚴?0個(gè)像素 */
right: -10px;
clip: rect(0 100px 200px 0); /* 上面為0 右邊到100px
下面200px 左邊到0 這個(gè)區(qū)域的我們裁剪出來 */
}
右邊類似只是裁剪位置改了
.right{
width: 200px;
height: 200px;
border-radius: 50%;
border: 10px solid lightblue;
position:absolute;
top: -10px; /* 10的原因是因?yàn)檫吙蚴?0個(gè)像素 */
right: -10px;
clip: rect(0 200px 200px 100px); /* 位置更改,計(jì)算可以參考上圖 */
}
完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div{
box-sizing: border-box;
}
.box{
width: 200px;
height: 200px;
position: relative;
background-color: #ccc;
border-radius: 50%;
left: 40%;
top: 200px;
}
.num{
position: absolute;
top: 50%;
left: 50%;
background: #fff;
border-radius: 50%;
width: 180px;
height: 180px;
transform: translate(-50%, -50%);
text-align: center;
line-height: 180px;
font-size: 32px;
}
.clip{
width: 200px;
height: 200px;
position: absolute;
border: 10px solid #ccc;
border-radius: 50%;
clip: rect(0, 200px, 200px, 100px);
}
.left{
width: 200px;
height: 200px;
position: absolute;
border: 10px solid lightblue;
border-radius: 50%;
clip: rect(0 100px 200px 0);
top: -10px;
left: -10px;
}
.right{
width: 200px;
height: 200px;
position: absolute;
border: 10px solid lightblue;
border-radius: 50%;
clip: rect(0 200px 200px 100px);
top: -10px;
left: -10px;
}
.width-none{
width: 0;
}
.auto{
clip: auto;
}
</style>
</head>
<body>
<div class="box">
<div class="clip">
<div class="left"></div>
<div class="right width-none"></div>
</div>
<div class="num">
</div>
</div>
<script >
let clip = document.querySelector('.clip'),
left = document.querySelector('.left'),
right = document.querySelector('.right'),
num = document.querySelector('.num'),
rotate = 0;
let loop = setInterval(() => {
if(rotate >= 100){
rotate = 0;
right.classList.add('width-none');
clip.classList.remove('auto');
} else if(rotate > 50){
right.classList.remove('width-none');
clip.classList.add('auto');
}
rotate++;
left.style.transform = 'rotate('+ 3.6*rotate + 'deg)';
num.innerHTML = `${rotate}%`
},100)
</script>
</body>
</html>
簡單說下上面的代碼
1啤斗、首先隱藏了右半圓表箭,這是因?yàn)槲覀冃枰D(zhuǎn)的是左半圓,我們可以等左半圓轉(zhuǎn)到右邊圓的位置再顯示右 邊钮莲,就是等到旋轉(zhuǎn)到180度的時(shí)候免钻。
2、同時(shí)我們看到主圓添加了clip: rect(0, 200px, 200px, 100px);
裁剪樣式崔拥,這是因?yàn)槟J(rèn)我們 進(jìn)度是0%的极舔,我們只顯示右邊的話才能隱藏左邊,但是我們右邊不是隱藏的嗎链瓦?那顯示它干嘛呢拆魏,因?yàn)? 旋轉(zhuǎn)左邊的時(shí)候就看到轉(zhuǎn)到右邊的圓了。稍微有點(diǎn)繞慈俯,請結(jié)合代碼稽揭,多多理解
3、等到左邊旋轉(zhuǎn)了180我們需要將右邊顯示出來肥卡,并且將box
元素的裁剪設(shè)置為默認(rèn)值,就是不裁剪事镣,這 這樣才能顯示完整的左右兩個(gè)圓步鉴。
4、最后我們使用js來控制旋轉(zhuǎn)角度并將百分比顯示在頁面上
寫在最后
如果上面的解釋看不明白璃哟,索性就不要看了氛琢,把代碼放在本地調(diào)試下,自己去理解随闪。
別鉆牛角尖阳似,代碼是最好的語言。
使用clip才實(shí)現(xiàn)圓環(huán)進(jìn)度還是很簡單的铐伴,還不需要考慮兼容性撮奏,關(guān)于clip可以看張鑫旭大神的日志