Skip to content

Instantly share code, notes, and snippets.

@tobinibot
Forked from charliepark/hatchshow.js
Created December 4, 2012 22:44
Show Gist options
  • Select an option

  • Save tobinibot/4209752 to your computer and use it in GitHub Desktop.

Select an option

Save tobinibot/4209752 to your computer and use it in GitHub Desktop.
A jquery typography plugin.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(window).load(function(){
$().hatchShow();
});
jQuery.fn.hatchShow = function (maxSize) {
if (!maxSize)
maxSize = 10000;
$('.hsjs').css('display', 'inner-block').css('white-space', 'pre').each(function () {
var t = $(this);
t.wrap("<span class='hatchshow_temp' style='display:block; text-align: center;'>");
var pw = t.parent().width();
while (t.width() < pw && t.fontSize() <= maxSize) {
t.css('font-size', (t.fontSize() + 1) + "px"), function () {
while (t.width() > pw) {
t.css('font-size', (t.fontSize() - .1) + "px");
}
};
}
}).css('visibility', 'visible');
};
jQuery.fn.fontSize = function () {
return parseInt($(this).css('font-size').replace('px', ''));
};
</script>
@tobinibot
Copy link
Copy Markdown
Author

Forked to allow a max point size to be passed in

@tobinibot
Copy link
Copy Markdown
Author

Also center the text in the container, this is useful when a max font size is passed in and the text does not cover the entire width

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment