摘要:圓環(huán)進度條被應(yīng)用于各個場景,比如我們可以用來表示加載進度等。通常我們可以用 css3 的動畫去實現(xiàn)。 詳解 css3 實現(xiàn)圓環(huán)進度條簡單的畫一個圓環(huán),我們都知道如何使用 css 畫一個圓環(huán)。(利用border屬性、border-radius屬性) HTML 代碼: <div class="circle></div> CSS 代碼: .circle{ width:160px; height:160px; border:20px solid red; border-radius:50%; } 實現(xiàn)圓環(huán)進度條屬性,我們利用 css 畫扇形,然后結(jié)合 css3 的動畫屬性去實現(xiàn)。結(jié)合代碼去講解: 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>
首先確定圓環(huán)進度條最外層 css 的屬性: .circle-progress { position: relative; width: 200px; height: 200px; border: 1px solid #888; /*可選屬性,僅供測試使用*/ } 然后定位 content 以及 left 和 right 的屬性值: .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 屬性: .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 代碼: @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>圓環(huán)進度條</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>
|
|
來自: 新進小設(shè)計 > 《待分類》