jsfiddle http://jsfiddle.net/mekwall/fNyHs/で見つかったコードを使用しています。これは、コンテナーに合わせて動的テキストが必要な場合 (フォント サイズを変更することによって)、私の状況に最適と思われるためです。
(function($) {
$.fn.textfill = function(maxFontSize, maxWords) {
maxFontSize = parseInt(maxFontSize, 10);
maxWords = parseInt(maxWords, 10) || 4;
return this.each(function(){
var self = $(this),
orgText = self.text(),
fontSize = parseInt(self.css("fontSize"), 10),
lineHeight = parseInt(self.css("lineHeight"), 10),
maxHeight = self.height(),
maxWidth = self.width(),
words = self.text().split(" ");
function calcSize(text) {
var ourText = $("<span class='dyntextval'><a href='#' class='trigger'>"+text+"</a></span>").appendTo(self),
multiplier = maxWidth/ourText.width(),
newSize = fontSize*(multiplier-0.1);
ourText.css(
"fontSize",
(maxFontSize > 0 && newSize > maxFontSize) ?
maxFontSize :
newSize
);
var scrollHeight = self[0].scrollHeight;
if (scrollHeight > maxHeight) {
multiplier = maxHeight/scrollHeight;
newSize = (newSize*multiplier);
ourText.css(
"fontSize",
(maxFontSize > 0 && newSize > maxFontSize) ?
maxFontSize :
newSize
);
}
}
self.empty();
if (words.length > maxWords) {
while (words.length > 0) {
var newText = words.splice(0, maxWords).join(" ");
console.log
calcSize(newText);
self.append("<br>");
}
} else {
calcSize(orgText);
}
});
};
})(jQuery);
$(document).ready(function() {
$('.large').textfill({ maxFontPixels: 120, minFontPixels: 36});
$('.medium').textfill({ maxFontPixels: 32 });
$('.small').textfill({ maxFontPixels: 18 });
});
しかし、私はこの部分に問題があります:
var ourText = $(""+text+"").appendTo(self),
問題は、クリックすると別の div を切り替えるリンクが必要だということです。
トグル:
$(".side-toggle").hide();
$('.trigger').click(function() {
$(this).closest('.group').find('.side-toggle').slideToggle();
return false; //Prevent the browser jump to the link anchor
});
$(".toggle").click(function() {
$(this).next(".group").slideToggle();
});
トグルはそれ自体で機能しますが、コード テキスト フィル コードも実装すると機能しません。
html:
<div class="quepasa">
<div class="large artist">
<span class="dyntextval">
<a title="<?php the_title();?>" href="#" class="trigger">
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>+
</a>
</span>
</div>
</div>
方法を知るのに十分なjQueryの知識がありません:a)リンクを入れるには?b) トグル機能を維持する
誰かが私を助けてくれることを願っています。