Skip to content

Instantly share code, notes, and snippets.

@FrancescoQ
Forked from charliepark/hatchshow.js
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancescoQ/31e43d72f3f6a05e0f9e to your computer and use it in GitHub Desktop.
Save FrancescoQ/31e43d72f3f6a05e0f9e to your computer and use it in GitHub Desktop.
(function ($) {
$(window).load(function(){
$().hatchShow();
});
jQuery.fn.hatchShow = function(){
$this = $(this);
$this.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();
var reduceText = false;
if (t.width() > pw) {
reduceText = true;
}
if (reduceText) {
while( t.width() > pw ){
t.css('font-size', (t.fontSize()-1)+"px");
};
}
else {
while( t.width() < pw ){
t.css('font-size', (t.fontSize()+1)+"px");
};
if (t.width() > pw) {
while( t.width() > pw ){
t.css('font-size', (t.fontSize() - 1)+"px");
};
}
}
t.css('line-height', '1em');
}).css('visibility','visible');
};
jQuery.fn.fontSize = function(){return parseInt($(this).css('font-size').replace('px',''));};
})(jQuery);
@FrancescoQ
Copy link
Author

Some settingis for my needs, and a "bug fixing" that make the script going in to an infinite loop if starting width of text was larger than the target width (so i have to reduce the font)

-Added the anonymous function (function ($) ... )(jQuery) to make always possible to use $ as jQuery (i'm using in on Drupal and the default setting doesen't permitt, to keep compatibility with other js framework)

-Added a control on the start text width to understand if i have to make the font greater o smaller

-Added the possibility to call the script to specified jQuery selector

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