摘要:圓環進度條被應用於各個場景,好比咱們能夠用來表示加載進度等。一般咱們能夠用 css3 的動畫去實現。css
簡單的畫一個圓環,咱們都知道如何使用 css 畫一個圓環。(利用border屬性、border-radius屬性)html
HTML 代碼:css3
<div class="circle></div>
CSS 代碼:測試
.circle{ width:160px; height:160px; border:20px solid red; border-radius:50%; }
實現圓環進度條屬性,咱們利用 css 畫扇形,而後結合 css3 的動畫屬性去實現。結合代碼去講解:flex
HTML代碼:動畫
<div class="circle-progress"> <div class="content left"> <div class="circle left-circle"></div> </div> <div class="content right"> <div class="circle right-circle"></div> </div> </div>
首先肯定圓環進度條最外層 css 的屬性:spa
.circle-progress { position: relative; width: 200px; height: 200px; border: 1px solid #888; /*可選屬性,僅供測試使用*/ }
而後定位 content 以及 left 和 right 的屬性值:code
.content { position: absolute; top: 0; width: 100px; height: 200px; margin: 0; padding: 0; overflow: hidden; } .left { left: 0; } .right { right: 0; }
最後肯定 left-circle 和 right-circle 屬性:orm
.circle { position: absolute; margin: 0; width: 160px; height: 160px; border-radius: 50%; border: 20px solid transparent; transform: rotate(135deg); } .left-circle { left: 0; border-top-color: green; border-left-color: green; animation: circle-left 5s linear infinite; } .right-circle { right: 0; border-bottom-color: green; border-right-color: green; animation: circle-right 5s linear infinite; }
動畫 css3 代碼:htm
@keyframes circle-right { 0% { transform: rotate(135deg); } 50%, 100% { transform: rotate(315deg); } } @keyframes circle-left { 0%, 50% { transform: rotate(135deg); } 100% { transform: rotate(315deg); } }
完整的代碼:
<!DOCTYPE html> <html> <head> <title>圓環進度條</title> <meta charset="utf-8" name="viewport" content="width=device-width;initial-scale=1.0" /> <style type="text/css"> html, body { width: 100%; height: 100%; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; } .circle-progress { position: relative; width: 200px; height: 200px; border: 1px solid #888; } .content { position: absolute; top: 0; width: 100px; height: 200px; margin: 0; padding: 0; overflow: hidden; } .left { left: 0; } .right { right: 0; } .circle { position: absolute; margin: 0; width: 160px; height: 160px; border-radius: 50%; border: 20px solid transparent; transform: rotate(135deg); } .left-circle { left: 0; border-top-color: green; border-left-color: green; animation: circle-left 5s linear infinite; } .right-circle { right: 0; border-bottom-color: green; border-right-color: green; animation: circle-right 5s linear infinite; } @keyframes circle-right { 0% { transform: rotate(135deg); } 50%, 100% { transform: rotate(315deg); } } @keyframes circle-left { 0%, 50% { transform: rotate(135deg); } 100% { transform: rotate(315deg); } } </style> </head> <body> <div class="circle-progress"> <div class="content left"> <div class="circle left-circle"></div> </div> <div class="content right"> <div class="circle right-circle"></div> </div> </div> </body> </html>