/* * file: jquery.barfiller.js * version: 1.0.1 * description: a plugin that fills bars with a percentage you set. * author: 9bit studios * copyright 2012, 9bit studios * http://www.9bitstudios.com * free to use and abuse under the mit license. * http://www.opensource.org/licenses/mit-license.php */ (function ($) { $.fn.barfiller = function (options) { var defaults = $.extend({ barcolor: '#16b597', tooltip: true, duration: 1000, animateonresize: true, symbol: "%" }, options); /****************************** private variables *******************************/ var object = $(this); var settings = $.extend(defaults, options); var barwidth = object.width(); var fill = object.find('.fill'); var tooltip = object.find('.tip'); var fillpercentage = fill.attr('data-percentage'); var resizetimeout; var transitionsupport = false; var transitionprefix; /****************************** public methods *******************************/ var methods = { init: function() { return this.each(function () { if(methods.gettransitionsupport()) { transitionsupport = true; transitionprefix = methods.gettransitionprefix(); } methods.appendhtml(); methods.seteventhandlers(); methods.initializeitems(); }); }, /****************************** append html *******************************/ appendhtml: function() { fill.css('background', settings.barcolor); if(!settings.tooltip) { tooltip.css('display', 'none'); } tooltip.text(fillpercentage + settings.symbol); }, /****************************** set event handlers *******************************/ seteventhandlers: function() { if(settings.animateonresize) { $(window).on("resize", function(event){ cleartimeout(resizetimeout); resizetimeout = settimeout(function() { methods.refill(); }, 300); }); } }, /****************************** initialize *******************************/ initializeitems: function() { var pctwidth = methods.calculatefill(fillpercentage); object.find('.tipwrap').css({ display: 'inline' }); if(transitionsupport) methods.transitionfill(pctwidth); else methods.animatefill(pctwidth); }, gettransitionsupport: function() { var thisbody = document.body || document.documentelement, thisstyle = thisbody.style; var support = thisstyle.transition !== undefined || thisstyle.webkittransition !== undefined || thisstyle.moztransition !== undefined || thisstyle.mstransition !== undefined || thisstyle.otransition !== undefined; return support; }, gettransitionprefix: function() { if(/mozilla/.test(navigator.useragent.tolowercase()) && !/webkit/.test(navigator.useragent.tolowercase())) { return '-moz-transition'; } if(/webkit/.test(navigator.useragent.tolowercase())) { return '-webkit-transition'; } if(/opera/.test(navigator.useragent.tolowercase())) { return '-o-transition'; } if (/msie/.test(navigator.useragent.tolowercase())) { return '-ms-transition'; } else { return 'transition'; } }, gettransition: function(val, time, type) { var cssobj; if(type === 'width') { cssobj = { width : val }; } else if (type === 'left') { cssobj = { left: val }; } time = time/1000; cssobj[transitionprefix] = type+' '+time+'s ease-in-out'; return cssobj; }, refill: function() { fill.css('width', 0); tooltip.css('left', 0); barwidth = object.width(); methods.initializeitems(); }, calculatefill: function(percentage) { percentage = percentage * 0.01; var finalwidth = barwidth * percentage; return finalwidth; }, transitionfill: function(barwidth) { var tooltipoffset = barwidth - tooltip.width(); fill.css( methods.gettransition(barwidth, settings.duration, 'width')); tooltip.css( methods.gettransition(tooltipoffset, settings.duration, 'left')); }, animatefill: function(barwidth) { var tooltipoffset = barwidth - tooltip.width(); fill.stop().animate({width: '+=' + barwidth}, settings.duration); tooltip.stop().animate({left: '+=' + tooltipoffset}, settings.duration); } }; if (methods[options]) { // $("#element").pluginname('methodname', 'arg1', 'arg2'); return methods[options].apply(this, array.prototype.slice.call(arguments, 1)); } else if (typeof options === 'object' || !options) { // $("#element").pluginname({ option: 1, option:2 }); return methods.init.apply(this); } else { $.error( 'method "' + method + '" does not exist in barfiller plugin!'); } }; })(jquery);