Last active
December 21, 2015 03:59
-
-
Save seafoox/6246517 to your computer and use it in GitHub Desktop.
Textfill Module // auto font resize based on the parent width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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