jQuery实现html可联动的百分比进度条

2020-05-29 07:26:44易采站长站整理

background: url("img/stripes.png") 0 0 repeat;
background-color: #FF5400;
}

JS代码:


$(document).ready(function (){
var w = $('.a-percentage').width();
var pos_x = $('.a-percentage').offset().left;
var inti_x = $('.a-percentage').attr('data-x')*4;
setProgressAndColor(inti_x, w);

$('.a-percentage').click(function(e) {
var x = e.originalEvent.x || e.originalEvent.layerX || 0;
x = x - pos_x;
if(x > 0 && x < w){
setProgressAndColor(x, w);
}
});
});

function setProgressAndColor(p, width){
$('.a-percentage-bar').width(p)
$('.a-percentage-bar').css('background-color',calcColor(p));
var per = Math.floor(p/4);
$('.a-percentage-bar').attr('data-x',per);

$('.b-percentage-bar').width(width-p)
$('.b-percentage-bar').css('background-color',calcColor(width-p));
per = 100-per;
$('.b-percentage-bar').attr('data-x', per);
}

function calcColor(x){
var R = 255
var G = 15;
var tmp = Math.floor(G+x);//取整保证RGB值的准确
if(tmp <= 255){
return 'rgb(255,'+tmp+',15)'
} else {
R = (R-(tmp-255));
return 'rgb('+R+',255,15)'
}
}

用了简单JQuery做支撑,需要引入JQuery看效果。