I've created this small jQuery extension that aims to reduce the size of text by setting% css% of CSS based on% of its container%.
The operation is simple, find the current font size and remove a pixel, then apply a new CSS definition for the element in question:
;(function ($) {
$.fn.fitWidth = function( options ) {
var opts = $.extend({
min: "11"
}, options );
var tamanho = parseInt(this.css('font-size'));
if (tamanho > opts.min) {
return this.css({
'font-size': tamanho-1
});
}
};
}( jQuery ));
However, there is a problem:
If the letter indicated by the user, or until the default minimum value is not low enough to avoid font-size
, calling this extension will cause an infinite loop:
$('#wrap span').each(function(){
var $this = $(this),
overflow = $this[0].scrollWidth > $this.width() ? true : false;
if (overflow) {
while (overflow) {
$this.fitWidth({"min":'1'});
overflow = $this[0].scrollWidth > $this.width() ? true : false;
}
}
});
With letter minimum to 11 for example, we will have an infinite loop.
Example for 1 JSFiddle , but beware of changing the value to test, an infinite loop of JavaScript may cause the browser to stop working.
Question:
How can I change the extension or call of it, in order to avoid infinite cycles?