Created
August 28, 2011 19:14
-
-
Save kenkeiter/1177073 to your computer and use it in GitHub Desktop.
JQuery zoom text to extents, supporting multiple inline spans.
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.textExtents = function(options){ | |
var spans = $('span:visible', this); | |
var heightMax = parseInt($(this).css('max-height')); | |
var widthMax = parseInt($(this).css('max-width')); | |
var fontSize = options.maxFontSizePx; | |
do{ | |
var textHeight = 0; | |
var textWidth = 0; | |
$(spans).each(function(){ | |
$(this).css('font-size', fontSize); | |
if($(this).height() > textHeight){ textHeight = $(this).height(); } | |
textWidth = textWidth + $(this).width(); | |
}); | |
fontSize = fontSize - 1; | |
} while ((textHeight > heightMax || textWidth > widthMax) && fontSize > 3); | |
return this; | |
} | |
})(jQuery); | |
$(document).ready(function(){ | |
$('.text-extents').textExtents({maxFontSizePx: 36}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment