HTML代码:
<!DOCTYPE html>
<html>
<body>
<svg id="svg" width="200" height="200" viewport="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle r="90" cx="100" cy="100" fill="transparent" stroke-width="1em" stroke-dasharray="565.48" stroke-dashoffset="0" stroke="#666"></circle>
<circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="297.48" stroke-dashoffset="0" stroke="red" stroke-width="1em"></circle>
</svg>
Value: <input type="text" value="" id="value1" /><br/>
<input type="button" value="Stroke-percentage" onclick="generateProgress()" />
</body>
</html>
jQuery脚本:
$('#generateProgress').on('click', function(){
var val = parseInt($('#value1').val());
var $circle = $('#svg #bar');
if (isNaN(val)) {
val = 100;
}
else{
var r = $circle.attr('r');
var c = Math.PI*(r*2);
if (val < 0) { val = 0;}
if (val > 100) { val = 100;}
var pct = ((100-val)/100)*c;
$circle.css({ strokeDashoffset: pct});
$('#cont').attr('data-pct',val);
}
});
Javascript代码:
var value = document.getElementById("value1");
function generateProgress() {
var val = parseInt(value);
var circle = document.getElementById('bar');
if (isNaN(val)) {
val = 100;
}
else{
var r = circle.getAttribute('r');
var c = Math.PI*(r*2);
if (val < 0) { val = 0;}
if (val > 100) { val = 100;}
var pct = ((100-val)/100)*c;
circle.style.strokeDashoffset = pct;
}
}
检查[这](https://codepen.io/JMChristensen/笔/ Ablch) – Durga