Skip to content

Instantly share code, notes, and snippets.

@charliepark
Created July 30, 2011 16:07
Show Gist options
  • Star 69 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save charliepark/1115680 to your computer and use it in GitHub Desktop.
Save charliepark/1115680 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(){
$('.hsjs').css('display','inner-block').css('white-space','pre').each(function(){
var t = $(this);
t.wrap("<span class='hatchshow_temp' style='display:block'>");
var pw = t.parent().width();
while( t.width() < pw ){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>
@bdelespierre
Copy link

@bdelespierre
Copy link

bdelespierre commented Nov 29, 2016

@mmswi A bit late (yes) but if you want it to be responsive:

    (function($){
        jQuery.fn.hatchShow = function(){
            return els = $(this), $(window).resize(function(){
                els.css({ display: 'inline-block', whiteSpace: 'pre' }).each(function(){
                    var parent = $(this).parent(),
                        fontSize = parseInt($(this).css('font-size').replace('px', '')),
                        maxWidth = parent.width() - (parent.outerWidth(true) - parent.width());

                    while ($(this).outerWidth(true) < maxWidth) {
                        $(this).css('font-size', ++fontSize)
                    }

                    while ($(this).outerWidth(true) > maxWidth) {
                        $(this).css('font-size', --fontSize)
                    }
                })
            }).resize(), els
        }
    })(jQuery);

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