1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>文字与背景反色的进度条</title> <style> .wrapper { position: relative; background-color: #e4f3ff; width: 125px; height: 20px; border-radius: 6px; overflow: hidden; }
.font { position: absolute; left: 0%; top: 0%; z-index: 2; width: 100%; height: 100%; background-image: linear-gradient( to right, #ffffff 0%, #ffffff 0%, #555555 0%, #555555 100% ); background-clip: text; color: transparent; border-radius: 6px; text-align: center; }
.bar { position: absolute; left: 0%; top: 0%; z-index: 1; width: 0%; height: 100%; border-radius: 6px; background-color: #2c96e0; } </style> </head>
<body> <div> <input class="input" type="number" min="0" max="100" value="0" /> </div>
<div class="wrapper"> <div class="font">0%</div> <div class="bar"></div> </div>
<script> const $input = document.getElementsByClassName("input")[0]; const $font = document.getElementsByClassName("font")[0]; const $bar = document.getElementsByClassName("bar")[0];
function setPercent(percent) { $font.style.backgroundImage = `linear-gradient(to right, #ffffff 0%, #ffffff ${percent}%, #555555 ${percent}%, #555555 100%)`; $font.innerText = `${percent}%`; $bar.style.width = `${percent}%`; }
$input.onchange = (ev) => { let percent = ev.target.value; if (percent < 0) percent = ev.target.value = 0; if (percent > 100) percent = ev.target.value = 100; setPercent(percent); }; </script> </body> </html>
|