在页面中引入progresscircle.css和jquery以及progresscircle.js文件。
<link href="css/progresscircle.css" rel="stylesheet"> <scriptt src="jquery.min.js"></scriptt> <scriptt src="progresscircle.js"></scriptt>
该jquery圆形进度条的基本html结构如下:
<div class="circlechart" data-percentage="-20"> -20% </div> <div class="circlechart" data-percentage="50"> 50% </div>
data-percentage属性代表圆形进度条的百分比,可以是负数,负数表示反向执行动画。
// progresscircle.js
(function($) {
$.fn.circlechart = function() {
this.each(function() {
var percentage = $(this).data("percentage");
var inner_text = $(this).text();
$(this).html(makesvg(percentage, inner_text));
});
return this;
};
// 添加的代码
$.fn.setChart = function(val) {
$(this).find('.circle-chart__circle').attr('stroke-dasharray', val + ',100').next().find('.circle-chart__percent').html(val + '%')
}
}(jQuery));
// 调用
$('.circlechart').circlechart(); // 初始化
setInterval(() => {
// 设置圆环
$('.circlechart').data('percentage', $('.circlechart').data('percentage') + 1)
// 设置文字百分比
$('.circlechart').setChart($('.circlechart').data('percentage') + 1);
}, 2000)