Skip to content

Instantly share code, notes, and snippets.

@seafoox
Last active December 21, 2015 03:59
Show Gist options
  • Save seafoox/6246517 to your computer and use it in GitHub Desktop.
Save seafoox/6246517 to your computer and use it in GitHub Desktop.
Textfill Module // auto font resize based on the parent width
// Textfill Module
// ----------------
// auto font rezize based on the parent size
//
(function($, window) {
var initialFontSize = null;
$.fn.textfill = function(maxFontSize)
{
maxFontSize = parseInt(maxFontSize, 10);
return this.each(function()
{
var ourText = $(this);
initialFontSize = (initialFontSize) ? initialFontSize : parseInt(ourText.css("fontSize"), 10);
var parent = ourText.parent();
var maxHeight = parent.height();
var maxWidth = parent.width();
var multiplier = ourText.width()/maxWidth;
var newSize = (initialFontSize*(multiplier-0.1));
var finalFontSize = (maxFontSize > 0 && newSize > maxFontSize) ? maxFontSize : newSize;
console.log($(this).width(), maxWidth, newSize, finalFontSize);
ourText.css("fontSize", finalFontSize);
});
}
$(window).resize(function() {
$("#selector").textfill('30px');
});
})(jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment