实现环形进度条效果【一】
生活随笔
收集整理的這篇文章主要介紹了
实现环形进度条效果【一】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
好基友扔過來一張效果圖,簡單分析下,一起看看如何實現它吧。
- 一個半環形用于表示 0 - 100%。
- 半環形開頭有一個圓點作為修飾。
- 半環形兩端需要呈現為圓角。
通過 div 實現
先畫一個長方形。
<div class="graph"></div>
.graph {
width: 200px;
height: 100px;
border: 20px solid rgb (58, 215, 217);
}
接下來把長方形轉換為半環形。
.graph {
width: 200px;
height: 100px;
border: 20px solid rgb (58, 215, 217);
+ border-radius: 0 0 200px 200px;
+ border-top: none;
}
給環形開頭添加圓點修飾,實際等于添加到長方形的左上角。
<div class="graph">
+ <div class="dot"></div>
</div>
.graph {
+ position: relative;
width: 200px;
height: 100px;
border: 20px solid rgb (58, 215, 217);
border-radius: 0 0 200px 200px;
border-top: none;
}
+.dot {
+ position: absolute;
+ top: 5px;
+ left: -15px;
+ z-index: 9999;
+ width: 12px;
+ height: 12px;
+ border-radius: 50%;
+ background-color: #fff;
+ transform-origin: center top;
+}
環形有了,如何實現進度條效果呢?讓半環形旋轉,并隱藏超出部分。可以給元素添加 transform 使其旋轉。
.graph {
position: relative;
width: 200px;
height: 100px;
border: 20px solid rgb (58, 215, 217);
border-radius: 0 0 200px 200px;
border-top: none;
+ transform: rotate (150deg);
}
半環形并沒有根據中心點旋轉,通過 transform-origin: center top 設置原點為中間頂部,即半環形的中心。
.graph {
position: relative;
width: 200px;
height: 100px;
border: 20px solid rgb (58, 215, 217);
border-radius: 0 0 200px 200px;
border-top: none;
+ transform-origin: center top;
transform: rotate (150deg);
}
給環形添加一個父元素,并設置超出部分隱藏。
<div class="graph-wrapper">
<div class="graph">
<div class="dot"></div>
</div>
</div>
.graph-wrapper {
width: 200px;
height: 100px;
overflow: hidden;
transform: rotate (90deg);
}
動態設置環形元素的 rotate 角度實就可以實現進度條效果了。0 - 100% 對應 180 - 360deg。
可以通過 JavaScript 設置半環形的進度。
function calculateValue(range, percentage) {
const [start, end] = range
const result = start + (end - start) * percentage / 100;
return result;
}
function renderGraph(percentage) {
const deg = calculateValue ([180, 360], percentage);
const el = document.querySelector ('.graph')
el.style.transform = `rotate (${deg} deg)`
}
renderGraph (30) // 30%
總結
我們先使用 div 畫了一個長方形,添加 border 與 border-radius 屬性使其轉換為半環形,又通過 transform 屬性使半環形可以旋轉。接下來給半環形套了一層元素,超出部分隱藏,以實現進度條效果。
在博文開頭處,我們對效果圖進行了分析。其中,第 3 點 “半環形兩端需要呈現為圓角” 還沒有被支持。限于篇幅,將在接下來的博文中實現,最終效果如下圖。
總結
以上是生活随笔為你收集整理的实现环形进度条效果【一】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何配置CentOS 7网络
- 下一篇: 自定义springboot-starte