// ********* 均等割り付けスクリプト *********** // 改造などはご自由に行って結構ですが、このスクリプトそのものを販売することを禁じます。 // web屋 内藤 http://www.web-naito.net/ // ***************** 2013.9.8 ***************** (function( $ ){ $.fn.justify = function(options) { return this.each(function(){ var words = $(this).text(), origFontSize = $(this).css("font-size").replace("px", ""), wordsLength = $(this).text().length, lineText = "", fontRatio = 0; if(wordsLength == 1) { $(this).css("text-align", "center"); return; } // 現在の文章の幅を測る function grabPixelLineSize(text) { var dummy = '
' + text + '
'; $("body").append(dummy); var emW = $("#dummy").width(); $("#dummy").remove(); return emW; }; var wordsWidth = grabPixelLineSize(words); // 文字列の幅を取得する // 親要素の禁則処理を無効にする $(this).css("word-break", "keep-all"); var parentWidth = $(this).width(); // 親要素の幅を取得する var fontRatio = ( parentWidth - wordsWidth ) / (wordsLength - 1) /*- origFontSize.replace("px", "")*/; lineText += '' + words.slice(0, wordsLength-1) + '' + words.slice(wordsLength-1); $(this).html(lineText); }); }; })(jQuery);