1. 静态效果
10%大小
40%大小
80%大小
99%大小
代码
CSS代码:
.pie-simple { width: 128px; height: 128px; background-color: white; border-radius: 50%; overflow: hidden; } .pie-left, .pie-right { width: 50%; height: 100%; float: left; position: relative; overflow: hidden; } .pie-left::before, .pie-right::before, .pie-right::after { content: ''; position: absolute; width: 100%; height: 100%; background-color: teal; } .pie-left::before { left: 100%; transform-origin: left; transform: rotate(calc(3.6deg * (var(--percent) - 50))); opacity: calc(99999 * (var(--percent) - 50)); } .pie-right::before { right: 100%; transform-origin: right; transform: rotate(calc(3.6deg * var(--percent))); } .pie-right::after { opacity: calc(99999 * (var(--percent) - 50)); }
HTML代码:
<div class="pie-item"> <p>10%大小</p> <div class="pie-simple" style="--percent: 10;"> <div class="pie-left"></div> <div class="pie-right"></div> </div> </div> <div class="pie-item"> <p>40%大小</p> <div class="pie-simple" style="--percent: 40;"> <div class="pie-left"></div> <div class="pie-right"></div> </div> </div> <div class="pie-item"> <p>80%大小</p> <div class="pie-simple" style="--percent: 80;"> <div class="pie-left"></div> <div class="pie-right"></div> </div> </div> <div class="pie-item"> <p>99%大小</p> <div class="pie-simple" style="--percent: 99;"> <div class="pie-left"></div> <div class="pie-right"></div> </div> </div>
2. 动画效果(单循环-左,交替循环-右)
代码
CSS代码(单循环):
.pie-spin { width: 128px; height: 128px; background-color: white; border-radius: 50%; overflow: hidden; } .pie-spin-left, .pie-spin-right { width: 50%; height: 100%; float: left; position: relative; overflow: hidden; } .pie-spin-left::before, .pie-spin-right::before, .pie-spin-left::after, .pie-spin-right::after { content: ''; position: absolute; width: 100%; height: 100%; background-color: teal; } .pie-spin-left { opacity: 1; animation: second-half-show 1.6s steps(1, end) infinite; } .pie-spin-left::before { left: 100%; transform-origin: left; animation: spin 1.6s .8s infinite linear; } .pie-spin-right::before { right: 100%; transform-origin: right; animation: spin 1.6s infinite linear; } .pie-spin-right::after { animation: second-half-show 1.6s steps(1, end) infinite; opacity: 1; } .pie-spin-left::after { animation: second-half-hide 1.6s steps(1, end) infinite; opacity: 0; } @keyframes spin { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } } @keyframes second-half-hide { 0% { opacity: 1; } 50%, 100% { opacity: 0; } } @keyframes second-half-show { 0% { opacity: 0; } 50%, 100% { opacity: 1; } }
HTML代码(单循环):
<div class="pie-spin"> <div class="pie-spin-left"></div> <div class="pie-spin-right"></div> </div>
CSS代码(交替循环):
.pie-spin2 { width: 128px; height: 128px; background-color: white; border-radius: 50%; overflow: hidden; } .pie-spin2-left, .pie-spin2-right { width: 50%; height: 100%; float: left; position: relative; overflow: hidden; } .pie-spin2-left::before, .pie-spin2-right::before { content: ''; position: absolute; width: 100%; height: 100%; background-color: teal; } .pie-spin2-left::before { left: 100%; transform-origin: left; animation: spinWait2 3.2s infinite linear; } .pie-spin2-right::before { right: 100%; transform-origin: right; animation: spinWait1 3.2s infinite linear; } @keyframes spinWait1 { 0% { transform: rotate(0deg); } 25%, 50% { transform: rotate(180deg); } 75%, 100% { transform: rotate(360deg); } } @keyframes spinWait2 { 0%, 25% { transform: rotate(0deg); } 50%, 75% { transform: rotate(180deg); } 100% { transform: rotate(360deg); } }
HTML代码(交替循环):
<div class="pie-spin2"> <div class="pie-spin2-left"></div> <div class="pie-spin2-right"></div> </div>
3. 3D原理示意
饼图进度(0-100):