Skip to content

Instantly share code, notes, and snippets.

@DoronShapiro
Last active December 11, 2015 08:09
Show Gist options
  • Save DoronShapiro/4571201 to your computer and use it in GitHub Desktop.
Save DoronShapiro/4571201 to your computer and use it in GitHub Desktop.
Modified to fill either width or height
(function($) {
$.fn.textfill = function(percentMax, direction) {
percentMax = parseFloat(percentMax);
return this.each(function(){
var ourText = $("span", this),
parent = ourText.parent(),
maxHeight = parent.height(),
maxWidth = parent.width(),
fontSize = parseInt(ourText.css("fontSize"), 10),
multiplier = (direction == "width") ?
maxWidth/ourText.width() :
maxHeight/ourText.height();
multiplier = (percentMax > 0) ?
percentMax * multiplier :
multiplier;
ourText.css(
"fontSize", (fontSize*(multiplier-0.1))
);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment