-
-
Save ADParris/4187613 to your computer and use it in GitHub Desktop.
jQuery Plugin to resize text to fit container
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
(function($) { | |
$.fn.textfill = function(maxFontSize) { | |
maxFontSize = parseInt(maxFontSize, 10); | |
return this.each(function(){ | |
var ourText = $("span", this); | |
function resizefont(){ | |
var parent = ourText.parent(), | |
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