Skip to content

Instantly share code, notes, and snippets.

@akheron
Created December 16, 2011 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akheron/1485792 to your computer and use it in GitHub Desktop.
Save akheron/1485792 to your computer and use it in GitHub Desktop.
/* JavaScript progress bar
*
* Reads the percentage value from the content of an element and
* replaces it with a progress bar.
*
* Example:
*
* <span class="progress">100 %</span>
* <script>
* $('.progress').progressbar();
* </script>
*/
(function($) {
$.fn.progressbar = function(options) {
var defaults = {
color: '#07f'
};
options = $.extend({}, defaults, options);
this.each(function() {
var text = $(this).html().replace('%', '').replace(/\s+/g, ''),
progress = parseFloat(text) / 10 | 0,
span = '<span>&nbsp;&nbsp;</span> ',
content = new Array(11).join(span);
$(this).html(content)
.find(':lt(' + progress + ')')
.css('background-color', options.color);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment