I have never written a jquery plug-in before. When developing this, I wrote some code and looked at the definition of jquery plug-in (how to develop it, etc.), and it went back and forth.
The reason why I want to write this plug-in is mainly to facilitate the implementation of this type of function in future projects. In the previous questionnaire survey application, this was used to display the results, but it was not developed at the time. It doesn’t have to be a plug-in. It’s a lot of code that is annoying to read and troublesome to use (it took me a whole morning to adjust it).
Fortunately, this function is relatively simple and is just right for new handwriting plug-ins.
The specific effect is as shown in the figure:
This is also a simple proportion diagram. ( function ($) {
$.fn.percentbar = function (o) {
var options1 = $.extend({
var g=this.length;
var styleData=InitColor();
$(this).css({ "background": "none repeat scroll 0 0 #EFEFEF", "border": "1px solid #E2E2E2" ,"height": options1.height "px","width": options1.width "px","background-color":options1 .bar_bgcolor,"border-color":options1.bar_bordercolor,"border-width":options1.bar_borderwidth "px"});
$(this).each(function(index, element) {
$( element).append("
");
if(g==index 1)
{
$( element).find("span").animate({ width: Math.round(options1.percent[index] * options1.width) }, "slow",options1.callback)
}else
{
$(element).find("span").animate({ width: Math.round(options1.percent[index] * options1.width) }, "slow")
}
});
//Initialize the color bar
function InitColor() {
var o = [];
var n = ["#5dbc5b", "#6c81b6", "#9eb5f0", "#a5cbd6 ", "#aee7f8", "#c2f263", "#d843b3", "#d8e929", "#e58652", "#e7ab6d", "#ee335f", "#fbe096", "#ffc535"];
var q = n.slice();
for (var p = 0, l = g; p < l; p ) {
var k = Math.floor(Math.random() * q. length);
o.push(q[k]);
q.splice(k, 1);
if (q.length == 0) {
q = n.slice( )
}
}
return o
}
}
})(jQuery);
Originally I wanted to separate css, but in the end It is better to export it directly to the jquery plug-in, which is more convenient to use.
Example:
Copy code
The code is as follows:
$(". good").percentbar({ percent: [0.5, 0.4], width: 500 });