javascript - Create a percentage with three numbers -
i'm trying write small script convert 3 numbers 3 percentages. jquery can set width of 3 coloured divs reflect 'spread' of these numbers.
think of 3 part loading bar.
i made pasta 2 part loading bar, have no idea how add third percentage. @ math able turn 3 numbers 3 percentages out of 100.
var xwidth = (+x/(+x+y)*100); var ywidth = (+100-xwidth); //var zwidth = (+100-ywidth);
add them = 45, divide each 45, 10/45 = .222 (or 22.22%), 15/45 = .333 (or 33.33%), 20/45 = .444 (or 44.44%)
if trying comment says, change code this:
var x = 10; var y = 15; var z = 20; var total = x+y+z; var xwidth = (x/total)*100; var ywidth = (y/total)*100; var zwidth = (z/total)*100; $(".x").css('width', xwidth+'%'); $(".y").css('width', ywidth+'%'); $(".z").css('width', zwidth+'%');
or, cleaner:
var x = 10; var y = 15; var z = 20; var total = x+y+z; $(".x").css('width', (x/total)*100+'%'); $(".y").css('width', (y/total)*100+'%'); $(".z").css('width', (z/total)*100+'%');
Comments
Post a Comment