Skip to content

Instantly share code, notes, and snippets.

@tphalp
Forked from juliankrispel/jquery.textfill.js
Created March 5, 2013 19:16
Show Gist options
  • Save tphalp/5093264 to your computer and use it in GitHub Desktop.
Save tphalp/5093264 to your computer and use it in GitHub Desktop.
Same as https://gist.github.com/tphalp/5093228, except that this one recalculates the font size when the window is re-sized.
(function($) {
$.fn.textfill = function(maxFontSize) {
maxFontSize = parseInt(maxFontSize, 10);
return this.each(function(){
var ourText = $("span", this);
function resizefont(){
var parent = ourText.parent(),
maxHeight = parent.height(),
maxWidth = parent.width(),
fontSize = parseInt(ourText.css("fontSize"), 10),
multiplier = maxWidth/ourText.width(),
newSize = (fontSize*(multiplier));
ourText.css("fontSize", maxFontSize > 0 && newSize > maxFontSize ? maxFontSize : newSize );
}
$(window).resize(function(){
resizefont();
});
resizefont();
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment